转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Delphi中Hook技术全接触         ★★★★

Delphi中Hook技术全接触

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2009 更新时间:2009/4/23 18:44:20
sult:=true;
 end;
end;

procedure Extro;
begin
 UnmapViewOfFile(Shared);
 CloseHandle(MemFile);
end;


function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall;
var k:integer;
begin
 Result:=DefWindowProc(hWnd,Msg,wParam,lParam);
 case Msg of
 wm_destroy:
   begin
     for k:=0 to 12 do SetHook(False,k);
     postmessage(findwindow(''''WinHook'''',nil),wm_destroy,0,0);
     ExitThread(0);
   end;
 end;
 if msg=wmhook then
 begin
   if wparam>0 then
   begin
     if sethook(true,wparam-1)=true then postmessage(findwindow(''''WinHook'''',nil),wmhook,wparam,0);
   end else
   begin
     if sethook(false,-wparam-1)=true then postmessage(findwindow(''''WinHook'''',nil),wmhook,wparam,0);
   end;
 end;
end;

procedure run;stdcall;
//var k:integer;
begin
 win.wClass.lpfnWndProc:=  @WindowProc;
 win.wClass.hInstance:=    hInstance;
 win.wClass.lpszClassName:=''''WideHook'''';
 RegisterClass(win.wClass);
 win.hmain:=CreateWindowEx(ws_ex_toolwindow,win.wClass.lpszClassName,''''WideHook'''',WS_CAPTION,0,0,1,1,0,0,hInstance,nil);
 FillChar(Shared^,SizeOf(TShared),0);
 shared^.self:=win.hmain;
 shared^.hinst:=hinstance;
 InitHookData;
 wmhook:=registerwindowmessage(pchar(''''wm_hook''''));
 while(GetMessage(win.Msg,win.hmain,0,0))do
 begin
   TranslateMessage(win.Msg);
   DispatchMessage(win.Msg);
 end;
end;

procedure DllEntryPoint(fdwReason:DWORD);
begin
 case fdwReason of
 DLL_PROCESS_DETACH:
   Extro;
 end;
end;

exports run;

begin
 //建立内存映象文件,用来保存全局变量
 MemFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HookMemFileName);
 Shared:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,0,0);
 DLLProc:=@DllEntryPoint;
end.

---------这是*.exe主程序---------------------------

Program WinHook;

uses windows,messages,sysutils;
{$r *.res}  //使用资源文件
const
 HTName:array[1..13] of pchar=(
 ''''CALLWNDPROC'''',''''CALLWNDPROCRET'''',''''CBT'''',''''DEBUG'''',''''GETMESSAGE'''',''''JOURNALPLAYBACK'''',
 ''''JOURNALRECORD'''',''''KEYBOARD'''',''''MOUSE'''',''''MSGFILTER'''',''''SHELL'''',''''SYSMSGFILTER'''',''''FOREGROUNDIDLE''''
 );
type
 TWin = record
   Msg:TMsg;
   wClass:TWndClass;
   hMain:integer;
   hbut,hlab:array[1..16] of integer;
   hLib:integer;
   HookStat:array[1..16] of bool;
 end;
var
 Win:TWin;                    //结构变量
 wmhook:integer;
 WorkPath:string;
 hRun:procedure;stdcall;
//
procedure runhookfun;
begin
 win.hlib:=loadlibrary(pchar(WorkPath+''''DemoHook.dll''''));
 if win.hlib=0 then messagebox(win.hmain,''''error'''','''''''',0);
 hrun:=GetProcAddress(win.hlib,''''run'''');
 if @hrun<>nil then hrun;
end;

procedure runhook;
var tid:integer;
begin
 createthread(nil,0,@runhookfun,nil,0,tid);
end;

function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall;
var k:integer;
begin
 case Msg of
 WM_SYSCOMMAND:
   begin
     case wparam of
     SC_CLOSE:
       begin
         if findwindow(''''WideHook'''',''''WideHook'''')<>0 then postmessage(findwindow(''''WideHook'''',''''WideHook''''),wm_destroy,0,0);
       end;//showwindow(hwnd,sw_hide);
     SC_MINIMIZE:;//showwindow(hwnd,sw_hide);
     SC_MAXIMIZE:;
     SC_DEFAULT:;
     SC_MOVE:;
     SC_SIZE:;
     //else
     //Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
     end;
     exit;
   end;
 wm_command:
   begin
     for k:=1 to 13 do
     begin
       if (lparam=win.hbut[k]) and ((k=6) or (k=7)) then break;
       if lparam=win.hbut[k] then
       begin
         if win.HookStat[k]=false then postmessage(findwindow(''''WideHook'''',''''WideHook''''),wmhook,k,0)
         else postmessage(findwindow(''''WideHook'''',''''WideHook''''),wmhook,-k,0);
       end;
     end;
   end;
 wm_destroy:
   begin
     freelibrary(win.hlib);
     halt;
   end;
 end;
 if msg=wmhook then
 begin
   if wparam>0 then
   begin
     setwindowtext(win.hbut[wparam],pchar(''''stop''''));
     win.HookStat[wparam]:=true;
   end else
   begin
     setwindowtext(win.hbut[-wparam],pchar(''''start''''));
     win.HookStat[-wparam]:=false;
   end;
 end;
 Result:=DefWindowProc(hWnd,Msg,wParam,lParam);
end;

//主程序的执行函数
procedure run;stdcall;
var k:integer;
begin
 if findwindow(''''WinHook'''',nil)<>0 then exit;
 win.wClass.hInstance:=    hInstance;
 with win.wclass do
 begin
   hIcon:=        LoadIcon(hInstance,''''MAINICON'''');
   hCursor:=      LoadCursor(0,IDC_ARROW);
   hbrBackground:= COLOR_BTNFACE+1;
   Style:=        CS_PARENTDC;
   lpfnWndProc:=  @WindowProc;
   lpszClassName:=''''WinHook'''';
 end;
 RegisterClass(win.wClass);
 win.hmain:=CreateWindow(win.wClass.lpszClassName,''''Delphi Hook Demo 2001'''',WS_VISIBLE or WS_OVERLAPPEDWINDOW,0,0,240,450,0,0,hInstance,nil);
 for k:=1 to 13 do
 begin
   win.hbut[k]:=CreateWindow(''''BUTTON'''',''''Start'''',WS_VISIBLE or WS_CHILD,10,10+30*(k-1),50,24,win.hmain,0,hInstance,nil);
   win.hlab[k]:=CreateWindow(''''STATIC'''',HTName[k],WS_VISIBLE or WS_CHILD,70,10+30*(k-1)+4,150,24,win.hmain,0,hInstance,nil);
   win.HookStat[k]:=false;
 end;
 WorkPath:=ExtractFilePath(ParamStr(0));
 runhook;
 wmhook:=registerwindowmessage(pchar(''''wm_hook''''));
 while(GetMessage(win.Msg,win.hmain,0,0)) do
 begin
   TranslateMessage(win.Msg);
   DispatchMessage(win.Msg);
 end;
end;

begin
 run;  //开始运行主程序
end.

上一页  [1] [2] 


[VB.NET程序]VB.net中HOOK的应用(二)  [VB.NET程序]VB.net中HOOK的应用(一)
[Delphi程序]浅谈API HOOK技术(二)  [Delphi程序]浅谈API HOOK技术(一)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台