I want to get the first HWND owned by a ProcessID. Everything runs but it always shows the dialog box saying "No windows found" but i know it is there since i can see on on the screen.
(I find it hard to debug in windows programs since i cant put printf everywhere, how is printf debugging done while writing windows programs?)

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lparam)
{
//Getting the ProcessID for that the HWND belongs to
tempID=GetWindowThreadProcessId(hwnd,NULL);
//If the ID is the same as my process id
if (tempID == processID){
//Take this hwnd as the one i want
myHWND = hwnd;
//Quit the Enum when the first window is found
return (FALSE);
}
//Otherwise run again
return (TRUE);
}

--------------------------------------------------------
This code runs when i hit a button
//Returns processID from functions that first runs CreateProcess and then GetProcessID and returns it
processID = startup();
//Iterate every windows to find the first one that belongs to my process
EnumWindows(&EnumWindowsProc,NULL);
//If the global variabel have recived a value the IF expression got executed in EnumWindowProc
if (myHWND != NULL)
//Visual confirmation of my the HWND
SetDlgItemInt(hwnd, IDC_NUMBER, (DWORD)myHWND, FALSE);
else
//Found no windows for this process
MessageBox(hwnd, "No windows found!", "Warning", MB_OK);

01-19-2012

favetelinguis

Is my code understandable?

01-19-2012

novacain

Why do you need to do this?

01-20-2012

favetelinguis

What my program does it that it starts an external exe and gets the hwnd for the window, then im going to get the DC for this window so that i can do stuff with it.

01-20-2012

oogabooga

Two problems:

1. GetWindowThreadProcessID returns the thread ID. To get the process ID you need to use the second parameter. GetWindowThreadProcessID(hwnd, &tempID)

2. If you enumerate the windows right after creating the process, the window may not have been created yet. Try Sleep(100) before enumerating.