


In addition to linking the "sub" function directly into the runtime, Windows users may also place the "sub" routine into one or more Dynamic Linked Library (DLL) files.
To do this, you must specify which routine to use as the "sub" interface routine by setting the DLL-SUB-INTERFACE configuration variable. Then you call the DLL from your COBOL program. The runtime loads the DLL, then checks DLL-SUB-INTERFACE for the name of the routine to use as the "sub" interface routine. For example, the following C program (subdll.c) is the source for the DLL that contains the "sub" interface, called AcuSub in this example:
#include <stdio.h>
#include <windows.h>
#include "sub.h"
#define DllExport __declspec(dllexport)
#define CCallingConvention __cdecl
DllExport int CCallingConvention
AcuSub( int argc, char *argv[] )
{
if ( strcmp( argv[0], "MSGBOX" ) == 0 ) {
MessageBox( NULL, argv[1], NULL, MB_OK );
return Okay;
}
return NotFound;
} /* AcuSub */
/* end of subdll.c */
The following COBOL program (callsub.cbl) shows how the DLL is loaded and called:
program-id. test.
working-storage section.
77 message-text pic x(80).
procedure division.
main-logic.
display standard window.
* Load DLL and establish "sub" interface
set environment "dll-sub-interface" to "AcuSub".
call "subdll".
* Call "MSGBOX", one of the routines handled by "AcuSub"
move "This is a test message" to message-text.
inspect message-text replacing trailing spaces by
low-values.
call "msgbox" using message-text.
accept omitted.
stop run.
If DLL-SUB-INTERFACE is blank when the DLL is loaded, then no "sub" routine is used in that DLL.
When a CALL statement executes, the "sub" interface routine in each loaded DLL is called, in the order that they were loaded, until one of them returns that it has executed the subroutine. If none of the DLLs returns success, the normal, or linked-in, "sub" routine is called. If that does not return success, then the standard calling sequence resumes. As soon as any routine returns success, the CALL is considered satisfied and no further processing of the CALL is done.
If you CANCEL a DLL with an active "sub" interface, that interface is removed from the list of available interfaces and the DLL is unloaded.