GetWindowThreadProcessId Function

Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

Platforms: Win 32s, Win 95/98, Win NT

GetWindowThreadProcessId finds identifiers for the thread which owns a given window and the process which created it. These identifiers can be used to later get information about the program controlling the window. Note that these two values are not handles but just numerical identifiers. The process identifier is put into the variable passed as lpdwProcessId, while the function returns the thread identifier.

hwnd
A handle to the window to find the identifers of the owning thread and creating process.
lpdwProcessId
Receives the identifer for the process which created the window.

Example:

' Display the title bar text of all windows controlled by the thread
' which the window Form1 is in.  This task is given to the callback function, which
' will receive each handle individually.  Note that if the window has no title bar
' text, it will not be displayed (for clarity's sake).

' *** Place this code in a module.  This is the callback function. ***
' This function displays the title bar text of the window identified by hwnd.
Public Function EnumThreadWndProc (ByVal hwnd As Long, ByVal lParam As Long) As Long
  Dim slength As Long, wintext As String  ' title bar text length and buffer
  Dim retval As Long  ' return value
  Static winnum As Integer  ' counter keeps track of how many windows have been enumerated

  winnum = winnum + 1  ' one more window enumerated....
  slength = GetWindowTextLength(hwnd) + 1  ' get length of title bar text
  If slength > 1  ' if return value refers to non-empty string
    buffer = Space(slength)  ' make room in the buffer
    retval = GetWindowText(hwnd, buffer, slength)  ' get title bar text
    Debug.Print "Window #"; winnum; " : ";  ' display number of enumerated window
    Debug.Print Left(buffer, slength - 1)  ' display title bar text of enumerated window
  End If

  EnumThreadWndProc = 1  ' return value of 1 means continue enumeration
End Function

' *** Place this code wherever you want to enumerate the windows. ***
Dim threadid As Long, processid As Long  ' receive id to thread and process of Form1
Dim retval As Long  ' return value

' Determine the thread which owns the window Form1.
threadid = GetWindowThreadProcessId(Form1.hWnd, processid)
' Use the callback function to list all of the enumerated thrad windows.  Note that lParam
' is set to 0 because we don't need to pass any additional information to the function.
retval = EnumThreadWindows(threadid, AddressOf EnumThreadWndProc, 0)

Category: Windows

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getwindowthreadprocessid.html