I've made a program that includes the ability to run multiple programs from the Form.Now, I use CreateProcessA to start it, but this only gives me a Struct of the ThreadID and ProcessID as return value.Now,if I want to close the process,I have no problems,using one of these two IDs are enough.
But now, I want to be able to know if a programs already running(Actually I'm already able to know this,since I keep an array of Exestarter class I made, and from there know if one of the available process was started or not).But if this process already running,I want to have its window to be brought back on top.
This is where I need to find the Hwnd.I have tried the following method to run through the processes running,but these api function seem to be running in a infinite loop.
Here's what I use:
Code:
Private Function gethWndFromProcessID(ByVal ProcessID As Long) As Long
gethWndFromProcessID = 0
Dim hwnd As Long, hWndStop As Long, hWndNext As Long, iLen, lngReply As Long
Dim lngAssocProcessID
' Get a handle to the active window (first in task list).
hwnd = GetActiveWindow()
hWndStop = hwnd
' Loop until you reach the end of the list.
Do
' Get the next window handle.
hWndNext = GetNextWindow(hwnd, GW_HWNDPREV)
' Get the ProcessID the this window
lngReply = GetWindowThreadProcessId(hWndNext, lngAssocProcessID)
' If this is the ProcessID I want set the return value and Exit.
If lngAssocProcessID = ProcessID Then
gethWndFromProcessID = hWndNext
Exit Function
End If
hwnd = hWndNext
Loop Until hwnd = hWndStop
End Function
[\code]
But it doesn't actually runs through all of the process.I don't understand.Any of you knows what might be thr problems or if there's any other way to obtain the Hwnd.
Thank you!