- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I try to call a Fortransubroutine by aC# program.
Fortran looks like this:
SUBROUTINE dynamicLIBSTRING(DATA_FILE,Error,Message)
!DEC$ ATTRIBUTES DLLEXPORT::DYNAMICLIBSTRING
!DEC$ ATTRIBUTES ALIAS:"dynamicLIBSTRING"::DYNAMICLIBSTRING
CHARACTER*(*) DATA_FILE
CHARACTER*(*) Message
integer:: Error
OPEN(12,FILE=DATA_FILE,FORM='FORMATTED',MODE='WRITE')
WRITE(12,*) Error
WRITE(12,*) DATA_FILE
WRITE(12,*) Message
CLOSE(12)
END SUBROUTINE
C#:
[DllImport("dynamiclib.dll")]
public static extern void dynamicLIBSTRING(string data_file, int data_file_len, int error, string msg, int msg_len);
private void button2_Click(object sender, EventArgs e)
{
string name = "123.bdg";
string Message = "hello";
int err = 1;
dynamicLIBSTRING(name, name.Length, err, Message, Message.Length);
}
But when I run the C# program, I got the following error:Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I also put all the code in the attachment.
can sombody help me here?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My impression is that the error is due to the fact that Intel Visual Fortran passes the hidden string length arguments AFTER all arguments. At least this is the default. Thus, either you change that in the "Properties" (Fortran / External Procedures) of your Fortran project or change the call in your C# code accordingly.
I guess you know that Fortran passes arguments by reference with the exception that thos hidden length arguments are passed by value. But I want to make sure that this is not another reason for an error. Good luck
Jrg Kuthe
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
thanks,Joerg.
That's the reason why I get that error.
And I also found out that if you want to return the string values that be changed in the Fortran dll,you have to use the StringBuilder.
Unfortunately, I encounter another problem, I can't step into the Fortran code with the debug mode. I hope some guys here can fix it.

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