Try OpenEdge Now
skip to main content
Programming Interfaces
External Program Interfaces : Shared Library and DLL Support : Code samples : Examples of dynamic access to a shared library
 
Examples of dynamic access to a shared library
The following examples show several variations on the use of the call object handle attributes for dynamic access to a shared library.
This example demonstrates the use of the RETURN-VALUE-DLL-TYPE attribute and the RETURN-VALUE attribute when invoking a Windows DLL routine. RETURN-VALUE-DLL-TYPE is set to "LONG", which is the value that the DLL routine expects to receive, as shown:
FUNCTION GetWinVersion RETURNS INTEGER:
  DEFINE VARIABLE iValue  AS INTEGER   NO-UNDO.
  DEFINE VARIABLE libName AS CHARACTER NO-UNDO.
  DEFINE VARIABLE hCall   AS HANDLE    NO-UNDO.

  CREATE CALL hCall.
  ASSIGN
    hCall:CALL-NAME             = "GetVersion"
    hCall:LIBRARY               = "kernel32.dll"
    hCall:CALL-TYPE             = DLL-CALL-TYPE
    hCall:RETURN-VALUE-DLL-TYPE = "LONG".
  hCall:INVOKE( ).
  iValue = hCall:RETURN-VALUE.

  DELETE OBJECT hCall.
  RETURN iValue.
END FUNCTION.
After the invoke finishes executing, iValue contains an INTEGER value.
The following example implements an ABL function, sleep, which causes the AVM to sleep for a specified number of milliseconds. The code checks to determine which OS is running, and invokes the appropriate Windows DLL or UNIX shared library.
FUNCTION sleep (msecs AS INTEGER):
  DEFINE VARIABLE libName AS CHARACTER NO-UNDO
  DEFINE VARIABLE hCall   AS HANDLE    NO-UNDO.

  libName = IF OPSYS = "WIN32" THEN "kernel32.dll" ELSE "libc.so.1".

  CREATE CALL hCall.
  ASSIGN
    hCall:CALL-NAME      = "sleep"
    hCall:LIBRARY        = libName
    hCall:CALL-TYPE      = DLL-CALL-TYPE
    hCall:NUM-PARAMETERS = 1.

  hCall:SET-PARAMETER(1, "LONG", "INPUT", msecs).
  hCall:INVOKE( ).
  
  DELETE OBJECT hCall.
  RETURN msecs.
END FUNCTION.