Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Passing a string to Fortran

Bradley_P_1
Beginner
2,933 Views

I've had quite a lot of luck interoperating between Fortran and C# using Robert Giesecke​'s "Unmanaged Exports" library.  However, one of the things I haven't been able to achieve yet is sending a string from C# to Fortran.  The worst of it is that I get no indication of what's wrong.  My Fortran application just crashes.  From all the research I've done, it would seem to me the following should work:

C# "sender"

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
    [MarshalAs(UnmanagedType.LPStr, SizeConst = 200)]
    public string Title;
    public int SourceNode;
    public int EndNode;
}

Fortran "receiver"

type, bind(c) :: MyFirstStruct
    character(200, kind=C_CHAR) :: Title
    integer :: SourceNode
    integer :: EndNode
end type

When I run this I get the following:

forrtl: severe (172): Program Exception - exception code = 0x4352 (17234)

Any help or ideas would be much appreciated!

Brad.

P.S.  I can include the entire source for both sides, if it helps - just let me know.

0 Kudos
1 Solution
GVautier
New Contributor II
2,907 Views

Hello

I agree about the fact that if it works from C to C# it will works from Fortran

I think you should go back to basis.

First, replace your function with a subroutine because Fortran creates a shadow argument for function returning structures a strings.

subroutine GetStruct(struct) bind(C, name="GetStruct")
	import MyFortranStruct
	type(MyFortranStruct) :: struct
!DEC$ ATTRIBUTES REFERENCE :: struct 
end subroutine 

and on C# side the equivalent of :

void GetStruct(MyFortranStruct *result)
   {
   string="This is the title, padded to 200 characters just to make sure 345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
   strncpy(&(result->Title),string,min(len(string),200));
   result->SourceNode = 123;
   result->EndNode = 321;
   return;
   }

2nd, on both sides (before the call to GetStruct in Fortran and in GetStruct function in C#), print the size and address of the structure and addresses of all members. They must match.

3nd, find a way in C# function GetStruct to fill an array of chars (because that's what is a Fortran string)

View solution in original post

0 Kudos
24 Replies
Bradley_P_1
Beginner
483 Views

Bradley P. wrote:

It is worth noting that, since the structure is created (i.e. allocated) in C#, this will likely be a memory leak.

I believe I fixed this quickly and easily by changing my C# code to the following:

        public static void GetStruct(out MyStruct myStruct)
        {
            myStruct.SourceNode = 123;
            myStruct.Title = "This is Title #1".PadRight(200).ToCharArray();
            myStruct.EndNode = 321;
        }

No changes were necessary on the Fortran side.

Brad.

0 Kudos
Anthony_Richards
New Contributor I
483 Views

Bradley,

Many thanks for persevering on this and for producing a working solution. I think that the full C# + Fortran solution would make a very useful mixed-language sample to be bundled with the Intel compiler.

No doubt someone will soon want a solution that permits arrays of such structures to be accessed in a similar way!

0 Kudos
Bradley_P_1
Beginner
483 Views

Anthony Richards wrote:

Bradley,

Many thanks for persevering on this and for producing a working solution. I think that the full C# + Fortran solution would make a very useful mixed-language sample to be bundled with the Intel compiler.

No doubt someone will soon want a solution that permits arrays of such structures to be accessed in a similar way!

Thanks!  The solution still isn't quite as complete as I'd like, but for now it's doing the job.

Brad.

0 Kudos
SR-9779
Beginner
483 Views

Bradley P. wrote:

Quote:

Anthony Richards wrote:

 

Bradley,

Many thanks for persevering on this and for producing a working solution. I think that the full C# + Fortran solution would make a very useful mixed-language sample to be bundled with the Intel compiler.

No doubt someone will soon want a solution that permits arrays of such structures to be accessed in a similar way!

 

 

Thanks!  The solution still isn't quite as complete as I'd like, but for now it's doing the job.

Brad.

It works - Thank you. And, I have created the lib for C# dll through the steps mentioned here - https://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/

 

What could I be missing here?

0 Kudos
Reply