7.2. Dynamic Linking
Dynamic Linking Is Used to Link The DLL At Runtime.
Handling Static and Dynamic Linking In The Jedi Environment
BECASE Both Static and Dynamic Linking Have The The Jedi Units. However, static linking is the default.
To Support Multiple Methods Compiler Symbols Are Used. The Two Compiler Symbols That Symbolize Dynamic Linking Area Follows:
IF xxxx_dynlink is defined The DLL Must Be Linked Dynamically at Startup (Initialization Section).
IF xxxx_linkonrequest is defined The DLL IS Linked Dynamically, Not At Startup, But asked by the user. Xxxx is the name of the API, E.G. Mapi_Dynlink or mapi_linkonRequest.
If Neither Symbol is defined, static linking is used.
Each Import Unit SHOULD IMPLEMENT The FOLLOWING FUNCTIONS:
Function XXXXINITAPI: Boolean;
Procedure xxxxfreeApi;
Function XXXXCHECKAPI: Boolean;
The functions XxxxInitAPI and XxxxFreeAPI are available for the user if the symbol Xxxx_LINKONREQUEST is defined. The User can call this function to load or free the DLL. XxxxInitAPI returns TRUE, if the DLL has been loaded successfully.
IF xxxx_dynlink is defined, but not xxxx_linkonrequest, these Functions Are Used INTERLY TO LOAD OROAD The DLL IN THE INITIZATION Section, But The User.
The function XxxxCheckAPI returnes TRUE if the API is available, so the return value is TRUE if the DLL has previously been loaded successfully using XxxxInitAPI. When static linking is used, the return value is always TRUE.
Here is an example of how to handle the symbols in a translation:
Unit Apisample;
Interface
Uses Windows,
Jediutil;
// there all dynamic //r Run Time Linking with Linking On Startup, The Other
// to link Upon Request. in Fact The Only Difference IS
// That the library is not loaded in the initialization
// section if the Upon-Request Symbol is defined.
// Both Symbols Start with the name of the API, E.g. TAPI.
// in this Example IT IS Apisample.
// Apisample_Dynlink signals what the API SHOULD BE Linked
// via loadLibrary and getProcaddress at Startup.
// Apisample_linkonRequest Signals That The libary shop
// NOT BE Linked At Startup, But Via An Initialization Procedure.
// Since Most of The Stuff Is The Same for Both WE Use
// Apisample_Dynlink as Common Symbol.
{$ IFDEF APISample_LinkonRequest}
{$ Define apisample_dynlink}
{$ ENDIF}
{$ IFDEF APISample_Dynlink}
// define function type and variables for Dynamic Linking
Type
Tapisamplefunc1 = function (lParam: longint): longint; stdcall;
Tapisamplefunc2 = function (wparam: word): longint; stdcall;
VAR
Apisamplefunc1: Tapisamplefunc1 = NIL;
Apisamplefunc2: TapisampleFunc2 = NIL;
{$ Else}
// We don't use Dynamic Linking So Weimplement Static Linking
Function Apisamplefunc1 (LPARAM: LongInt): longint; stdcall;
Function Apisamplefunc2 (WPARAM: WORD): longint; stdcall;
{$ ENDIF}
//
// linking control functions
//
// the xxxinitapi function backs
// naming convention and is only visible if
// xxxxx_linkonRequest is defined. The Same is True
// for the xxxxxfreeApi function which frees the
// Library
{$ IFDEF APISample_LinkonRequest}
Function Apisampleinitapi: Boolean;
Procedure apisamplefreeApi;
{$ ENDIF}
// the xxxxxcheckapi function returns true if
// The API is Available. with static linking // The function always returns true
Function ApisampleCheckapi: Boolean;
IMPLEMENTATION
Const apisampledll = 'apisample.dll'; // name of the dll
{$ IFDEF APISample_Dynlink}
Var HDLL: THANDE = 0; // Handle to the lib. Only Req. For
Dyn.link.
Function Apisampleinitapi: Boolean;
Begin
Result: = FALSE;
// load library if nesery
IF HDLL = 0 Then HDLL: = loadingLibrary (Apisampledll);
IF JedicheckInstanceHandle (HDLL) THEN
Begin
// set Pointers to Functions
@ Apisamplefunc1: = getProcaddress (HDLL, 'Apisamplefunc1');
@ Apisamplefunc2: = getProcaddress (HDLL, 'Apisamplefunc2');
// everything ok, return true
RESULT: = TRUE;
end
END;
Procedure apisamplefreeApi;
Begin
IF HDLL <> 0 THEN
Freelibrary (HDLL);
HDLL: = 0;
END;
{$ Else}
Function Apisamplefunc1; External Apisampledll Name 'Apisamplefunc1';
Function Apisamplefunc2; External Apisampledll Name 'Apisamplefunc1';
{$ ENDIF}
Function ApisampleCheckapi: Boolean;
Begin
{$ IFDEF APISample_Dynlink}
Result: = HDLL <> 0;
{$ Else}
RESULT: = true;
{$ ENDIF}
END;
INITIALIZATION
Begin
{$ IFDEF APISample_Dynlink}
{$ IFNDEF APISAMPLE_LINKONREQUEST}
// Call init if Dynamic Linking and not link on Request
Apisampleinitapi
{$ ENDIF}
{$ ENDIF}
END;
Finalization
Begin
{$ IFDEF APISample_Dynlink}
Apisampleinitapi; // Call Free IF Dynamic Linking
{$ ENDIF}
END;
End.
You can use the the the jedicheckinstancehandle function from the commitary.
Back to Contents
8. The Jedi Common Support Unit
Related resources: Delphi programmer's gospel - C header translator