Kbase P44958: How to pass structure parameter to a DLL/Shared Library function
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  21/10/2008 |
|
Status: Verified
GOAL:
How to pass structure parameter to a DLL/Shared Library function
GOAL:
Is it possible to pass a structure to a dll function ?
GOAL:
Is there an equivalent to the C struct available in the ABL ?
FACT(s) (Environment):
Progress 9.x
OpenEdge 10.x
All Supported Operating Systems
FIX:
The Progress 4GL does not offer a direct equivalent to a C structure, so doing this directly is not possible.
However, if the definition and size of the structure is known it is possible to pass a MEMPTR that contains the required data in the expected locations:
- Define a MEMPTR and set the size to the total size of the structure.
- Use the various PUT-<datatype> statements to fill the MEMPTR by PUTting the data at the correct offset - the offsets would be calculated based on the definitions of the structure.
- Pass this MEMPTR to the DLL function.
- The DLL function should map the data correctly to the C structure.
Take note of the following:
1. Pointer values in a structure must match the bit-level of the process. On 32-bit platforms they should be treated as LONG values (32-bit integer), on 64-bit platforms as INT64 (64-bit integer).
The INT64 datatype in ABL is available from 10.1B onwards only. If accessing a 64-bit shared library is required, upgrading to 10.1B or later is highly recommended as being able to use the INT64 datatype removes important limitations on the GET-POINTER-VALUE and SET-POINTER-VALUE functions, as well as making pointer values passed between shared library calls more manageable.
2. The raw data in the MEMPTR must follow the proper byte alignment for the C struct. This byte alignment depends on the C-compiler, Operating System and CPU architecture used.
If the bytes in the MEMPTR are not properly aligned, the session will crash as soon as it's passed to the DLL/shared library
For these reasons, calls to DLL functions or Shared Libraries should always be considered to be platform-dependant.
Take care with pointers in the structure. You will have to pass the actual memory addresses as 32-bits (LONG) values. The memory address a 4GL MEMPTR points to can be retrieved with the GET-POINTER-VALUE function.