to engleking:<br> 你从 UNDU 拷一段代码干吗?全局钩子函数应放在 Dll 中,再贴:<br>In WinME/2K there is a new avaiable API called OpenThread().<br>However there are two different solutions even if you do not have WinME/2K<br>... the first idea is based upon the documentation of GetCurrentThread()<br>function:<br><br>"The function cannot be used by one thread to create a handle that can be<br>used by other threads to refer to the first thread. The handle is always<br>interpreted as referring to the thread that is using it. A thread can create<br>a "real" handle of itself that can be used by other threads, or inherited by<br>other processes, by specifying the pseudohandle as the source handle in a<br>call to the DuplicateHandle function."<br><br>The idea is the following:<br><br>1 - You install an Hook into the interested Thread (you have its Thread ID)<br>2 - From the remote process you call the DuplicateHandle passing the<br>PseudoHandle retrieved by GetCurrentThread<br>3 - You "export" the real handle<br><br>The only problem is that if the interested Thread does not have a message<br>queue the Hook will never be installed and you will not be able to call the<br>DuplicateHandle ...<br><br>Source Code:<br><br>{钩子 DLL}<br>library hThreadFromTid;<br><br>{Copyright (c) by Carlo Bertuccini -- 2001}<br><br>uses<br> Dialogs,<br> SysUtils,<br> ClipBrd,<br> Windows;<br><br>{$R *.RES}<br><br>var<br>hHook : THandle;<br>OnlyOnce : Boolean = True;<br>WM_NOTIFICA : Cardinal;<br><br><br>Function HookProc(Code:Integer; WPar : Wparam; LPar :Lparam) : Integer;<br>Stdcall;<br>var hThread, hNewThread, hRemoteProcess : THandle;<br> PID : dWord;<br>Begin<br> If OnlyOnce Then begin<br> OnlyOnce := False;<br> ClipBoard.Clear;<br> WM_NOTIFICA:=RegisterWindowMessage('MyMessage');<br> hThread := GetCurrentThread();<br> GetWindowThreadProcessId(FindWindow('TForm1','TheFormText'),@PID);<br> hRemoteProcess:=OpenProcess(PROCESS_DUP_HANDLE,TRUE,PID);<br> if<br>DuplicateHandle(OpenProcess(PROCESS_DUP_HANDLE,True,GetCurrentProcessId),hTh<br>read,hRemoteProcess,@(hNewThread),0,True,DUPLICATE_SAME_ACCESS) <> False<br>then<br> ClipBoard.SetTextBuf(PChar(IntToStr(hNewThread)))<br> else<br> ClipBoard.SetTextBuf('Errore');<br> PostMessage(HWND_BROADCAST,WM_NOTIFICA,0,0);<br> end;<br> Result := CallNextHookEx(hHook,Code,WPar,LPar);<br>end;<br><br><br>Procedure Load (TID : dWord); stdcall;<br>Begin<br> hHook:=SetWindowsHookEx(WH_GETMESSAGE,@HookProc,hInstance,TID);<br>end;<br><br><br>Procedure Remove; stdcall;<br>Begin<br> UnHookWindowsHookEx(hHook);<br>end;<br><br>Exports<br>Load,Remove;<br><br><br>end.<br><br>Executable (set the Form caption to "TheFormText" ... is important)<br>{exe 主文件}<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, ClipBrd;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Edit1: TEdit;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> Procedure MessageHandler (var MSG : TMSG; var Handled : boolean);<br> { Public declarations }<br> end;<br><br>Procedure Load (TID : dWord); stdcall; External 'hThreadFromTid.DLL';<br>Procedure Remove; stdcall; External 'hThreadFromTid.DLL';<br><br><br>var<br> Form1: TForm1;<br> WM_NOTIFICA : Cardinal;<br><br>implementation<br><br>{$R *.DFM}<br><br>Procedure TForm1.MessageHandler(var MSG : TMSG; var Handled : boolean);<br>Begin<br> if (Msg.message = WM_NOTIFICA) then begin<br> Edit1.Clear;<br> Edit1.text := 'Handle Reale: ' + ClipBoard.AsText;<br> end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>Var TID : dWord;<br> hExplorer : THandle;<br>begin<br> hExplorer:=FindWindow('Progman','Program Manager');<br> If hExplorer<>0 Then Begin<br> TID := GetWindowThreadProcessId(hExplorer,nil);<br> Load(TID);<br> PostMessage(hExplorer,WM_MOUSEMOVE,0,0);<br> end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> Remove;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> WM_NOTIFICA:=RegisterWindowMessage('MyMessage');<br> Application.OnMessage := MessageHandler;<br>end;<br><br>end.<br><br>If this solution fails I have an undocumented solution: the OpenProcess()<br>API can become an "OpenThread()" ...