Delphi中Hook技术全接触(3)

2008-04-09 04:30:39来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


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.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:win32下的系统日志钩子示例程序(Delphi 版)

下一篇:*****查看技术