[Mono-dev] Dllimporting Fortran API in C#

Sunil Venkateswara sunvenk at hotmail.com
Fri Jul 14 02:21:11 EDT 2006


I am exporting a function in fortran and in another fortran program 
importing this API as follows, it works fine
!----------
! FORTRAN EXPORT
!-------------------
MODULE TestExport
PUBLIC GETSTRING
Contains
SUBROUTINE GETSTRING(str)
!DEC$ ATTRIBUTES DLLEXPORT::GETSTRING
CHARACTER(LEN=*) :: str
!DEC$ ATTRIBUTES REFERENCE :: s
str = 'String from FORTRAN'
END SUBROUTINE GETSTRING
END MODULE TestExport
!----------
! FORTRAN IMPORT
!-------------------
	program TestImport
	use TestExport
	implicit none
	character(1024)  str
	str = 'Hello from Import'
	WRITE(*,*) TRIM(str)
	CALL GETSTRING(str)
	WRITE(*,*) TRIM(str)
	end program TestImport

Now when I use a C# console application to import the same fortran code by 
DLLIMPORT,
using the following code ( added a write call and removed module definition)
!----------
! FORTRAN EXPORT
!-------------------
SUBROUTINE GETSTRING(str)
!DEC$ ATTRIBUTES DLLEXPORT::GETSTRING
CHARACTER(LEN=*) :: str
WRITE(*,*) 'I am in Fortran'
!DEC$ ATTRIBUTES REFERENCE :: str
str = 'String from FORTRAN'
END SUBROUTINE GETSTRING

/* C# Import */
[ DllImport( "TestExport.dll" )]
public static extern void GETSTRING([In,Out]string strMessage);
static void Main(string[] args)
{
	string strMessage = "String from C#";
	Console.WriteLine(strMessage);
	GETSTRING( strMessage);
	Console.WriteLine(strMessage);
	Console.WriteLine("Finished call to Fortran");
}
OS : Windows XP Professional
IN MS CLR 1.1 --> I dont get the required O/p, in fortran while string gets 
assigned, it crashes and just comes out without showing any exception.
IN Mono --> I get "Unhandled Exception: System.NullReferenceException: 
Object reference not set toan instance of an object", the o/p is String from 
C#  I am in Fortran. So fortran write statement is executed, but while 
assgining value to string, throws error.

Am I doing anything wrong?


Thanks
Sunil

_________________________________________________________________
Shah Rukh fan? Know all about the Baadshah of Bollywood. On MSN Search 
http://server1.msn.co.in/profile/shahrukh.asp




More information about the Mono-devel-list mailing list