Example - Translating C Calling Conventions

The following example shows how the Header-to-copy utility translates calling conventions in C source code to their equivalent COBOL copyfile form.

C source:

typedef HINSTANCE HMODULE;
DWORD _far _pascal GetVersion(void);
UINT _far _pascal GetFreeSystemResources(UINT);
#define GFSR_SYSTEMRESOURCES 0x0000
BOOL _far _pascal SetWinDebugInfo(const WINDEBUGINFO _far*
<_><_>lpwdi);
void _far _cdecl DebugOutput(UINT flags, LPCSTR lpsz, ...);
#define WDI_OPTIONS 0x0001

COBOL output:

 01  HMODULE    is typedef usage uns-int.
 end program "c-typedefs".
 program-id."c-typedefs" is external.
 special-names.
     call-convention pascal-convention-val is pascal-conv.
$set constant GetVersion "GetVersion"
 entry GetVersion pascal-conv
  returning   uns-long
     .
$set constant GetFreeSystemResources "GetFreeSystemResources"
 entry GetFreeSystemResources pascal-conv using
  by value     uns-int
  returning    uns-int
     .
 end program "c-typedefs".
 program-id."c-typedefs" is external.
 special-names.
     call-convention pascal-convention-val is pascal-conv.
 78  GFSR-SYSTEMRESOURCES     value h"0000".
 end program "c-typedefs".
 program-id."c-typedefs" is external.
 special-names.
     call-convention pascal-convention-val is pascal-conv.
$set constant SetWinDebugInfo "SetWinDebugInfo"
 entry SetWinDebugInfo pascal-conv using
  by reference windebuginfo
  returning    int
     .
 end program "c-typedefs".
 program-id."c-typedefs" is external.
 special-names.
     call-convention cdecl-convention-val is cdecl-conv.
$set constant DebugOutput "DebugOutput"
 entry DebugOutput cdecl-conv using
  by value     uns-int
  by reference any
  by value     any
     .
 end program "c-typedefs".
 program-id."c-typedefs" is external.
 special-names.
     call-convention cdecl-convention-val is cdecl-conv.
 78  WDI-OPTIONS    value h"0001".
 end program "c-typedefs".

H2cpy starts generating COBOL within the default external program c_typedefs which uses the default calling convention. The routine GetVersion uses the Pascal convention which is different, so H2cpy generates a header for a new external program containing a Procedure Division ENTRY statement. The routine GetFreeSystemResources uses the same calling convention so a second entry statement is generated with no need for a new program header. H2cpy then needs to generate a level-78 item in the Data Division, so it generates another program header. The routine DebugOutput uses the cdecl calling convention, so another program header is needed.