Scalabium Software |
|
| Knowledge for your independence'. | |
#130: How can I use a procedure from dll in the Delphi? |
|
Today I want to describe a method of dll-using. Of course, for some advanced users this tip is not useful but I hope that somebody will find it as interested. In Delphi you have two mechanism of dll-loading: static and dynamic. - the static method is when your application will be failed when dll is not present. Inside of application you'll define a function as external and in any code you'll use a function from dll as usual "native" function. - the dynamic method allow to have a control under dll-load.
For example, you can check an dll-existing and show a warning if
dll is not exist (or try to load another dll, for example). Also
to call a some function from such Small example. In static method you must declare a function as: procedure ExecScript(UserID: Integer; ScriptName: PChar); stdcall; far; external 'yourDLLName.DLL'; and in the code you can call it as: and it's all. For dynamic mechanism you must: {define a procedure type with required parameters of your procedure in the DLL}
type
TDLLFunc = procedure(param1: Integer; param2: PChar);
{assign a nil - not loaded function}
const
DLLFunc: TDLLFunc = nil;
{handle of loaded dll}
var
DLLHandle: THandle;
{ load a library }
DLLHandle := LoadLibrary(DLLName);
if (DLLHandle < HINSTANCE_ERROR) then
raise Exception.Create(DLLName + ' library can not be loaded or not found. ' + SysErrorMessage(GetLastError));
try
{ load an address of required procedure}
@DLLFunc := GetProcAddress(DLLHandle, 'ExecScript');
{if procedure is found in the dll}
if Assigned(DLLFunc) then
DLLFunc(5, 'LogToSystem');
finally
{unload a library}
FreeLibrary(DLLHandle);
end;
|
|
|
Copyright© 1998-2025, Scalabium
Software. All rights reserved. |