- 1 year ago
COBOL - Calling the Windows API Getsysteminfo()
In COBOL, you can call Windows API functions using the CALL
statement with the WINAPI
or WINAPI-C
calling convention. To call the GetSystemInfo
function from the Windows API, you'll need to define the necessary data structures and function prototype in your COBOL program.
Here's an example of how to call GetSystemInfo
from COBOL:
cobolIDENTIFICATION DIVISION.
PROGRAM-ID. CallGetSystemInfo.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SystemInfoPtr POINTER.
01 SystemInfo UNSTRINGED.
05 dwPageSize DWORD.
05 lpMinimumApplicationAddress POINTER.
05 lpMaximumApplicationAddress POINTER.
05 dwActiveProcessorMask DWORD.
05 dwNumberOfProcessors DWORD.
05 dwProcessorType DWORD.
05 dwAllocationGranularity DWORD.
05 wProcessorLevel DWORD.
05 wProcessorRevision DWORD.
01 Kernel32 EXTERNAL.
05 LoadLibrary POINTER.
05 GetProcAddress POINTER.
01 GetSystemInfo EXTERNAL.
05 GetSystemInfoAddr POINTER.
01 CallConv PIC X(2) VALUE 'CD'.
01 ReturnCode S9(9) COMP VALUE 0.
PROCEDURE DIVISION.
MAIN-LOGIC.
CALL KERNEL32 "LoadLibrary" USING BY VALUE "kernel32.dll"
RETURNING LoadLibrary
IF LoadLibrary = NULL
DISPLAY "Error loading kernel32.dll"
STOP RUN
END-IF
CALL KERNEL32 "GetProcAddress" USING BY REFERENCE LoadLibrary
BY VALUE "GetSystemInfo"
RETURNING GetSystemInfoAddr
IF GetSystemInfoAddr = NULL
DISPLAY "Error getting address of GetSystemInfo"
STOP RUN
END-IF
MOVE GetSystemInfoAddr TO SystemInfoPtr
CALL WINAPI-C "GetSystemInfo" USING SystemInfoPtr
IF ReturnCode NOT = 0
DISPLAY "Error calling GetSystemInfo"
STOP RUN
END-IF
DISPLAY "Page Size: " dwPageSize
DISPLAY "Number of Processors: " dwNumberOfProcessors
DISPLAY "Processor Type: " dwProcessorType
CALL KERNEL32 "FreeLibrary" USING LoadLibrary
IF ReturnCode NOT = 0
DISPLAY "Error freeing kernel32.dll"
STOP RUN
END-IF
STOP RUN.
In this example, the GetSystemInfo
function returns various system information values, such as the page size, number of processors, and processor type. The LoadLibrary
and GetProcAddress
functions are used to dynamically load the kernel32.dll
library and get the address of the GetSystemInfo
function, respectively.
Please note that calling the Windows API from COBOL requires some familiarity with COBOL and Windows API data structures and conventions. Additionally, this example assumes that you are using a COBOL compiler that supports calling external functions using the WINAPI
or WINAPI-C
convention. The specific syntax and conventions may vary depending on the COBOL compiler you are using.