The following COBOL source code is a simple example of a DLL source subprogram. When compiled, a DLL can contain numerous outermost programs, each of which is considered a subprogram within the DLL.
In the following example, the DLL contains only one subprogram. When another program calls the subprogram named MYDLL that is in the DLL named MYDLL.DLL and this subprogram runs, it will display the text MYDLL Entered on the computer screen.
IDENTIFICATION DIVISION.
PROGRAM-ID. MYDLL.
PROCEDURE DIVISION.
DISPLAY “MYDLL Entered”.
EXIT PROGRAM.
The .DEF file for the compiled MYDLL.CBL source program illustrates the statements most frequently used in module definition files that build DLLs.
;*********************************************** ;* MYDLL.DEF * ;* Description: Provides the module definition * ;* for MYDLL.DLL, a simple COBOL DLL * ;*********************************************** LIBRARY MYDLL INITINSTANCE TERMINSTANCE PROTMODE DATA MULTIPLE READWRITE LOADONCALL NONSHARED CODE LOADONCALL EXECUTEREAD EXPORTS MYDLL
The following COBOL source program calls MYDLL in MYDLL.DLL:
IDENTIFICATION DIVISION.
PROGRAM-ID. RTDLLRES.
*
* THIS PROGRAM USES CALL identifier to call a subprogram
* NAMED MYDLL in a DLL. IT REQUIRES A DLL
* NAMED MYDLL.DLL.
*
*
DATA DIVISION.
WORKING-STORAGE SECTION.
77 CALLNAM PIC IS X(8).
PROCEDURE DIVISION.
DISPLAY “Start sample program RTDLLRES”.
MOVE “MYDLL” TO CALLNAM.
CALL CALLNAM.
DISPLAY “RTDLLRES successful”.
STOP RUN.
In this example, the following statement is a reference to the single subprogram MYDLL of the DLL MYDLL.DLL:
MOVE “MYDLL” TO CALLNAM.
This DLL must be in a directory defined by the COBPATH environment variable.
The following program is identical to RTDLLRES.CBL, except that the call is made directly to a symbol exported in a .DEF file rather than to a user-defined word:
IDENTIFICATION DIVISION.
PROGRAM-ID. LTDLLRES.
*
* THIS PROGRAM CALLS A SUBPROGRAM CALLED MYDLL WHICH
* RESOLVES AT LINK-TIME. THE DLL IN WHICH “MYDLL”
* IS FOUND IS NOT REQUIRED TO BE NAMED MYDLL.DLL
*
PROCEDURE DIVISION.
DISPLAY “Start sample program LTDLLRES”.
CALL “MYDLL”
DISPLAY “LTDLLRES successful”.
STOP RUN.
In this example, the call to MYDLL is not a direct reference to MYDLL.DLL (although, in this case, the DLL happens to have the same name). The call is to the symbolic name MYDLL, which was exported in the MYDLL.DEF file. MYDLL, in turn, is the name of the only COBOL subprogram in MYDLL.DLL. When LTDLLRES is built using cob2, the linker will resolve the call to MYDLL in MYDLL.DLL.
Use cob2 to compile and link the DLL and main executable programs. The DLL and programs that call it must be compiled in separate steps.
Either of the following cob2 commands will build the DLL named MYDLL.DLL:
cob2 mydll.cbl mydll.def cob2 mydll.cbl -dll:mydll
The following cob2 command will build the program RTDLLRES.EXE, which calls the DLL subprogram MYDLL with runtime resolution:
cob2 rtdllres.cbl
The following cob2 command will build the program LTDLLRES.EXE, which calls the DLL subprogram MYDLL with link-time resolution:
cob2 ltdllres.cbl mydll.lib