Quantcast
Channel: Recent posts
Viewing all articles
Browse latest Browse all 415

Calling Fortran DLL from C++ DLL using Visual Studio 2015

$
0
0

Dear All,

I have a DLL written in C++ and a program written in Fortran. What I need to do is to transform the Fortran program into a DLL and make the C++ DLL able to call it.

I read several topics on this forum but I am still having trouble: LoadLibrary fails. I don't know if this is a problem due to errors in compiling the Fortran DLL or due to errors in the C++ side.

So far I have:

- Fortran side:

!!!!!PROGRAM MAIN
MODULE MAIN

USE, INTRINSIC :: ISO_C_Binding

USE MODULE_Subs  ! This has all the subroutines needed in the program

IMPLICIT  NONE

CONTAINS

SUBROUTINE FORTRAN_DLL ( ind_TimeStep, avrSWAP ) BIND (C, NAME='FORTRAN_DLL')
!DEC$ ATTRIBUTES DLLEXPORT :: FORTRAN_DLL

IMPLICIT  NONE

INTEGER(C_INT),         INTENT(IN)    :: ind_TimeStep
REAL(C_FLOAT),          INTENT(INOUT) :: avrSWAP   (*)


!!!!! Instructions


RETURN

CONTAINS

!!!!! Some other subroutines used only here

!!!!!END PROGRAM MAIN
END SUBROUTINE FORTRAN_DLL

END MODULE MAIN

 

- C++ side:

#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_SECURE_NO_WARNINGS  1

#include <windows.h> //CED - for external DLL management
#include <fstream>

HINSTANCE h_FORTRAN_dll;

typedef void (*__FORTRAN_DLL)(int, float *);

__FORTRAN_DLL  FORTRAN_DLL;

// Variables for FORTRAN DLL
int ind_TimeStep = 1;

//avoid mangled names
extern "C"
{
void __declspec(dllexport) __cdecl C_DLL(float *avrSwap,
                             int *aviFail,
                             char *accInfile,
                             char *avcOutname,
                             char *avcMsg);
}

//Main DLL routine
void __declspec(dllexport) __cdecl C_DLL (float *avrSwap,
                             int *aviFail,
                             char *accInfile,
                             char *avcOutname,
                             char *avcMsg)
{

			if (!(h_FORTRAN_dll = LoadLibrary(".\..\Folder\FORTRAN_DLL.dll")))
			{
				printf("\n*** FORTRAN_DLL.dll not found. Aborting.\n"); exit(-1); return;
			}

			FORTRAN_DLL = (__FORTRAN_DLL)GetProcAddress(h_FORTRAN_dll, "FORTRAN_DLL");


			FORTRAN_DLL(ind_TimeStep, avrSwap );

			ind_TimeStep = ind_TimeStep + 1;


}

This C++ DLL does not have to do anything but pass avrSWAP(coming from the main program) to the Fortran DLL.

C++ DLL is compiled in release version in Win32 Configuration.

Fortran DLL is compiled in release versione in x86 Configuration.

 

Any help would be appreciated.

Thank you in advance

 

Best regards,

Matteo Strada


Viewing all articles
Browse latest Browse all 415

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>