Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

mixed programming(C#&Fortran) problems with strings

owenkid
Beginner
598 Views

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?

0 Kudos
2 Replies
joerg_kuthe
Novice
598 Views

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

www.qtsoftware.de


0 Kudos
owenkid
Beginner
598 Views

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.

0 Kudos
Reply