- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
hi,
I have two function calls from my C# code to Fortran (DoSomething() and PROGRESS()). I can call the function DoSomething() with no issues, but when i tried to add another function call (i.e PROGRESS()), i get the following error message.
An unhandled exception of type 'System.TypeLoadException' occurred in Microsoft.VisualStudio.HostingProcess.Utilities.dll
Additional information: Could not load type 'FortMod' from assembly 'FortMod, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'PROGRESS' has no implementation (no RVA).
c# code to call the methods
public static extern void DoSomething();
public static extern void PROGRESS(ref int PERCENT_COMPLETE, ref int CURRENT_STATUS);
private void ListenMainThread()
{
int PERCENT_COMPLETE = 0, CURRENT_STATUS = 0;
while (Cond)
{
PROGRESS(ref PERCENT_COMPLETE, ref CURRENT_STATUS);
}
}
Fortran Code:
SUBROUTINE PROGRESS(PERCENT_COMPLETE,CURRENT_STATUS)
!MS$ATTRIBUTES DLLEXPORT, ALIAS:'PROGRESS' :: PROGRESS
USE PROGRESS_DECLARATIONS
IMPLICIT NONE
INTEGER :: CURRENT_STATUS
INTEGER :: PERCENT_COMPLETE
CURRENT_STATUS=FLOAT(IPROGRESS(1))
PERCENT_COMPLETE=FLOAT(IPROGRESS(2))
RETURN
END
Please let me know if i am missing anything while adding another function call to my C# code.
Thank you!
-Chouhan
I have two function calls from my C# code to Fortran (DoSomething() and PROGRESS()). I can call the function DoSomething() with no issues, but when i tried to add another function call (i.e PROGRESS()), i get the following error message.
An unhandled exception of type 'System.TypeLoadException' occurred in Microsoft.VisualStudio.HostingProcess.Utilities.dll
Additional information: Could not load type 'FortMod' from assembly 'FortMod, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'PROGRESS' has no implementation (no RVA).
c# code to call the methods
public static extern void DoSomething();
public static extern void PROGRESS(ref int PERCENT_COMPLETE, ref int CURRENT_STATUS);
private void ListenMainThread()
{
int PERCENT_COMPLETE = 0, CURRENT_STATUS = 0;
while (Cond)
{
PROGRESS(ref PERCENT_COMPLETE, ref CURRENT_STATUS);
}
}
Fortran Code:
SUBROUTINE PROGRESS(PERCENT_COMPLETE,CURRENT_STATUS)
!MS$ATTRIBUTES DLLEXPORT, ALIAS:'PROGRESS' :: PROGRESS
USE PROGRESS_DECLARATIONS
IMPLICIT NONE
INTEGER :: CURRENT_STATUS
INTEGER :: PERCENT_COMPLETE
CURRENT_STATUS=FLOAT(IPROGRESS(1))
PERCENT_COMPLETE=FLOAT(IPROGRESS(2))
RETURN
END
Please let me know if i am missing anything while adding another function call to my C# code.
Thank you!
-Chouhan
Link Copied
9 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You declare current_status and percent_complete as INTEGER
You then set them with the FLOAT() command
You then set them with the FLOAT() command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This problem pops up whenhever i add a new Routine in Fortran project and try to call from my c# code.
This is the new Fortran Subroutine i added ..
SUBROUTINE RoutineTest(var1)
!MS$ATTRIBUTES DLLEXPORT,ALIAS:'RoutineTest' :: RoutineTest
IMPLICIT NONE
INTEGER :: var1
var1=var1+1
RETURN
END
Whenhever i try to declare this function my c# code(as shown below), the problem popsup..
public static extern void RoutineTest(ref int var1);
This is the new Fortran Subroutine i added ..
SUBROUTINE RoutineTest(var1)
!MS$ATTRIBUTES DLLEXPORT,ALIAS:'RoutineTest' :: RoutineTest
IMPLICIT NONE
INTEGER :: var1
var1=var1+1
RETURN
END
Whenhever i try to declare this function my c# code(as shown below), the problem popsup..
public static extern void RoutineTest(ref int var1);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I thought you had to use DllImport and "MarshallAs" to call unmanaged code from C#.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve,
I already have dllimport in my code and i am able to access a Fortran Routine method from my code. But when i tried to call another routine which is part of the same Fortran solution the problem occurs.
[DllImport(Fort.dll", CallingConvention = CallingConvention.Cdecl)]
i dont know about MarshallAs.
-Chouhan
I already have dllimport in my code and i am able to access a Fortran Routine method from my code. But when i tried to call another routine which is part of the same Fortran solution the problem occurs.
[DllImport(Fort.dll", CallingConvention = CallingConvention.Cdecl)]
i dont know about MarshallAs.
-Chouhan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I tried to run your solution. I get following error at the function call.
An unhandled exception of type 'System.BadImageFormatException' occurred in CsharpCallsFortran.exe
Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
I can see that the subroutine accepts 6 arguments, where as we are trying to pass 8 argument.
In my case i have character array in the call, which unmanagedType shuould i use to MarshallAs.
MarshalAs(UnmanagedType.?) for char array..
thank u!
An unhandled exception of type 'System.BadImageFormatException' occurred in CsharpCallsFortran.exe
Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
I can see that the subroutine accepts 6 arguments, where as we are trying to pass 8 argument.
In my case i have character array in the call, which unmanagedType shuould i use to MarshallAs.
MarshalAs(UnmanagedType.?) for char array..
thank u!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If youy are on a 64-bit system you must build the DLL as 64-bits (and I think specify x64 as the target architecture for the C# project. I haven't looked at this project in a while but I had to do that with the VB.NET samples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have bulid the solution as 64-bit. It runs perfectly now.
Now i tried to implement MarshallAs in my solution. It gives me following error.
An unhandled exception of type 'System.AccessViolationException' occurred in FortranTest1.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Even i tried to create a new solution (a simple fortran application) it gives me the same error.
pls help.
Now i tried to implement MarshallAs in my solution. It gives me following error.
An unhandled exception of type 'System.AccessViolationException' occurred in FortranTest1.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Even i tried to create a new solution (a simple fortran application) it gives me the same error.
pls help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tha Answer to the root problem is,
We should have dllimport statement for each method that we are trying to import from Fortran DLL.
ie.) [DllImport("Dll")
public static extern void SubRout1();
[DllImport("Dll")
public static extern void SubRout2();
We should have dllimport statement for each method that we are trying to import from Fortran DLL.
ie.) [DllImport("Dll")
public static extern void SubRout1();
[DllImport("Dll")
public static extern void SubRout2();

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page