Fylix
Fylix

Reputation: 2713

GetWindowThreadProcessId can not find my window handle, even though its process ID exists on Window

I have a setup application that if user clicks the same setup twice, they would get the pop up message "Another setup instance already running". Upon clicking OK on that message I want to put focus back on the existing installation window which has been running. I currently have the following codes:

if("setup.exe" == CString(buffer))
EnumWindows(EnumWindowsProc,(LPARAM)processID);


BOOL CALLBACK EnumWindowsProc(HWND windowHandle,LPARAM lParam)
{
DWORD searchedProcessId = (DWORD)lParam;
DWORD windowProcessId = 0;

GetWindowThreadProcessId(windowHandle,&windowProcessId);

if(searchedProcessId == windowProcessId)
{
       //Set focus when detects the right window.
       SetForegroundWindow(windowHandle);
       return FALSE;
}
return TRUE;
 } 

The above code works if I stay on the first screen/step on the installation wizard (install shield).

When I move to the next screen on the installation wizard, this focus logic no longer works. Upon debugging I found that the function GetWindowThreadProcessId could not find any windowProcessID that match searchedProcessId. I also confirmed the following:

I read up on GetWindowThreadProcessId and see the following note: "The return value is the identifier of the thread that created the window." In my own interpretation, was this because the 2nd step/screen on the wizard is generated by its own thread?

I tried to play around with EnumChildWindow() function but that did not help. I'd be very appricated if anyone able to point me in the right direction of trying to get this to work?

Upvotes: 0

Views: 5792

Answers (1)

uesp
uesp

Reputation: 6204

I would use Spy++ which comes with Visual Studio or a similar system monitoring tool to show you all the windows/threads so you can try to confirm exactly what the install wizard is doing. Chances are EnumWindows() is working just fine and it is a problem with your code or your assumptions of how things work.

Upvotes: 1

Related Questions