- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
PROGRAM UseCDll
USE kernel32
IMPLICIT NONE
!DEC$
!DEC$ ATTRIBUTES ALIAS:'_idata@@3HA'::idata
integer idata
common / idata / idata
INTEGER i,ret
POINTER(p_USERFUNC,compute)
INTEGER(HANDLE)::dll_handle
INTEGER(BOOL)::free_status
INTERFACE
integer function compute () BIND(C)
USE,INTRINSIC :: ISO_C_BINDING
implicit none
END function compute
END INTERFACE
write (*,*) "Loading library..."
dll_handle = LoadLibrary ("cdll-external.dll"C)
write (*,*) "Getting routine address..."
p_USERFUNC = GetProcAddress (dll_handle,"compute"C)
print *,'before idata=',idata
ret=compute()
print *,'after idata=',idata
write(*,*) "Unloading library..."
free_status = FreeLibrary (dll_handle)
END PROGRAM UseCDll
----------------------------------------------------------------------------------
This is FORTRAN code that defines the external variable idata.
It was built well.
-----------------------------------------------------------------------------------
extern int idata;
extern "C" __declspec(dllexport) void compute()
{
idata=343;
return;
}
---------------------------------------------------------------------------
This isC DLL file that uses the external variable defined in FORTRAN.
I couldn't build this code. The error was LNK2001 that the compiler doesn't check the external variable idata in 'CDLL-external.obj'
Can someone suggest how I can use the external variable between FORTRAN and C?
Thank you
USE kernel32
IMPLICIT NONE
!DEC$
!DEC$ ATTRIBUTES ALIAS:'_idata@@3HA'::idata
integer idata
common / idata / idata
INTEGER i,ret
POINTER(p_USERFUNC,compute)
INTEGER(HANDLE)::dll_handle
INTEGER(BOOL)::free_status
INTERFACE
integer function compute () BIND(C)
USE,INTRINSIC :: ISO_C_BINDING
implicit none
END function compute
END INTERFACE
write (*,*) "Loading library..."
dll_handle = LoadLibrary ("cdll-external.dll"C)
write (*,*) "Getting routine address..."
p_USERFUNC = GetProcAddress (dll_handle,"compute"C)
print *,'before idata=',idata
ret=compute()
print *,'after idata=',idata
write(*,*) "Unloading library..."
free_status = FreeLibrary (dll_handle)
END PROGRAM UseCDll
----------------------------------------------------------------------------------
This is FORTRAN code that defines the external variable idata.
It was built well.
-----------------------------------------------------------------------------------
extern int idata;
extern "C" __declspec(dllexport) void compute()
{
idata=343;
return;
}
---------------------------------------------------------------------------
This isC DLL file that uses the external variable defined in FORTRAN.
I couldn't build this code. The error was LNK2001 that the compiler doesn't check the external variable idata in 'CDLL-external.obj'
Can someone suggest how I can use the external variable between FORTRAN and C?
Thank you
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You've complicated things by not only putting the C code in a DLL but also doing dynamic load. If you had not done dynamic load, you could DLLEXPORT the variable and then DLLIMPORT it in Fortran. Otherwise, you will still need to DLLEXPORT the variable from C, do a GetProcAddress of the variable global symbol which will actually be a pointer to the variable. I don't have time right now to build an example.
Do you really need the DLL and the dynamic load in your application?
Let me suggest an alternative which is to have routines in the DLL to get and set the variable. Alternatively, you could have a routine that returns a pointer to the variable.
Do you really need the DLL and the dynamic load in your application?
Let me suggest an alternative which is to have routines in the DLL to get and set the variable. Alternatively, you could have a routine that returns a pointer to the variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your reply.
I already tried to add this line '!DEC$ ATTRIBUTES DLLEXPORT::idata', but C compiler still doesn't know about 'idata'.
The total structure is like below.
VS2008 IDE ------------- Project A(FORTRAN) - Definean external variable idata 'common / idata / idata'
|
------- Project B(C DLL) - refer the external variable idata from 'Project A'
My question is that how the project B can know about the idata outside itself...
Thanks a lot.
I already tried to add this line '!DEC$ ATTRIBUTES DLLEXPORT::idata', but C compiler still doesn't know about 'idata'.
The total structure is like below.
VS2008 IDE ------------- Project A(FORTRAN) - Definean external variable idata 'common / idata / idata'
|
------- Project B(C DLL) - refer the external variable idata from 'Project A'
My question is that how the project B can know about the idata outside itself...
Thanks a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The dllexport has to be in the DLL, which is your C project.
I can tell you how to make this work with a linked DLL, but doing it with a loaded DLL is a bit tougher.
I can tell you how to make this work with a linked DLL, but doing it with a loaded DLL is a bit tougher.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much.
Can I get a simple example to use static C DLL from FORTRAN code?
Can I get a simple example to use static C DLL from FORTRAN code?
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page