一、VC
--------------------------------------------------------------------------------
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.
意譯:
找出某個視窗的創建者(執行緒或進程),返回創建者的標誌符。
哪個執行緒創建了這個視窗,返回的就是這個執行緒的id號 (進程只有一個執行緒的話,那么執行緒標誌符與進程標誌符就是指同一個標誌符)
可理解為找出某棟房屋的建築工人的名字。
DWORD GetWindowThreadProcessId(
HWND hWnd,
LPDWORD lpdwProcessId
);
hWnd [in] (向函式提供的)被查找視窗的句柄.
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. (如果參數不為NULL,即提供了存放處--變數,那么本函式把進程標誌拷貝到存放處,否則不動作。)
The return value is the identifier of the thread that created the window.
返回執行緒號,注意, lpdwProcessId 是存放進程號的變數。返回值是執行緒號, lpdwProcessId 是進程號存放處。
當然可以把答案再放到其它地方。如
DWORD dwPID, dwTID;
dwTID = GetWindowThreadProcessId( hWnd, &dwPID );
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
二、VB聲明
Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
1、說明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
這個函式獲得指定執行緒的標識符,此執行緒創建了指定的視窗,並且隨機的產生了這個標識符.
Long,擁有視窗的執行緒的標識符
3、參數表參數 類型及說明
lpdwProcessId Long,指定一個變數,用於裝載擁有那個視窗的一個進程的標識符
hwnd Long,指定視窗句柄
delphi實例:
var
t: Thandle;
begin
t := findwindow('Notepad2', nil);
ShowMessage(IntToStr(t));
memo1.lines.Add(IntToStr(GetWindowThreadProcessId(t, nil)));
二、c#聲明
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
實例
int calcID=0;
int calcTD=0;
calcTD = GetWindowThreadProcessId(FrmMain.mainhWnd, out calcID);
返回值calcTD為執行緒ID
輸出值calcID為進程ID