Delphi 정적 로드 DLL 및 동적 로드 DLL 예

18746 단어 Delphi
다음은 Delphi로 터치스크린 동적 라이브러리 xkutility를 호출합니다.DLL 및 DLL을 정적 로드하는 방법 및 DLL 동적 로드를 예로 들 수 있습니다.
코드를 직접 올리다.
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.

 

좋은 웹페이지 즐겨찾기