GetWindowThreadProcessId Function--------------------------------------------------------------------------------The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window. SyntaxDWORD GetWindowThreadProcessId( HWND hWnd, LPDWORD lpdwProcessId );ParametershWnd [in] Handle to the window. lpdwProcessId [out] Pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not. Return ValueThe return value is the identifier of the thread that created the window.
GetWindowThreadProcessId 这是获得线程ID的,楼主要的是进程句柄
正是!请看MSDN对第二个参数的说明,这个我用过,返回的正是进程ID.主线程ID就是进程ID.lpdwProcessId [out] Pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not.
LZ 给你一段根据进程名获取进程ID的代码 unit GetProcessPid;interfaceuses TLhelp32,SysUtils;Function GetPID(_GetPID:String):String;implementation //------------------------------------------------------------------------------Function GetPID(_GetPID:String):String; var h:thandle; f:boolean; lppe:tprocessentry32; begin h := CreateToolhelp32Snapshot(TH32cs_SnapProcess, 0); lppe.dwSize := sizeof(lppe); f := Process32First(h, lppe); //lppe.szExeFile是进程的名字,自己挑选你要的 //lppe.th32ProcessID就是你要的进程号 while integer(f) <> 0 do begin //if lppe.szExeFile='QQ.exe' then showmessage('ok'); if lppe.szExeFile = _GetPID then begin Result:=(inttostr(lppe.th32ProcessID)); break; end; f := Process32Next(h, lppe); end; end; end.
HWND hWnd,
LPDWORD lpdwProcessId
);ParametershWnd
[in] Handle to the window. lpdwProcessId
[out] Pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not. Return ValueThe return value is the identifier of the thread that created the window.
这是获得线程ID的,楼主要的是进程句柄
[out] Pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not.
HANDLE WINAPI OpenProcess(
__in DWORD dwDesiredAccess,
__in BOOL bInheritHandle,
__in DWORD dwProcessId
);
得進程就是 FindWindow
procedure TForm1.Button1Click(Sender: TObject);
var
hWindow: HWND; { 窗体句柄 }
dwProcessID: DWORD; { 进程 ID }
hProcess: THandle; { 进程句柄 }
begin
{ 根据标题获取窗体的句柄 }
hWindow := FindWindow(nil, '标题'); { 通过窗体句柄获取进程 ID }
GetWindowThreadProcessId(hWindow, dwProcessID); { 通过进程 ID 获取进程句柄 }
hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, dwProcessID); { 结束该进程 }
TerminateProcess(hProcess, 0);
end;
winh := FindWindow(nil, PChar('记事本'));得到进程句柄:
var
pid: DWORD; // 进程ID
tid: DWORD; // 线程IDtid = GetWindowThreadProcessId(winh, pid);
然后GetWindowThreadId找到句柄
unit GetProcessPid;interfaceuses
TLhelp32,SysUtils;Function GetPID(_GetPID:String):String;implementation
//------------------------------------------------------------------------------Function GetPID(_GetPID:String):String;
var
h:thandle;
f:boolean;
lppe:tprocessentry32;
begin
h := CreateToolhelp32Snapshot(TH32cs_SnapProcess, 0);
lppe.dwSize := sizeof(lppe);
f := Process32First(h, lppe); //lppe.szExeFile是进程的名字,自己挑选你要的
//lppe.th32ProcessID就是你要的进程号
while integer(f) <> 0 do
begin
//if lppe.szExeFile='QQ.exe' then showmessage('ok');
if lppe.szExeFile = _GetPID then
begin
Result:=(inttostr(lppe.th32ProcessID));
break;
end;
f := Process32Next(h, lppe);
end;
end;
end.