Groups | Blog | Home
all groups > dotnet interop > july 2003 >

dotnet interop : Return string from Fortran Dll back to C#.


Jugoslav Dujic
7/3/2003 1:51:37 PM
Instead of all groups you crossposted to, you had better posted
post this to (Compaq)Intel Fortran Developer Forum at

http://intel.forums.liveworld.com/forum.jsp?forum=76

OK, since I grumbled about the crosspost (few irrelevant
groups snipped), let me try to answer:

[quoted text, click to view]
| My C# program need a string returned from a Fortran Dll.
| The Fortran Dll is like this:
|
| SUBROUTINE REDO(s)

That compiles?
Shouldn't that be !DEC$ ATTRIBUTES C, DLLEXPORT::REDO instead?

| !DEC$ ATTRIBUTES DLLEXPORT::REDO, C

| CHARACTER*(*) s
| !DEC$ ATTRIBUTES REFERENCE :: s
| s = 'Let them talk, now!'C
| END

I'm not familiar with C#, but with the change above, the code will
be equivalent (interchangeable with) to:

extern "C" void __cdecl redo(char* s)
{
strcpy(s, "Let them talk, now!");
}

--
Jugoslav
___________
www.geocities.com/jdujic

Alex Dong
7/3/2003 5:45:54 PM
Dear all:
My C# program need a string returned from a Fortran Dll.
The Fortran Dll is like this:

SUBROUTINE REDO(s)
!DEC$ ATTRIBUTES DLLEXPORT::REDO, C
CHARACTER*(*) s
!DEC$ ATTRIBUTES REFERENCE :: s
s = 'Let them talk, now!'C
END

And I use C# PInvoke to call it, the client code like this:

public class BackString
{
[DllImport(@"BackString.dll",
EntryPoint="REDO")]
public static extern void REDO(string output);
}

string output = "";
BackString.REDO(output);

When I call this, I got an NullReferenceException, can anybody here explain
why? and how could I return the string?

I also tried to return the value as "Return" but have the same problem. The
..NET Framework's PInvoke example to use C's return string works fine, but I
just can't make the Fortran work.

Thank everybody and hope I could get some reply.

Regards
Alex

Vadim Melnik
7/4/2003 2:02:30 PM
Hi,

[quoted text, click to view]

StringBuilder should be used for string out arguments. How about the
following in C# ?

public static extern void REDO(StringBuilder output);

.....

StringBuilder sb = new StringBuilder(1024);
REDO(sb);
Console.WriteLine(sb);


...
Regards,
Vadim



xinhuang NO[at]SPAM online.microsoft.com (
7/7/2003 11:07:42 AM
What about

string output = new String( new char[ 256 ]);

Hope this helps.

Regards,
Xin

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. (c) 2003 Microsoft Corporation. All
rights reserved.
AddThis Social Bookmark Button