Coding Requirements for Sharing Data in
- Declare the order, size, and data types of shared data consistently in the DLL and in all procedures importing the DLL exported data.
- If more than one thread or process can write to the common block simultaneously, use the appropriate features of the Windows* operating system to control access to the shared data. Such features include critical sections (for single process, multiple thread synchronization) and mutex objects (for multi-process synchronization).
Exporting and Importing Common Block
- Create a common block in the subprogram that will be built into a Fortran DLL. Export that common block with!DEC$ ATTRIBUTES DLLEXPORT, followed by theCOMMONstatement, associated data declarations, and any procedure declarations to be exported. For example:!DEC$ ATTRIBUTES DLLEXPORT :: /X/ COMMON /X/ C, B, A REAL C, B, A END ...If the Fortran DLL procedure contains only a common block declaration, you can use theBLOCK DATAstatement:BLOCK DATA T !DEC$ ATTRIBUTES DLLEXPORT :: /X/ COMMON /X/ C, B, A REAL C, B, A ENDThe Fortran procedure to be linked into a DLL can contain a procedure, such as the following:SUBROUTINE SETA(I) !DEC$ ATTRIBUTES DLLEXPORT :: SETA, /X/ COMMON /X/ C, B, A REAL C, B, A INTEGER I A = A + 1. I = I + 1 WRITE (6,*) 'In SETA subroutine, values of A and I:' , A, I RETURN END SUBROUTINE
- Refer to the common block in the main program with!DEC$ ATTRIBUTES DLLIMPORT, followed by the local data declarations and any procedure declarations defined in the exported DLL. For example:PROGRAM COMMONX !DEC$ ATTRIBUTES DLLIMPORT:: SETA, /X/ COMMON /X/ C, B, A REAL C, B, A, Q EQUIVALENCE (A,Q) A = 0. I = 0 WRITE (6,*) 'In Main program before calling SETA...' WRITE (6,*) 'values of A and I:' , A, I CALL SETA(I) WRITE (6,*) 'In Main program after calling SETA...' WRITE (6,*) 'values of A and I:' , Q, I A = A + 1. I = I + 1 WRITE (6,*) 'In Main program after incrementing values' END PROGRAM COMMONX