Calling Windows API from managed COBOL

Hello,

I need to call Windows API function from managed COBOL. In native COBOL I'd write something like

environment division.
special-names.
call-convention 74 is WAPI.

...

working-storage section.

01 VersionInfoEx.
   03 dwEVersionInfoSize        DWORD.
   03 dwEMajorVersion           DWORD.
   03 dwEMinorVersion           DWORD.
   03 dwEBuildNumberFull.
      05 dwEBuildNumber         USHORT.
      05 dwEMajorAndMinor       USHORT.
   03 dwEPlatformId             DWORD.
   03 szECSDVersion             pic x(128).
   03 wEServicePackMajor        WORD.
   03 wEServicePackMinor        WORD.
   03 wESuiteMask               WORD.
   03 wEProduktType             BYTE.
      88 ver-nt-workstation        value x"01".
      88 ver-nt-server             value x"02" x"03".
      88 ver-nt-domain-controller  value x"02".
   03 wEReserved                BYTE.

...

call WAPI "GetVersionExA" using VersionInfoEx

where DWORD, USHORT, WORD and BYTE are declared in windows.cpy. But I don't know how to make this call from managed COBOL. Could anyone help me ? Thnx.

  • Verified Answer

    You can still call Windows APis from managed code and you can still use TYPEDEFs such as WORD, DWORD etc but there is a problem when trying to use them as defined in windows.cpy which is not meant for use with managed code.

    I defined the required typedefs directly in the program and was able to make the call that you reference in your post. You do have to load the appropriate Windows .dll containing the API call. Please see the recent post which discussed this topic here:

    Example in managed code:

          $set ilpinvoke"kernel32"
           program-id.  testapi.
           environment division.
           special-names.
           call-convention 74 is WAPI.
    
           working-storage section.
           77 DWORD  pic 9(9) comp-5 typedef.
           77 USHORT pic 9(4) comp-5 typedef.
           77 WORD   pic 9(4) comp-5 typedef.
           77 #BYTE   pic 9(2) comp-5 typedef.
           01 VersionInfoEx.
              03 dwEVersionInfoSize        pic 9(9) comp-5.
              03 dwEMajorVersion           DWORD.
              03 dwEMinorVersion           DWORD.
              03 dwEBuildNumberFull.
                 05 dwEBuildNumber         USHORT.
                 05 dwEMajorAndMinor       USHORT.
              03 dwEPlatformId             DWORD.
              03 szECSDVersion             pic x(128).
              03 wEServicePackMajor        WORD.
              03 wEServicePackMinor        WORD.
              03 wESuiteMask               WORD.
              03 wEProduktType             #BYTE.
                 88 ver-nt-workstation        value h"01".
                 88 ver-nt-server             value h"02" h"03".
                 88 ver-nt-domain-controller  value h"02".
              03 wEReserved                #BYTE.
           procedure division.
              set dwEVersionInfoSize to length of VersionInfoEx
              call WAPI "GetVersionExA" using VersionInfoEx
              goback.
               
           
    
  • Actually, the problem with the Windows.cpy is that it is using a few data-names that are now COBOL reserved words.

    This can be remedied so that you can use Windows.cpy in managed code by removing these as reserved words using the remove directive.

    The following example will allow you to use windows.cpy in your .NET programs:

          $set ilpinvoke"kernel32"
          $if ilgen set
          $set remove"byte"
          $set remove"event"
          $set remove"internal"
          $set  change-message"1202 N"
          $set  change-message"1207 N"
          $set  change-message"1122 N"
          $end
           copy "windows.cpy".
          $if ilgen set
          $set  change-message"1202 W"
          $set  change-message"1207 W"
          $set  change-message"1122 W"
          $end
           
           program-id.  testapi.
           environment division.
           special-names.
           call-convention 74 is WAPI.
    
           working-storage section.
           01 VersionInfoEx.
              03 dwEVersionInfoSize        pic 9(9) comp-5.
              03 dwEMajorVersion           DWORD.
              03 dwEMinorVersion           DWORD.
              03 dwEBuildNumberFull.
                 05 dwEBuildNumber         USHORT.
                 05 dwEMajorAndMinor       USHORT.
              03 dwEPlatformId             DWORD.
              03 szECSDVersion             pic x(128).
              03 wEServicePackMajor        WORD.
              03 wEServicePackMinor        WORD.
              03 wESuiteMask               WORD.
              03 wEProduktType             BYTE.
                 88 ver-nt-workstation        value h"01".
                 88 ver-nt-server             value h"02" h"03".
                 88 ver-nt-domain-controller  value h"02".
              03 wEReserved                BYTE.
           procedure division.
              set dwEVersionInfoSize to length of VersionInfoEx
              call WAPI "GetVersionExA" using VersionInfoEx
              goback.