function SnifferProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT; stdcall;
var
vWinControl: TWinControl;
vCopyDataStruct: TCopyDataStruct;
I: Integer;
S: string;
begin
case code of
HC_ACTION:
begin
if PCWPStruct(lParam)^.message = WM_SNIFFWINDOW then
begin
if ControlAtomString = '' then
begin
ControlAtomString := Format('ControlOfs%.8X%.8X', [
GetWindowLong(vSnifferInfo^.rHandle, GWL_HINSTANCE),
GetWindowThreadProcessId(vSnifferInfo^.rHandle)]);
ControlAtom := GlobalAddAtom(PChar(ControlAtomString));
RM_GetObjectInstance := RegisterWindowMessage(PChar(ControlAtomString));
end;
vWinControl := FindControl(vSnifferInfo^.rHandle);
if Assigned(vWinControl) then
begin
for I := 0 to vWinControl.ComponentCount - 1 do
begin
S := '';
if SameText(vWinControl.Components[I].ClassName, 'TLabel') then
begin
TLabel(vWinControl.Components[I]).Visible:=false;
S := Format('%s: %s', [vWinControl.Components[I].Name,
QuotedStr(TLabel(vWinControl.Components[I]).Caption)]);
end else if SameText(vWinControl.Components[I].ClassName, 'TMemo')
or SameText(vWinControl.Components[I].ClassName, 'TEdit') then
begin
S := Format('%s: %s', [vWinControl.Components[I].Name,
QuotedStr(TLabel(vWinControl.Components[I]).Caption)]);
end;
vCopyDataStruct.cbData := Length(S);
vCopyDataStruct.dwData := 0;
vCopyDataStruct.lpData := @S[1];
SendMessage(vSnifferInfo^.rParam, WM_COPYDATA,
vSnifferInfo^.rHandle, Integer(@vCopyDataStruct));
end;
end;
end;
end;
end;
Result := CallNextHookEx(vSnifferInfo^.rHOOK, code, wParam, lParam);
end;
对,崩掉了N次了,做个简单的试验,就
function ExeSniffer( // 执行嗅探
AHandle: THandle; // 窗体句柄
AParam: Integer // 附加参数
): BOOL; stdcall;
begin
vSnifferInfo^.rHandle := AHandle;
vSnifferInfo^.rParam := AParam;
vSnifferInfo^.rHOOK := setwindowshookex(WH_CBT, @lpfn,hinstance,0);
end;
function lpfn(code:integer;wparam :integer;lparam:integer):LRESULT;stdcall;
var
msg:Tmsg;
vHandle: THandle;
begin
if(code=HCBT_CREATEWND)then
begin
if (FindWindow(nil, PChar(’窗体名称’))>0) and (not flag) then //HCBT_CREATEWND WM_KEYUP
begin
showmessage('ok') ;
flag:=true
end;
end;
Result := CallNextHookEx(VWindowHookInfo^.rhook, code, wParam, lParam);
end;
这样一个最简单的钩子过程都不成功!是DELPHI太烂了,还是技术太烂了。
type
TMainFrm = class(TForm)
btn_install: TButton;
btn_uninstall: TButton;
procedure btn_installClick(Sender: TObject);
procedure btn_uninstallClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainFrm: TMainFrm;
function InstHook32: Boolean; external 'h_calc.dll';
function UnInstHook32: Boolean; external 'h_calc.dll';
implementation
{$R *.dfm}
procedure TMainFrm.btn_installClick(Sender: TObject);
begin
if InstHook32 then
begin
btn_install.Enabled:= False;
btn_uninstall.Enabled:= True;
end;
end;
procedure TMainFrm.btn_uninstallClick(Sender: TObject);
begin
if UnInstHook32 then
begin
btn_install.Enabled:= True;
btn_uninstall.Enabled:= False;
end;
end;
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
function InstHook32: Boolean; stdcall; export;
function UnInstHook32: Boolean; stdcall; export;
implementation
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
hnd: HWND;
begin
if nCode = HSHELL_WINDOWCREATED then
begin
hnd:= FindWindow(nil, '计算器');
if hnd <> 0 then
begin
MessageBox(hnd, '计算器窗体已经运行', '提示', MB_OK + MB_ICONEXCLAMATION);
end;
end;
Result:= CallNextHookEx(hHk, nCode, WParam, LParam);
end;
function InstHook32: Boolean; stdcall; export;
begin
if hhk <> 0 then
begin
Result:= False;
Exit;
end;
hhk:= SetWindowsHookEx(WH_SHELL, @HookProc, HINSTANCE, 0);
Result:= hhk <> 0;
end;
function UnInstHook32: Boolean; stdcall; export;
begin
if hhk <> 0 then
begin
UnhookWindowshookEx(hhk);
hhk:= 0;
end;
Result:= hhk = 0;
end;
对,崩掉了N次了,做个简单的试验,就
function ExeSniffer( // 执行嗅探
AHandle: THandle; // 窗体句柄
AParam: Integer // 附加参数
): BOOL; stdcall;
begin
vSnifferInfo^.rHandle := AHandle;
vSnifferInfo^.rParam := AParam;
vSnifferInfo^.rHOOK := setwindowshookex(WH_CBT, @lpfn,hinstance,0);
end;
function lpfn(code:integer;wparam :integer;lparam:integer):LRESULT;stdcall;
var
msg:Tmsg;
vHandle: THandle;
begin
if(code=HCBT_CREATEWND)then
begin
if (FindWindow(nil, PChar(’窗体名称’))>0) and (not flag) then //HCBT_CREATEWND WM_KEYUP
begin
showmessage('ok') ;
flag:=true
end;
end;
Result := CallNextHookEx(VWindowHookInfo^.rhook, code, wParam, lParam);
end;
这样一个最简单的钩子过程都不成功!是DELPHI太烂了,还是技术太烂了。
type
TMainFrm = class(TForm)
btn_install: TButton;
btn_uninstall: TButton;
procedure btn_installClick(Sender: TObject);
procedure btn_uninstallClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainFrm: TMainFrm;
function InstHook32: Boolean; external 'h_calc.dll';
function UnInstHook32: Boolean; external 'h_calc.dll';
implementation
{$R *.dfm}
procedure TMainFrm.btn_installClick(Sender: TObject);
begin
if InstHook32 then
begin
btn_install.Enabled:= False;
btn_uninstall.Enabled:= True;
end;
end;
procedure TMainFrm.btn_uninstallClick(Sender: TObject);
begin
if UnInstHook32 then
begin
btn_install.Enabled:= True;
btn_uninstall.Enabled:= False;
end;
end;
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
function InstHook32: Boolean; stdcall; export;
function UnInstHook32: Boolean; stdcall; export;
implementation
function HookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
hnd: HWND;
begin
if nCode = HSHELL_WINDOWCREATED then
begin
hnd:= FindWindow(nil, '计算器');
if hnd <> 0 then
begin
MessageBox(hnd, '计算器窗体已经运行', '提示', MB_OK + MB_ICONEXCLAMATION);
end;
end;
Result:= CallNextHookEx(hHk, nCode, WParam, LParam);
end;
function InstHook32: Boolean; stdcall; export;
begin
if hhk <> 0 then
begin
Result:= False;
Exit;
end;
hhk:= SetWindowsHookEx(WH_SHELL, @HookProc, HINSTANCE, 0);
Result:= hhk <> 0;
end;
function UnInstHook32: Boolean; stdcall; export;
begin
if hhk <> 0 then
begin
UnhookWindowshookEx(hhk);
hhk:= 0;
end;
Result:= hhk = 0;
end;