Rational Developer for System z
COBOL for Windows, Version 7.5, Programming Guide


Example: COBOL program calling C/C++ DLL

The following example shows a COBOL program that calls a C/C++ dynamic link library (DLL) by using the CALL statement.

The example illustrates the following concepts:

Do the following steps to create and run the example:

  1. Create the file sub.c from the following source. This will be your DLL.

    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID);
    _declspec (dllexport) int Sub(void);
    
    BOOL WINAPI  DllMain (
       HANDLE    hModule,
       DWORD     dwFunction,
       LPVOID    lpNot)
    {
    
       return TRUE;
    }
    
     _declspec (dllexport) int Sub(void)
        {
          printf (“Hello from sub.\n”);
            return 0;
        }
  2. Create a module definition file named sub.def that has the following statements. This definition file will make Sub an external entry point.

     LIBRARY Sub
    
     EXPORTS
       Sub
  3. Compile the DLL by using the following command. The result will be a file named sub.dll that will be called by COBOL.

    cl /EHsc /LD /DLL /DEF:sub.def sub.c
  4. Create a COBOL program file driver.cbl with the following source to call sub.dll:

    cbl CALLINT(CDECL),PGMNAME(MIXED),LIST
         Identification Division.
         Program-Id. “Driver”.
         Data Division.
         Working-Storage Section.
         77  rc pic 9(8) usage binary.
         Procedure Division.
             Display “Hello World, from Driver”
    
             Call “Sub” returning rc
             Display “Sub Returned to Driver”
    
             Goback.
  5. Compile the COBOL program and link it to the DLL by using the following command:

    cob2 -g -v driver.cbl -IMP:sub.lib
  6. Run the sample by issuing the command driver.

related tasks
Building dynamic link libraries

related references
cob2 options
CDECL
CALL statement (COBOL for Windows Language Reference)


Terms of use | Feedback

Copyright IBM Corporation 1996, 2008.
This information center is powered by Eclipse technology. (http://www.eclipse.org)