M
movax
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Setlong;<br><br>interface<br><br>uses<br> SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br> Forms, Dialogs, Buttons, StdCtrls, ExtCtrls;<br>const<br> wTestMessage=WM_USER+200;<br><br>type<br> TInspecterform = class(TForm)<br> Panel1: TPanel;<br> Label1: TLabel;<br> edtn_Targetwindow: TEdit;<br> spb_Locate: TSpeedButton;<br> lbl_TargetHwnd: TLabel;<br> grb_Inspecter: TGroupBox;<br> Mmo_Inspecter: TMemo;<br> spb_Setproc: TSpeedButton;<br> spb_sendTestmessage: TSpeedButton;<br> spb_Unhook: TSpeedButton;<br> lbl_WndProcAddress: TLabel;<br> procedure spb_LocateClick(Sender: TObject);<br> procedure spb_SetprocClick(Sender: TObject);<br> procedure spb_sendTestmessageClick(Sender: TObject);<br> private<br> { Private declarations }<br> procedure Wndproc(var Msg :TMessage);override;<br> public<br> { Public declarations }<br> end;<br>var<br> Inspecterform: TInspecterform;<br> TargetHwnd :Integer;<br> TargetWindowproc ointer;<br>implementation<br>{$R *.DFM}<br><br>procedure TInspecterform.Wndproc(var Msg:TMessage);<br>begin<br> if Msg.Msg = wTestMessage then<br> ShowMessage('i got it');<br> inherited Wndproc(Msg);<br>end;<br><br>function InspecterWndProc(Handle:Hwnd;Msg,Wparam:Word;Lparam:LongInt):LongInt;<br>begin<br>{后来,这里我什么都不写,直接把消息传给被监控程序的wndproc,不过还是会报错}<br>Result := CallWindowproc(TargetWindowProc,TargetHwnd,Msg,Wparam,lparam);<br>end;<br><br>procedure TInspecterform.spb_LocateClick(Sender: TObject);<br>begin<br>{TargetHwnd是被监控程序的Hwnd}<br>TargetHwnd := FindWindow('TmainForm','mainform');<br>if TargetHwnd =0 then begin<br> ShowMessage('Can not Find the Target Window');<br> Exit;<br>end<br>else<br> lbl_TargetHwnd.Caption := 'TargetWindow Handle is : '+IntToHEX(TargetHwnd,4);<br>end;<br><br>procedure TInspecterform.spb_SetprocClick(Sender: TObject);<br>begin<br>TargetWindowproc := Pointer(SetWindowLong(TargetHwnd,GWL_WNDPROC,Longint(@InspecterWndProc)));<br>end;<br>procedure TInspecterform.spb_sendTestmessageClick(Sender: TObject);<br>begin<br> PostMessage(TargetHwnd,wTestMessage,0,0);<br>end;<br><br>end.<br>这段程序是在win31运行的,用来监控系统发给某个程序的所有消息的。<br>inspecterform是监控程序,mainform是被监控的。<br>用到了setwindowslong,和callwindowproc这两个api函数。<br><br>我能正确的得到mainform的wndproc的地址。但是如果maiform一响应消息,<br>win31就报genernal protect错误。<br><br><br>想了好久,还是解决不了。请大家帮忙。