String returned from C# to Fortran - problem

anthonyrichards
Total Points:
4,222
Status Points:
3,722
Brown Belt
November 27, 2008 9:24 AM PST
Rate
 
#10 Reply to #9
Essentially, you have to reproduce in C# what is given below as the code for a C++  DLL (given the name mycdll for example) containing one function MyCDllRoutine. Note that given the calling convention used, this will export a symbol _MyCDllRoutine@8 in the generated symbol table mycdll.lib (note the preservation of case, the added leading underscore and the decoration @8). The Fortran code to produce a Console application (so that you can write to the screen) that calls the DLL is given below the C++ code. In order for the Fortran program to work, the file mycdll.lib must be added to the Fortran project and the mycdll.dll code added to the folder containing the Fortran executable.
// mycdll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

// myDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
      )
{
    return TRUE;
}

   extern "C" __declspec(dllexport)  void  __stdcall MyCDllRoutine(int a, int b) ;


// MAIN ENTRY FUNCTION
void  __stdcall MyCDllRoutine (int a, int b)
{
	char achar[10], bchar[10], str[80];

itoa(a,achar,10); 
itoa(b,bchar,10);
strcpy(str," a = ");
strcat(str,achar);
strcat(str,", b = ");
strcat(str,bchar);
cout <<" Entered MyCDllRoutine in the C++ DLL"< 
Fortran Code:
!****************************************************************************
!
!  PROGRAM: testmycdll
!
!  PURPOSE:  Entry point for 'Hello World' sample console application to access
!			 a routine held in a Win32 Dynamic link library compiled from C++ code
!
!****************************************************************************

	program testmycdll

implicit none
!
INTEGER I,J

       INTERFACE
       SUBROUTINE MYCDLLROUTINE (I,J)

       !DEC$ ATTRIBUTES DLLIMPORT :: MYCDLLROUTINE
       !DEC$ ATTRIBUTES STDCALL :: MYCDLLROUTINE
       !DEC$ ATTRIBUTES ALIAS:'_MyCDllRoutine@8' :: MYCDLLROUTINE
	   INTEGER I,J

       END SUBROUTINE MYCDLLROUTINE
       END INTERFACE

 print *, 'Hello World'
 I=10
 J=25

       CALL MYCDLLROUTINE(I,J)

	end program testmycdll



Intel Software Network Forums Statistics

8290 users have contributed to 31236 threads and 99111 posts to date.
In the past 24 hours, we have 7 new thread(s) 20 new posts(s), and 30 new user(s).

In the past 3 days, the most popular thread for everyone has been comparison cilk++, openmp, pthreads first results The most posts were made to comparison cilk++, openmp, pthreads first results The post with the most views is Very amusing...  Escalated as

Please welcome our newest member zq.x