关于拦截messagebox创建消息的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 太阳火
  • 开始时间 开始时间

太阳火

Unregistered / Unconfirmed
GUEST, unregistred user!
我是想拦截WM_INITDIALOG消息,得到messagebox的句柄,可是下面的代码总是出错,access violation.<br><br>unit myunit;<br><br>interface<br><br>implementation<br><br>uses<br> &nbsp;Windows, Messages,<br><br>var<br> &nbsp;gHook: Hwnd;<br> &nbsp;oldProc: Pointer;<br><br>function HookWndProc(AHwnd: Hwnd; uMsg: Cardinal; wP: WParam; lP: LParam): LRESULT;<br>begin<br> &nbsp;Result := CallWindowProc(oldProc, AHwnd, uMsg, wP, lP);<br> &nbsp;if uMsg = WM_INITDIALOG then<br> &nbsp;begin<br> &nbsp; &nbsp;//取得了messagebox的hwnd, 做我的事情<br> &nbsp;end;<br>end;<br><br>function SetHook(nCode: integer; wP: WParam; lP: LParam): LRESULT;<br>begin<br> &nbsp;if (nCode = HC_ACTION) and (PCwpStruct(lP).message = WM_INITDIALOG) then<br> &nbsp;begin<br> &nbsp; &nbsp;oldProc := Pointer(GetWindowLong(PCwpStruct(lP).hwnd, GWL_WNDPROC));<br> &nbsp; &nbsp;SetWindowLong(PCwpStruct(lP).hwnd, GWL_WNDPROC, Longint(@HookWndProc));<br> &nbsp;end;<br><br> &nbsp;Result := CallNextHookEx(gHook, nCode, wP, lP);<br>end;<br><br>initialization<br><br>gHook := SetWindowsHookEx(WH_CALLWNDPROC, @SetHook, 0, GetCurrentThreadId);<br><br>finalization<br><br>UnhookWindowsHookEx(gHook);<br><br>end.
 
我是想拦截WM_INITDIALOG消息,得到messagebox的句柄,可是下面的代码总是出错,access violation.<br><br>unit myunit;<br><br>interface<br><br>implementation<br><br>uses<br> &nbsp;Windows, Messages,<br><br>var<br> &nbsp;gHook: Hwnd;<br> &nbsp;oldProc: Pointer;<br><br>function HookWndProc(AHwnd: Hwnd; uMsg: Cardinal; wP: WParam; lP: LParam): LRESULT;<br>begin<br> &nbsp;Result := CallWindowProc(oldProc, AHwnd, uMsg, wP, lP);<br> &nbsp;if uMsg = WM_INITDIALOG then<br> &nbsp;begin<br> &nbsp; &nbsp;//取得了messagebox的hwnd, 做我的事情<br> &nbsp;end;<br>end;<br><br>function SetHook(nCode: integer; wP: WParam; lP: LParam): LRESULT;<br>begin<br> &nbsp;if (nCode = HC_ACTION) and (PCwpStruct(lP).message = WM_INITDIALOG) then<br> &nbsp;begin<br> &nbsp; &nbsp;oldProc := Pointer(GetWindowLong(PCwpStruct(lP).hwnd, GWL_WNDPROC));<br> &nbsp; &nbsp;SetWindowLong(PCwpStruct(lP).hwnd, GWL_WNDPROC, Longint(@HookWndProc));<br> &nbsp;end;<br><br> &nbsp;Result := CallNextHookEx(gHook, nCode, wP, lP);<br>end;<br><br>initialization<br><br>gHook := SetWindowsHookEx(WH_CALLWNDPROC, @SetHook, 0, GetCurrentThreadId);<br><br>finalization<br><br>UnhookWindowsHookEx(gHook);<br><br>end.
 
自己重写 messagebox 的代码嘛
 
var<br> &nbsp;FormHook: HHOOK;<br><br>function CBTProc(Code: Integer; wParam, lParam: longint): longint; stdcall;<br>begin<br> &nbsp;if Code &lt; 0 then CallNextHookEx(0, Code, wParam, lParam);<br> &nbsp;case Code of<br> &nbsp; &nbsp;HCBT_CREATEWND:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Form1.Caption:='对话框句柄:'+inttohex(wParam,2)+' &nbsp; '+'对话框标题:'+TCBTCreateWnd(Pointer(lParam)^).lpcs.lpszName;<br> &nbsp; &nbsp; &nbsp; &nbsp;UnHookWindowsHookEx(FormHook);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;Result := CallNextHookEx(0, Code, wParam, lParam);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;FormHook := SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadID);<br> &nbsp;MessageBox(Self.Handle, 'AAAAAAAAAAAAAAAAAAAAAAA', 'BBBBBBBBBBBBBBBBBBBBB', 64);<br>end;<br><br><br>给分吧,我也穷的可怜呀!
 
后退
顶部