Search code examples
windowsvb.netprocessenumerate

vb.net/pinvoke: enum specific process windows


Hello guys
i'm making a little app in which i need to enumerate all the windows of a specific process.
i managed to do that but through enumerating all the windows in the system and then just test if it is the process i'm talking about or not by using

GetWindowThreadProcessId(hwnd, proc)
Dim _Process = Process.GetProcessById(proc)
Dim _name = _Process.ProcessName
If (_name = "ProcessName") then

but this method seem to take a lot of unnecessary time. is there a wind32 function that can allow me to enumerate the windows of a specific process giving its handle. thank you.


Solution

  • You are already using GetWindowThreadProcessId(). That function returns a thread ID, you can use it with EnumThreadWindows() to get all the toplevel windows owned by the thread. If necessary, EnumChildWindows() gets you the child windows owned by each toplevel window.

    This approach does not give you windows that might be owned by any other thread in the process. That's however quite rare and you typically don't want to know about them. If that's a hangup then use Process.Threads to enumerate all the threads in the process, ProcessThread.Id gets you the thread ID.