Delphi 정적 로드 DLL 및 동적 로드 DLL 예
18746 단어 Delphi
코드를 직접 올리다.
1. 정적 로드 예
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btnEnableTouch: TButton;
btnDisEnableTouch: TButton;
Label1: TLabel;
Memo1: TMemo;
procedure btnEnableTouchClick(Sender: TObject);
procedure btnDisEnableTouchClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
{ }
type
XTOUCH_ENUM_CALLBACK_PROC = function(pContext:Pointer;szSymbloicName:PChar;nType:Word):Boolean;stdcall;
function EnumerateTouchInstance(hWnd:THandle;pContext:Pointer;pCallback:XTOUCH_ENUM_CALLBACK_PROC):DWORD;stdcall;external 'xtkutility.dll';
// :
function CreateDevice(szSymbolicName: PChar): THandle;stdcall;external 'xtkutility.dll';
//
function CloseDevice(hFile: THandle): Boolean;stdcall;external 'xtkutility.dll';
//
procedure EnableTouch(hFile: THandle;bEnable: Boolean);stdcall;external 'xtkutility.dll';
// bEnable true bEnable false
function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
// ,
function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
// ,
var
Form1: TForm1;
implementation
{$R *.dfm}
function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,False);
CloseDevice(hDevice);
Result := True;
//
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end;
function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,True);
CloseDevice(hDevice);
Result := True;
//
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end;
procedure TForm1.btnEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
dwNumsOfDevices := EnumerateTouchInstance(0, nil , EnableTouchscreenCallback);
end;
procedure TForm1.btnDisEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
dwNumsOfDevices := EnumerateTouchInstance(0, nil , DisEnableTouchscreenCallback);
end;
end.
2. 동적 로드 예
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btnEnableTouch: TButton;
btnDisEnableTouch: TButton;
Label1: TLabel;
Memo1: TMemo;
procedure btnEnableTouchClick(Sender: TObject);
procedure btnDisEnableTouchClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
{ }
type
XTOUCH_ENUM_CALLBACK_PROC = function(pContext:Pointer;szSymbloicName:PChar;nType:Word):Boolean;stdcall;
procedure loadDll(dllName: PChar);
function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
// ,
function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
// ,
// dll
type
TEnumerateTouchInstance = function(hWnd:THandle;pContext:Pointer;pCallback:XTOUCH_ENUM_CALLBACK_PROC):DWORD;stdcall;
TCreateDevice = function(szSymbolicName: PChar): THandle;stdcall;
TCloseDevice = function(hFile: THandle): Boolean;stdcall;
TEnableTouch = procedure(hFile: THandle;bEnable: Boolean);stdcall;
var
Form1: TForm1;
DllHandle: THandle;
EnumerateTouchInstance: TEnumerateTouchInstance;
CreateDevice: TCreateDevice;
CloseDevice: TCloseDevice;
EnableTouch: TEnableTouch;
implementation
{$R *.dfm}
procedure loadDll(DllName: PChar);
begin
try
if FileExists(DllName) then
begin
DllHandle := LoadLibrary(DllName);
if DllHandle = 0 then
begin
raise Exception.Create(' dll :' + DllName + ' !');
end
else
begin
EnumerateTouchInstance := GetProcAddress(DllHandle,PChar('EnumerateTouchInstance'));
if @EnumerateTouchInstance = nil then
raise Exception.Create(' EnumerateTouchInstance !');
CreateDevice := GetProcAddress(DllHandle,PChar('CreateDevice'));
if @CreateDevice = nil then
raise Exception.Create(' CreateDevice !');
CloseDevice := GetProcAddress(DllHandle,PChar('CloseDevice'));
if @CloseDevice = nil then
raise Exception.Create(' CloseDevice !');
EnableTouch := GetProcAddress(DllHandle,PChar('EnableTouch'));
if @EnableTouch = nil then
raise Exception.Create(' EnableTouch !');
end;
end
else
begin
ShowMessage(DllName + ' !');
end;
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
end;
function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,False);
CloseDevice(hDevice);
Result := True;
//
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end;
function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,True);
CloseDevice(hDevice);
Result := True;
//
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end;
procedure TForm1.btnEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
//
dwNumsOfDevices := EnumerateTouchInstance(0, nil , EnableTouchscreenCallback);
end;
procedure TForm1.btnDisEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
//
dwNumsOfDevices := EnumerateTouchInstance(0, nil , DisEnableTouchscreenCallback);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeLibrary(DllHandle);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
DllName: string;
begin
DllName := ExtractFilePath(ParamStr(0)) + 'xtkutility.dll';
loadDll(PChar(DllName));
end;
end.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Delphi] TStringBuilder그리고 꼭 사용해야만 할까? 그림처럼 Heap 영역에 "Hello" 공간을 생성하고 포인팅을 한다. "Hello World" 공간을 새로 생성한 후 포인팅을 하게 된다. 결국 "Hello" 라는 String 객체가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.