Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28447 Discussions

Dynamically Importing Common Block Variables from a Compiled DLL

Nick3
New Contributor I
419 Views

I have a compiled DLL, call it Stone.dll, which I'm not allowed to re-compile, and which has many, many COMMON blocks, with many, many variables, exported like this:

!DIR$ ATTRIBUTES DLLEXPORT :: /FOM/
      DOUBLE PRECISION A,B,C,D,E,F,G
      COMMON/FOM/ A,B,C,D,E,F,G


I have another DLL that I'm writing, call it Flex.dll, where I would love to do the following to be able to read and modify variables from Stone.dll:

!DIR$ ATTRIBUTES DLLIMPORT :: /FOM/
      DOUBLE PRECISION A,B,C,D,E
      COMMON/FOM/ A,B,C,D,E

However, the main executable loads multiple instances of both Stone.dll and Flex.dll from multiple folder locations (so, yes, there are two or more instances of the COMMON variables).  The first Flex.dll should manipulate variables from the first Stone.dll, and so on.  Trouble comes because at run time, the second instance of Flex.dll is importing the data from the first Stone.dll.

So, how do I get the second instance of Flex.dll to resolve to the data of the second instance of Stone.dll?  I can use GetProcAddress after some clever WinAPI manipulation to get the correct address of the correct /FOM/ object.  But, then what?  Do I have to resolve each variable A,B,C,D, ... individually?  In C++ I would do this:

typedef struct FOM_struct { double A,B,C,D,E; } *pFOM_struct;
pFOM_struct cmnFOM = (pFOM_struct)GetProcAddress(hInst, "FOM");

Can the same (or similar) thing be done in Fortran?

Is there some trickery to tell !DIR$ ATTRIBUTES DLLIMPORT that it should use my preferred hInst result of GetModuleHandle rather than the ill-defined result of the operating system GetModuleHandle call that doesn't include the full path?

0 Kudos
1 Reply
Steve_Lionel
Honored Contributor III
419 Views

You can't DLLIMPORT the COMMON because all that will do is put out an external reference for the indirect pointer, and then have the compiler reference through that pointer. You certainly can do the equivalent of that C++ code, with a pointer of the derived type, and then use C_F_POINTER to convert the address you get from GetProcAddress to a Fortran pointer. I have never done this with data so you might need to first treat the return from GetProcAddress as the address of an address.

0 Kudos
Reply