7. Linking
There are two ways to link a DLL and import a function. Static linking is very easy and the recommended way if the DLL is certain to be available on the client's machine. If the DLL is optional it is better to use dynamic linking (runtime- LINKING) TO Make Sure That The Application DOS NOT FAIL JUST BECAUSE OF A MISSING (POSSIBLY UNIMPORTANT) DLL.
Static linking encodes the calling of the library DLL into the code as a direct call with no checking for inability to connect to the library. Dynamic linking calls the library DLL during the application's run-time and has the capability to handle problems with connection to the Library. Dynamic Linking Can Occur At Startup, or During The Program Run-Time As The Library Is Needed by The User.
Borland Usually Uses Static Linking, But a Few API Translations Use Dynamic Linking. One Example Is The Translation Of (Mapi.PAS).
Let's Look at the Two Ways of Linking A DLL AND IMPORTING A FUNCTION.
Back to Contents
7.1. Static lineing
IF Using Static Linking, Simply Declare The Function Prototype in The Interface Section and The Implementation The Following Way:
Interface
{Function | Procedure} FunctionName [FunctionDeclaration;]
IMPLEMentation
{Function | Procedure} FunctionName External Dllname [Name 'FunctionExportName "];
The translation of a function prototype was in the previous section. What about importing the function? It's not obligatory to include the parameter part of the function in the implementation section, but you can if you want. As an example to explain how to import a Function, Let's Use the OpenEvent Function from the Kernel32.dll.
There Are Two Implementations Of The Function, One with Unicode-Support (16bit-Widechar) And One with Ansi-Character (8bit Char) Support.The C-Declaration IS
WinBaseApi
Handle
WinAPI
Openeventa
DWORD DWDESIREDACCESS,
Bool binherithandle,
LPCSTR LPNAME
);
WinBaseApi
Handle
WinAPI
OpenEventw
DWORD DWDESIREDACCESS,
Bool binherithandle,
LPCWSTR LPNAME
);
#ifdef unicode
#define OpenEvent OpenEventw
#ELSE
#define OpenEvent OpenEventa
#ndif //! Unicode
Three function names are declared:. OpenEventA for the ansi-character version of the function, OpenEventW for the wide-character (unicode) version, and OpenEvent OpenEvent uses the widechar version of the function (OpenEventW) if the UNICODE symbol is declared or the Ansichar-Version OpenEventa if The Unicode Symbol is Not Declared.
Interface
{...}
Function OpenEventa (dwdesiredAccess: DWORD;
BinheritHandle: BOOL;
LPNAME: PANSICHAR): THANDLE; stdcall;
Function OpenEventw (dwdesiredAccess: DWORD)
BinheritHandle: BOOL;
LPNAME: PWIDECHAR): THANDLE; STDCALL;
{$ IFDEF Unicode}
Function OpenEvent (dwdesiredAccess: DWORD;
BinheritHandle: BOOL;
LPNAME: PWIDECHAR): THANDLE; STDCALL;
{$ Else}
Function OpenEvent (dwdesiredAccess: DWORD;
BinheritHandle: BOOL;
LPNAME: PCHAR): THANDLE; STDCALL;
{$ ENDIF}
{...}
IMPLEMentation
Const
KERNEL32 = 'kernel32.dll';
{...}
Function OpenEventa; External Kernel32 Name 'OpenEventa';
Function OpenEventw; External Kernel32 name 'OpenEventw';
{$ IFDEF Unicode}
Function OpenEvent; External Kernel32 name 'OpenEventw';
{$ Else}
Function OpenEvent; External Kernel32 Name 'OpenEventa';
{$ ENDIF}
{...}
Back to Contents