5,390
社区成员
发帖
与我相关
我的任务
分享
unit Sniffer;
interface
uses Windows;
function ExeSniffer( // 执行嗅探
AHandle: THandle; // 窗体句柄
AParam: Integer // 附加参数
): BOOL; stdcall;
implementation
uses SysUtils, Classes, Controls, StdCtrls, Messages;
var
WM_SNIFFWINDOW: Longword;
type
TSnifferInfo = packed record
rHOOK: HHOOK;
rHandle: HWND;
rParam: Integer;
end;
PSnifferInfo = ^TSnifferInfo;
var
vMapFile: THandle;
vSnifferInfo: PSnifferInfo;
var
ControlAtom: TAtom;
ControlAtomString: string = '';
RM_GetObjectInstance: DWORD; // registered window message
function FindControl(Handle: HWnd): TWinControl;
var
OwningProcess: DWORD;
begin
Result := nil;
if (Handle <> 0) and (GetWindowThreadProcessID(Handle, OwningProcess) <> 0) and
(OwningProcess = GetCurrentProcessId) then
begin
if GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom then
Result := Pointer(GetProp(Handle, MakeIntAtom(ControlAtom)))
else
Result := Pointer(SendMessage(Handle, RM_GetObjectInstance, 0, 0));
end;
end; { FindControl }
procedure ScanComponent(AComponent: TComponent);
var
I: Integer;
S: string;
vCopyDataStruct: TCopyDataStruct;
begin
if not Assigned(AComponent) then Exit;
S := '';
if SameText(AComponent.ClassName, 'TLabel') then
begin
S := Format('%s: %s', [AComponent.Name,
QuotedStr(TLabel(AComponent).Caption)]);
end else if SameText(AComponent.ClassName, 'TMemo')
or SameText(AComponent.ClassName, 'TEdit') then
begin
S := Format('%s: %s', [AComponent.Name,
QuotedStr(TLabel(AComponent).Caption)]);
end;
vCopyDataStruct.cbData := Length(S);
vCopyDataStruct.dwData := 0;
vCopyDataStruct.lpData := @S[1];
SendMessage(vSnifferInfo^.rParam, WM_COPYDATA,
vSnifferInfo^.rHandle, Integer(@vCopyDataStruct));
for I := 0 to AComponent.ComponentCount - 1 do
ScanComponent(AComponent.Components[I]);
end;
function SnifferProc(code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT; stdcall;
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;
ScanComponent(FindControl(vSnifferInfo^.rHandle));
end;
end;
end;
Result := CallNextHookEx(vSnifferInfo^.rHOOK, code, wParam, lParam);
end;
function ExeSniffer( // 执行嗅探
AHandle: THandle; // 窗体句柄
AParam: Integer // 附加参数
): BOOL; stdcall;
begin
vSnifferInfo^.rHandle := AHandle;
vSnifferInfo^.rParam := AParam;
vSnifferInfo^.rHOOK := SetWindowsHookEx(
WH_CALLWNDPROC, SnifferProc, HInstance, GetWindowThreadProcessId(AHandle));
Result := SendMessageTimeout(AHandle, WM_SNIFFWINDOW, 0, 0,
SMTO_NORMAL, 3000, THandle(Result)) = 0;
UnhookWindowsHookEx(vSnifferInfo^.rHOOK);
end;
initialization
WM_SNIFFWINDOW := RegisterWindowMessage('SnifferLib.SniffWindow');
vMapFile := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, '~Sniffer');
if vMapFile = 0 then
vMapFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0,
SizeOf(TSnifferInfo), '~Sniffer');
vSnifferInfo := MapViewOfFile(vMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
finalization
UnmapViewOfFile(vSnifferInfo);
CloseHandle(vMapFile);
end.