看流星社区

 找回密码
 注册账号
查看: 2773|回复: 0

[Delphi] HOOK DLL注入实例,让新手获得新生

[复制链接]

该用户从未签到

发表于 2011-3-31 08:48:03 | 显示全部楼层 |阅读模式
声明:本贴是转的,例子不错,适合新手。找了好长时间才找到的。


  1. Hook32.dpr 源代码:  
  2. library Hook32;  

  3. uses  
  4. SysUtils,  
  5. Forms,  
  6. Classes,  
  7. myDLl in 'myDLl.pas' {Form1};  


  8. {$R *.res}  

  9. exports  
  10. HookOn,HookOff;  

  11. begin  
  12. {Application.Initialize;  
  13. Application.Run; }  
  14. end.  

  15. 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》  
  16. myDLl.pas 源代码:  
  17. unit myDLl;  

  18. interface  

  19. uses  
  20. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  21. Dialogs, StdCtrls;  

  22. type  
  23. TForm1 = class(TForm)  
  24. Memo1: TMemo;  

  25. private  
  26. { Private declarations }  
  27. public  
  28. { Public declarations }  
  29. end;  

  30. var  
  31. Form1: TForm1;  
  32. function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;  
  33. function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;export;  
  34. function HookOff:Boolean;stdcall;export;  

  35. implementation  

  36. var  
  37. hHk: HHOOK=0;  
  38. mhwnd:HWND=0;  
  39. bShow:Integer=1;  
  40. hThread: Cardinal;  
  41. hmod: Pointer; //Hinstance  
  42. hProcessId: Cardinal;  
  43. mMode:Integer;  

  44. {$R *.dfm}  

  45. function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;  
  46. begin  
  47. //接收按键F8开始挂钩DLL  
  48. if (bShow=1) And (wParam=VK_F8) then  
  49. begin  
  50. bShow:=5;  
  51. Form1:=TForm1.Create(Application);  
  52. Form1.Show;  
  53. ShowCursor(true);  
  54. end;  
  55. if (bShow=2) And (wParam=VK_F8) then  
  56. begin  
  57. bShow:=6;  
  58. Form1.Free;  
  59. end;  

  60. if (bShow=3) And (wParam=VK_F8) then  
  61. begin  
  62. bShow:=2;  
  63. end;  

  64. if (bShow=4) And (wParam=VK_F8) then  
  65. begin  
  66. bShow:=1;  
  67. end;  
  68. if (bShow=5) then bShow:=3;  
  69. if (bShow=6) then bShow:=4;  
  70. Result := CallNextHookEx(hHk,nCode,WParam,LParam);  
  71. end;  

  72. function HookOn(lpHwnd:HWND;lpType:Longint): Longint;stdcall; export;  
  73. begin  
  74. hThread :=GetWindowThreadProcessId(lpHwnd,hmod);  
  75. //注入开始  
  76. if lpHwnd<>0 then hHk :=SetWindowsHookEx(lpType,@HookProc,hInstance,hThread); // WH_KEYBOARD  
  77. Result :=hHk  
  78. end;  

  79. function HookOff:Boolean;stdcall; export;  
  80. begin  
  81. if hHk<>0 then  
  82. begin  
  83. //移除挂钩  
  84. UnHookWindowsHookEx(hHk);  
  85. hHk :=0;  
  86. Result :=true;  
  87. end  
  88. else  
  89. Result :=false;  
  90. end;  

  91. end.  

  92. 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》  
  93. 调用DLL的执行文件Unit1.pas源代码:  
  94. unit Unit1;  

  95. interface  

  96. uses  
  97. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  98. Dialogs, StdCtrls, ExtCtrls;  

  99. type  
  100. TForm1 = class(TForm)  
  101. Button1: TButton;  
  102. Button2: TButton;  
  103. procedure FormClose(Sender: TObject; var Action: TCloseAction);  
  104. procedure Button1Click(Sender: TObject);  
  105. procedure Button2Click(Sender: TObject);  

  106. private  
  107. { Private declarations }  
  108. public  
  109. { Public declarations }  
  110. end;  

  111. var  
  112. Form1: TForm1;  
  113. function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;external 'Hook32.dll' name 'HookOn';  
  114. function HookOff:Boolean;stdcall;external 'Hook32.dll' name 'HookOff';  
  115. implementation  

  116. {$R *.dfm}  



  117. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);  
  118. begin  
  119. hookoff;  
  120. end;  

  121. procedure TForm1.Button1Click(Sender: TObject);  
  122. var  

  123. h1:HWND;  
  124. begin  

  125. h1:=FindWindow(NIL,'无标题 - 记事本');//这是窗口的句柄,要自己找到后,填写入。  
  126. if h1=0 then showmessage('没找到进程!');  
  127. if h1>0 then showmessage('找到进程!');  
  128. sleep(2000);  
  129. HookOn(h1,WH_KEYBOARD);  


  130. end;  

  131. procedure TForm1.Button2Click(Sender: TObject);  
  132. begin  
  133. HookOff;  
  134. end;  

  135. end.
复制代码
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-28 19:08

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表