Community in read only mode June 18 & 19
This community will be set in READ ONLY mode for a while on Tuesday June 18 into Wednesday June 19 while we import content and users from our Micro Focus Forums community site. MORE INFORMATION
alejanecz Absent Member.
Absent Member.
1518 views

Calling Windows API from managed COBOL

Jump to solution

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.

Tags (4)
0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

RE: Calling Windows API from managed COBOL

Jump to solution

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.
           
       
0 Likes
3 Replies
Micro Focus Expert
Micro Focus Expert

RE: Calling Windows API from managed COBOL

Jump to solution

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.
           
       
0 Likes
Micro Focus Expert
Micro Focus Expert

RE: Calling Windows API from managed COBOL

Jump to solution

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.
           
       
0 Likes
alejanecz Absent Member.
Absent Member.

RE: Calling Windows API from managed COBOL

Jump to solution

Thanks a lot. It does work)).

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.