1,183
社区成员
发帖
与我相关
我的任务
分享
library QqHook;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Windows,
Messages,
CommCtrl;
var
hhook: Windows.HHOOK;
hwndTarget: HWND;
oldWndProc: Integer;
{$R *.res}
function WindowProc(hwnd: HWND; msg: UINT; wp: WPARAM; lp: LPARAM): LRESULT; stdcall;
begin
if (msg = WM_COMMAND) and (HIWORD(wp) = BN_CLICKED) and (LOWORD(wp) = $077E) then
begin
MessageBox(0, 'Is the message correct?', ':-)', MB_OK);
end;
if msg = WM_DESTROY then
begin
SetWindowLongA(hwnd, GWL_WNDPROC, Integer(oldWndProc));
hwndTarget := 0;
end;
result := CallWindowProc(TFNWndProc(oldWndProc), hwnd, msg, wp, lp);
end;
function QqHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): Integer; stdcall;
var
cwp: PCwpStruct;
hwnd: Windows.HWND;
hwndParent: Windows.HWND;
wndLong: DWORD;
found: Boolean;
textBuffer: Array[0..MAX_PATH] of AnsiChar;
fileName: PAnsiChar;
begin
Result := 0;
if (nCode < 0) then // or (nCode = HC_NOREMOVE)
begin
Result := CallNextHookEx(hhook, nCode, wParam, lParam);
exit;
end;
cwp := PCwpStruct(lParam);
if cwp^.message <> WM_CREATE then
begin
Result := CallNextHookEx(hhook, nCode, wParam, lParam);
exit;
end;
// 此处判断是不是 QQ 进程
found := false;
GetModuleFileNameA(0, textBuffer, MAX_PATH);
fileName := StrRScan(textBuffer, '\');
fileName := fileName + 1;
if AnsiStrIComp(fileName, 'QQ.exe') <> 0 then
begin
Result := CallNextHookEx(hhook, nCode, wParam, lParam);
exit;
end;
hwnd := cwp^.hwnd;
OutputDebugStringA('Is child window?');
// 首先判断是不是个子窗口
wndLong := GetWindowLongA(hwnd, GWL_STYLE);
if (wndLong and WS_CHILD) = WS_CHILD then
begin
OutputDebugStringA(' Yes');
OutputDebugStringA('Is parent window a dialog?');
// 其次判断其父窗口是不是一个对话框
hwndParent := GetParent(hwnd);
GetClassNameA(hwndParent, @textBuffer, 80);
if AnsiStrIComp(textBuffer, '#32770') = 0 then
begin
OutputDebugStringA(' Yes');
OutputDebugStringA('Is its ID 0x077E?');
// 再次判断其 ID 是不是 0x077E,这是 QQ 2008 对话窗口中发送按钮的 ID 值
wndLong := GetWindowLongA(hwnd, GWL_ID);
if wndLong = $0000077E then
begin
OutputDebugStringA(' Yes');
// 注意:子类化父窗口
hwndTarget := hwndParent;
oldWndProc := SetWindowLongA(hwndTarget, GWL_WNDPROC, Integer(@WindowProc));
found := true;
OutputDebugStringA('The target window found!');
end
else
OutputDebugStringA(' No');
end
else
OutputDebugStringA(' No');
end
else
OutputDebugStringA(' No');
if not found then
begin
Result := CallNextHookEx(hhook, nCode, wParam, lParam);
exit;
end;
end;
procedure Hook(bEnable: Boolean); stdcall; export;
begin
if bEnable then
begin
if hhook = 0 then
begin
hhook := SetWindowsHookEx(WH_CALLWNDPROC, QqHookProc, HInstance, 0);
end;
end
else
begin
if hhook <> 0 then
begin
UnhookWindowsHookEx(hhook);
hhook := 0;
end;
end;
end;
procedure DllEntry(reason: Integer);
begin
if reason = DLL_PROCESS_DETACH then
begin
if (oldWndProc <> 0) and (hwndTarget <> 0) then
begin
SetWindowLongA(hwndTarget, GWL_WNDPROC, oldWndProc);
end;
end;
end;
exports
Hook;
begin
hhook := 0;
hwndTarget := 0;
oldWndProc := 0;
DllProc := DllEntry;
end.