如何处理消息? ( 积分: 80 )

  • 主题发起人 主题发起人 wzwcn
  • 开始时间 开始时间
W

wzwcn

Unregistered / Unconfirmed
GUEST, unregistred user!
知道一个外部程序按扭的句柄,如何知道它已经按下?<br>(最好能举例,谢谢。)
 
知道一个外部程序按扭的句柄,如何知道它已经按下?<br>(最好能举例,谢谢。)
 
subclass 啊 ...
 
http://dev.csdn.net/develop/article/21/21439.shtm<br><br>用跨进程子类化技术实现对其它进程消息的拦载 &nbsp; &nbsp; 选择自 pankun 的 Blog &nbsp;<br>关键字 &nbsp; 跨进程,子类化,消息监视 <br>出处 &nbsp; &nbsp;<br> <br> &nbsp; 大家都知道每个窗口都有默认的窗口函数来进行对窗口消息的处理.<br> &nbsp;而子类化技术就是替换窗口的窗口函数为自己定义的函数的技术.例如下面的代码:<br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;OldWndProc: Pointer;<br>implementation<br><br>{$R *.dfm}<br>function NewWndProc(hHwnd, Msg, wParam, lParam: LongWORD): Longint; stdcall;<br>begin<br> &nbsp;if Msg=WM_CLOSE then<br> &nbsp; &nbsp;exit;<br> &nbsp;Result := CallWindowProc(OldWndProc, hHwnd, Msg, wParam, lParam);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> &nbsp;{保存旧的窗口函数地址}<br> &nbsp;OldWndProc := Pointer(GetWindowLong(Self.Handle, GWL_WNDPROC));<br> &nbsp;{设置新的窗口函数为自定义函数}<br> &nbsp;SetWindowLong(Self.Handle, GWL_WNDPROC, Longint(@NewWndProc));<br>end;<br> &nbsp;这样在窗口建立时就对窗口实现了子类化,这时按下窗口的关闭按钮就会发现关不了窗口,因为新的窗口处理函数把WM_CLOSE消息过滤掉了,要取消子类化,只需要简单的把以前的窗口函数恢复过来就可以了.SetWindowLong(Self.Handle, GWL_WNDPROC, Longint(OldWndProc));<br><br> &nbsp;现在看来似乎很简单,只要对其它进程中的目标窗口进行子类化就可以实现对其消息的拦载监视了.但是在WIN32下,每一个进程都有自己独立的内存空间,新的窗口函数必须和目标窗口在同一个进程内,直接使用SetWindowLong(其它进程中窗口的句柄, GWL_WNDPROC, 新窗口函数)就会失败,所以就要想办法把我们的窗口函数代码放到目标进程内,这儿有二个办法,一是使用CreateRemoteThread在目标进程内建立线程,但这函数只在NT及以上操作系统实现,而且还要涉及到API地址重定位等问题,很麻烦(请参考http://www.csdn.net/develop/Read_Article.asp?Id=21079).另一个方法就是使用HOOK技术(SetWindowsHookEx,如果不知道,请先参考HOOK技术方面的文章),大家都知道,对其它进程进行HOOK时,此进程会自动加载HOOK过程所在的DLL,如果我们把窗口函数也放在DLL中,那窗口函数就相当于加载到了目标进程的地址空间中了,这方法简单易行.在这里我们就采用HOOK技术来实现跨进程子类化.<br><br> &nbsp;最后一个问题是如何在DLL中实现全局变量,因为DLL中的变量在每个进程加载这个DLL时都申请新的空间来存放变量,所以DLL中的变量在各个进程内不一样,可以利用内存文件映射,WM_COPYDATA等方法来实现全局变量.这儿采用内存文件映射.<br><br> &nbsp;现在需要的知识都已了解了,就让我们来看具体的代码吧(这儿是把所有函数放在一个DLL中):<br>library Hook;<br><br>uses<br> &nbsp;SysUtils,windows, Messages;<br><br>const<br> &nbsp;WM_UNSUBCLASS = WM_USER + 1001; &nbsp;{卸载子类化消息}<br> &nbsp;WM_NEWMESSAGE = WM_USER + 1002; &nbsp;{通知监视窗口拦到了新消息}<br> &nbsp;HOOK_EVENT_NAME = 'MyHook';<br><br>type<br> &nbsp;PMyDLLVar = ^TMyDLLVar;<br> &nbsp;TMyDLLVar = record<br> &nbsp; &nbsp;SubClass: Boolean; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {是否已经子类化}<br> &nbsp; &nbsp;HookWindow, SpyWindow: LongWORD; &nbsp; {要安装HOOK的窗口及用于接收消息的窗口}<br> &nbsp; &nbsp;hHook: LongWORD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {HOOK句柄}<br> &nbsp; &nbsp;OldWndProc: pointer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {旧的窗口过程}<br> &nbsp; &nbsp;MsgHwnd: LongWORD;<br> &nbsp; &nbsp;Msg: TMessage;<br> &nbsp;end;<br><br>var<br> &nbsp;DLLData: PMyDLLVar;<br><br>{---------------------------------------}<br>{函数名:NewWndProc<br>{函数功能:新的窗口过程<br>{函数参数:hHwnd:窗口句柄 Msg:消息ID<br>{ &nbsp; &nbsp; &nbsp; &nbsp; wParam, lParam:消息参数<br>{函数返回值:下一个窗口过程的返回值<br>{---------------------------------------}<br>function NewWndProc(hHwnd, Msg, wParam, lParam: LongWORD): Longint; stdcall;<br>begin<br> &nbsp;if Msg = WM_UNSUBCLASS then &nbsp; {如果收到卸载子类化消息就恢复以前的WndProc}<br> &nbsp;begin<br> &nbsp; &nbsp;SetWindowLong(DLLData^.HookWindow, GWL_WNDPROC, longint(DLLData^.OldWndProc));<br> &nbsp; &nbsp;exit;<br> &nbsp;end;<br> &nbsp;{这儿是把收到的消息放在映射的内存中,我们自己的程序可以通过读这个内存来得到监视到的消息.}<br> &nbsp;DLLData^.Msg.Msg := Msg; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp;DLLData^.Msg.WParam := wParam;<br> &nbsp;DLLData^.Msg.LParam := lParam;<br> &nbsp;DLLData^.MsgHwnd := hHwnd;<br> &nbsp;{给监视窗口发送拦载新消息的消息}<br> &nbsp;SendMessage(DLLData^.SpyWindow, WM_NEWMESSAGE, 0, 0);<br> &nbsp;{这儿可以添加自己对目标进程消息处理的代码,因为己经是在目标进程的地址空间内,现在可以为所<br> &nbsp;欲为 ^_^)<br> &nbsp;Result := CallWindowProc(DLLData^.OldWndProc, hHwnd, Msg, wParam, lParam);<br>end;<br><br>{------------------------------------}<br>{过程名:HookProc<br>{过程功能:HOOK过程<br>{过程参数:nCode, wParam, lParam消息的相<br>{ &nbsp; &nbsp; &nbsp; &nbsp; 关参数<br>{------------------------------------}<br>procedure HookProc(nCode, wParam, lParam: LongWORD);stdcall;<br>var<br> &nbsp;hEvent: THandle;<br>begin<br> &nbsp;if not DllData^.SubClass then &nbsp;{如果此窗口未子类化}<br> &nbsp;begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{保存窗口过程地址并子类化}<br> &nbsp; &nbsp;if hEvent &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;WaitForSingleObject(hEvent, INFINITE);<br> &nbsp; &nbsp; &nbsp;CloseHandle(hEvent); <br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;DLLData^.OldWndProc := pointer(GetWindowLong(DLLData^.HookWindow, GWL_WNDPROC));<br> &nbsp; &nbsp;SetWindowLong(DLLData^.HookWindow, GWL_WNDPROC, integer(@NewWndProc));<br> &nbsp; &nbsp;DLLData^.SubClass := True;<br> &nbsp; &nbsp;hEvent := OpenEvent(Synchronize, False, HOOK_EVENT_NAME);<br> &nbsp;end;<br> &nbsp;{调用下一个Hook}<br> &nbsp;CallNextHookEx(DLLData^.hHook, nCode, wParam, lParam);<br>end;<br><br><br>{------------------------------------}<br>{函数名:InstallHook<br>{函数功能:在指定窗口上安装HOOK<br>{函数参数:HWindow:要安装HOOK的窗口<br>{ &nbsp; &nbsp; &nbsp; &nbsp; SWindow:用于接收消息的窗口<br>{返回值:成功返回TRUE,失败返回FALSE<br>{------------------------------------}<br>function InstallHook(HWindow, SWindow: LongWORD):Boolean;stdcall;<br>var<br> &nbsp;ThreadID: LongWORD;<br> &nbsp;hEvent: THandle;<br>begin<br> &nbsp;Result := False;<br> &nbsp;DLLData^.hHook := 0;<br> &nbsp;DLLData^.HookWindow := HWindow;<br> &nbsp;DLLData^.SpyWindow := SWindow;<br> &nbsp;{得到指定窗口的线程ID}<br> &nbsp;ThreadID := GetWindowThreadProcessId(HWindow, nil);<br> &nbsp;{给指定窗口挂上钩子}<br> &nbsp;hEvent := CreateEvent(nil, True, False, HOOK_EVENT_NAME);<br> &nbsp;DLLData^.hHook := SetWindowsHookEx(WH_GETMESSAGE, @HookProc, Hinstance, ThreadID);<br> &nbsp;SetEvent(hEvent);<br> &nbsp;CloseHandle(hEvent);<br> &nbsp;if DLLData^.hHook &gt; 0 then Result := True; &nbsp;{是否成功HOOK}<br>end;<br><br>{------------------------------------}<br>{过程名:UnHook<br>{过程功能:卸载HOOK<br>{过程参数:无<br>{------------------------------------}<br>procedure UnHook;stdcall;<br>begin<br> &nbsp;{发送卸载子类化消息给指定窗口}<br> &nbsp;SendMessage(DLLData^.HookWindow, WM_UNSUBCLASS, 0, 0);<br> &nbsp;DLLData^.SubClass := False;<br> &nbsp;{卸载Hook}<br> &nbsp;UnhookWindowsHookEx(DLLData^.hHook);<br>end;<br><br>{------------------------------------}<br>{过程名:DLL入口函数<br>{过程功能:进行DLL初始化,释放等<br>{过程参数:DLL状态<br>{------------------------------------}<br>procedure MyDLLHandler(Reason: Integer);<br>var<br> &nbsp;FHandle: LongWORD;<br>begin<br> &nbsp;case Reason of<br> &nbsp; &nbsp;DLL_PROCESS_ATTACH:<br> &nbsp; &nbsp;begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{建立文件映射,以实现DLL中的全局变量}<br> &nbsp; &nbsp; &nbsp;FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, $ff, 'MYDLLDATA');<br> &nbsp; &nbsp; &nbsp;if FHandle = 0 then<br> &nbsp; &nbsp; &nbsp;if GetLastError = ERROR_ALREADY_EXISTS then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');<br> &nbsp; &nbsp; &nbsp; &nbsp;if FHandle = 0 then Exit;<br> &nbsp; &nbsp; &nbsp;end else Exit;<br> &nbsp; &nbsp; &nbsp;DLLData := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);<br> &nbsp; &nbsp; &nbsp;if DLLData = nil then<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(FHandle);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;DLL_PROCESS_DETACH:<br> &nbsp; &nbsp; &nbsp;if Assigned(DLLData) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;UnmapViewOfFile(DLLData);<br> &nbsp; &nbsp; &nbsp; &nbsp;DLLData := nil;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;DLL_THREAD_ATTACH:;<br> &nbsp; &nbsp;DLL_THREAD_DETACH:;<br> &nbsp;end;<br>end;<br><br>{$R *.res}<br>exports<br> &nbsp;InstallHook, UnHook, HookProc;<br><br>begin<br> &nbsp;DLLProc := @MyDLLHandler;<br> &nbsp;MyDLLhandler(DLL_PROCESS_ATTACH);<br>end.<br><br> &nbsp;编译这个DLL,然后在我们的程序中加载这个DLL,并调用InstallHook(目标窗口句柄, 自己窗口句柄)就可 &nbsp;以实现对目标窗口消息的监视了(在接收到WM_NEWMESSAGE消息时读映射的内存),调用UnHook则可以卸载掉子类化和HOOK.具休的代码还请读者自行编写.
 
谢谢,我研究一下先。<br>结贴~~
 
接受答案了.
 
后退
顶部