Delphi Static load DLL and dynamic load DLL example

Source: Internet
Author: User

The following is an example of using Delphi to invoke the touchscreen dynamic library Xtkutility.dll, which shows how to statically load DLLs and dynamically load DLLs.

directly on the code.

1. Static Loading Example

Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrl s;typeTForm1=Class (Tform) Btnenabletouch:tbutton;    Btndisenabletouch:tbutton;    Label1:tlabel;    Memo1:tmemo; procedureBtnenabletouchclick (Sender:tobject); procedureBtndisenabletouchclick (Sender:tobject); Private{Private Declarations} Public{Public Declarations}  End;{declaring a callback function type}typeXtouch_enum_callback_proc=function(Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall;functionEnumeratetouchinstance (Hwnd:thandle;pcontext:pointer;pcallback:xtouch_enum_callback_proc):D Word;stdcall; External'Xtkutility.dll';//function: Enumerate all touch devices in the systemfunctionCreateDevice (Szsymbolicname:pchar): thandle;stdcall;external'Xtkutility.dll';//turn on the touch devicefunctionCloseDevice (hfile:thandle): boolean;stdcall;external'Xtkutility.dll';//turn off the touch deviceprocedureEnabletouch (Hfile:thandle;benable:boolean); stdcall;external'Xtkutility.dll';//Touch Control benable is true when touch benable is allowed to false when touch is forbiddenfunctionDisenabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall;//declares a callback function that prohibits touching all touch devicesfunctionEnabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall;//declares a callback function that allows touching all touch devicesvarform1:tform1;implementation{$R *.DFM}functionDisenabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall;varHdevice:thandle;beginHdevice:=CreateDevice (szsymbloicname);  Enabletouch (Hdevice,false);  CloseDevice (Hdevice); Result:=True; //Displays the touch device identifier Form1.  Memo1.clear; FORM1.MEMO1.LINES.ADD (szsymbloicname);End;functionEnabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall;varHdevice:thandle;beginHdevice:=CreateDevice (szsymbloicname);  Enabletouch (hdevice,true);  CloseDevice (Hdevice); Result:=True; //Displays the touch device identifier Form1.  Memo1.clear; FORM1.MEMO1.LINES.ADD (szsymbloicname);End;procedureTform1.btnenabletouchclick (sender:tobject);varDwnumsofdevices:word;begindwnumsofdevices:= Enumeratetouchinstance (0,Nil, enabletouchscreencallback);End;procedureTform1.btndisenabletouchclick (sender:tobject);varDwnumsofdevices:word;begindwnumsofdevices:= Enumeratetouchinstance (0,Nil, disenabletouchscreencallback);End;End. 

2. Dynamic Loading Example

Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrl s;typeTForm1=Class (Tform) Btnenabletouch:tbutton;    Btndisenabletouch:tbutton;    Label1:tlabel;    Memo1:tmemo; procedureBtnenabletouchclick (Sender:tobject); procedureBtndisenabletouchclick (Sender:tobject); procedureFormclose (Sender:tobject;varaction:tcloseaction); procedureformcreate (Sender:tobject); Private{Private Declarations} Public{Public Declarations}  End;{declaring a callback function type}typeXtouch_enum_callback_proc=function(Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall; procedureLoaddll (Dllname:pchar); functionDisenabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall; //declares a callback function that prohibits touching all touch devicesfunctionEnabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall; //declares a callback function that allows touching all touch devices//Dynamic Call DLLtypetenumeratetouchinstance=function(Hwnd:thandle;pcontext:pointer;pcallback:xtouch_enum_callback_proc):D Word;stdcall; Tcreatedevice=function(Szsymbolicname:pchar): Thandle;stdcall; Tclosedevice=function(Hfile:thandle): Boolean;stdcall; Tenabletouch=procedure(Hfile:thandle;benable:boolean); stdcall;varForm1:tform1;  Dllhandle:thandle;  Enumeratetouchinstance:tenumeratetouchinstance;  Createdevice:tcreatedevice;  Closedevice:tclosedevice;  Enabletouch:tenabletouch; Implementation{$R *.DFM}procedureLoaddll (Dllname:pchar);beginTryifFileExists (DllName) Then    beginDllhandle:=LoadLibrary (DllName); ifDllhandle =0  Then      beginRaise Exception.create ('Load DLL file:'+ DllName +'failed! '); End      Else      beginenumeratetouchinstance:= GetProcAddress (Dllhandle,pchar ('enumeratetouchinstance')); if@EnumerateTouchInstance =Nil  ThenRaise Exception.create ('defining function Enumeratetouchinstance failed! '); CreateDevice:= GetProcAddress (Dllhandle,pchar ('CreateDevice')); if@CreateDevice =Nil  ThenRaise Exception.create ('defining function CreateDevice failed! '); CloseDevice:= GetProcAddress (Dllhandle,pchar ('CloseDevice')); if@CloseDevice =Nil  ThenRaise Exception.create ('defining function CloseDevice failed! '); Enabletouch:= GetProcAddress (Dllhandle,pchar ('Enabletouch')); if@EnableTouch =Nil  ThenRaise Exception.create ('defining function Enabletouch failed! '); End; End    Else    beginShowMessage (DllName+'does not exist! '); End; except on E:exception Do    beginShowMessage (e.message); End; End;End; functionDisenabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall;varHdevice:thandle;beginHdevice:=CreateDevice (szsymbloicname);  Enabletouch (Hdevice,false);  CloseDevice (Hdevice); Result:=True; //Displays the touch device identifier Form1.  Memo1.clear; FORM1.MEMO1.LINES.ADD (szsymbloicname);End;functionEnabletouchscreencallback (Pcontext:pointer;szsymbloicname:pchar;ntype:word): Boolean;stdcall;varHdevice:thandle;beginHdevice:=CreateDevice (szsymbloicname);  Enabletouch (hdevice,true);  CloseDevice (Hdevice); Result:=True; //Displays the touch device identifier Form1.  Memo1.clear; FORM1.MEMO1.LINES.ADD (szsymbloicname);End;procedureTform1.btnenabletouchclick (sender:tobject);varDwnumsofdevices:word;begin//enable all touch devices to touch dwnumsofdevices:= Enumeratetouchinstance (0,Nil, enabletouchscreencallback);End;procedureTform1.btndisenabletouchclick (sender:tobject);varDwnumsofdevices:word;begin//make all touch devices non-touchable dwnumsofdevices:= Enumeratetouchinstance (0,Nil, disenabletouchscreencallback);End;procedureTform1.formclose (Sender:tobject;varaction:tcloseaction);beginFreeLibrary (dllhandle);End;proceduretform1.formcreate (sender:tobject);vardllname:string;beginDllName:= Extractfilepath (Paramstr (0)) +'Xtkutility.dll'; Loaddll (PChar (DllName));End;End. 

Delphi Static load DLL and dynamic load DLL example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.