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

Calling Visual Fortran DLL from Visual C#

kim7
Einsteiger
1.689Aufrufe

Hello,

I am writing an application using Visual C# .NET. The application calls Visual Fortran 6.6A subroutines in a DLL.

I have been able to call test Fortran subroutines in the DLL from C#, change values in floating point and integer arrays in the subroutines and pass the new values back to C#.

I am having trouble passing string values to a Fortran subroutine. I can pass one string and access the string. I was able to use a System.Text.Stringbuilder and with some odd manipulation in C# of the returned values, I was able to change the string in a subroutine and pass the new string back to C#. But I cannot pass more than one string.

Please reply with any suggestions or hints you might have for this. Thank you.

Best,


Kim
0 Kudos
6 Antworten
durisinm
Anfänger
1.689Aufrufe
Fortran strings are passed with a hidden length. Visual Fortran's on-line help has a "mixed-language programming" topic under the "Strings" heading that explains a lot about this. There is no specific information about exchanging strings between C# and Fortran, but it will probably help you. The example programs that come with VF include ones dealing with passing strings.

Mike
kim7
Einsteiger
1.689Aufrufe
Mike,

Thanks much for your reply. I have printed out the Visual Fortran help information on mixed languaged programming and strings and think this is key information. I do not have success to report yet and will work on this again on Monday when a colleague with much expertise in Fortran returns from vacation.

Best,

Kim
durisinm
Anfänger
1.689Aufrufe
The Canaima Software Web site has some good manuals and tutorials about calling Visual Fortran from Visual Basic. I think that they contain a discussion of passing strings that may help you.

Mike
kim7
Einsteiger
1.689Aufrufe

Mike,

Thanks for the additional site. I am keeping it for future reference.

I found an example from the Compaq Fortran web site to be helpful.

This morning we successfully passed from Visual C# to Visual Fortran a string by value (not to be changed) and another string by reference (changed on the Fortran side and successfully read back on the C# side).

C# programmers, also look at the "Platform Invoke Tutorial" in Help for Visual Studio.

Best,

Kim


Visual C#

using System.Runtime.InteropServices;

...

[DllImport("TestFOR.dll")]
public static extern void HELLO_S(
[MarshalAs(UnmanagedType.LPStr)]
System.Text.StringBuilder TEST,
[MarshalAs(UnmanagedType.I4)]
int TEST_LEN,
[MarshalAs(UnmanagedType.LPStr)]
System.Text.StringBuilder MESSAGE,
[MarshalAs(UnmanagedType.I4)]
int MESSAGE_LEN);

...

StringBuilder TEST = new StringBuilder("TESTVAL");
StringBuilder MESSAGE = new StringBuilder("01234567890123456789012345678901234567890123456789012345678901234567890123456789"); // 80 chars. max

...

HELLO_S(TEST, TEST.Length, MESSAGE, MESSAGE.Length);


Visual Fortran

SUBROUTINE HELLO_S(TEST,MESSAGE)

! Expose subroutine HELLO_S to users of this DLL
!
!DEC$ ATTRIBUTES DLLEXPORT::HELLO_S

CHARACTER TEST*(*)
CHARACTER MESSAGE*(*)
WRITE(10,'(1X,A)') 'TEST="'//TEST//'"'
IF(TEST.EQ.'TESTVAL') THEN
WRITE(10,'(1X,A)') 'TEST.EQ.TESTVAL'
ELSE
WRITE(10,'(1X,A)') 'TEST.NE.TESTVAL'
ENDIF
L=LEN(TEST)
WRITE(10,*) 'LEN(TEST)=',L
L2=LEN(MESSAGE)
WRITE(10,*) 'LEN(MESSAGE)=',L2
WRITE(10,'(1X,A)') 'MESSAGE="'//MESSAGE//'"'
MESSAGE='Test Message'//CHAR(0)
WRITE(10,'(1X,A)') 'MESSAGE="'//MESSAGE//'"'
RETURN
END


durisinm
Anfänger
1.689Aufrufe
Steve,

Is this a good candidate to include with VF examples? More and more people are going to be mixing VF with .NET languages.

Mike
Steven_L_Intel1
Mitarbeiter
1.689Aufrufe
I'm sure we'll be adding examples like this in the future.

Steve
Antworten