dll调用(二)

unit Unit5;
2
3 interface
4
5 uses
6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
8
9 type
10   TForm5 = class(TForm)
11     Button1: TButton;
12     Button2: TButton;
13     procedure Button1Click(Sender: TObject);
14     procedure Button2Click(Sender: TObject);
15   private
16     { Private declarations }
17   public
18     { Public declarations }
19   end;
20
21 var
22   Form5: TForm5;
23
24 implementation
25
26 {$R *.dfm}
27   {要先要定义和 DLL 中同样参数和返回值的的函数类型}
28 type
29   TDLLFun = function: Boolean; stdcall;
30   {现在需要的 DLL 中的函数的格式都是这样, 定义一个就够了}
31 var
32  h: HWND;                   {声明一个 DLL 句柄}
33  SetHook, DelHook: TDLLFun; {声明两个 TDLLFun 变量}
34
35
36 procedure TForm5.Button1Click(Sender: TObject);
37 var
38 b:Boolean;
39 begin
40   h := LoadLibrary('myHook.dll'); {载入 DLL 并获取句柄}
41   if h<>0 then
42   begin
43     SetHook := GetProcAddress(h, 'setHook'); {让 SetHook 指向 DLL 中相应的函数}
44     DelHook := GetProcAddress(h, 'deleteHook'); {让 DelHook 指向 DLL 中相应的函数}
45   end else ShowMessage('Err');
46   b:=SetHook; {执行钩子建立函数, 这里的 SetHook 和它指向的函数是同名的, 也可以不同名}
47 end;
48
49 procedure TForm5.Button2Click(Sender: TObject);
50 begin
51   DelHook;        {执行钩子释放函数}
52   FreeLibrary(h); {释放 DLL 资源}
53 end;
54
55 end.

posted @ 2012-08-06 12:37  随风--飘  阅读(120)  评论(0编辑  收藏  举报