I've got it setup to just grab the process list without worrying about window captions. I think this will suit my purpose, at least for now.
Thanks
I've got it setup to just grab the process list without worrying about window captions. I think this will suit my purpose, at least for now.
Thanks
Give this a try...
Code:#include <windows.h> #include <stdio.h> BOOL CALLBACK GetWindowFromProcessID(HWND hWnd, LPARAM lParam) { static HWND hWnd2Find; static DWORD dwMatchProcessID; if(lParam != 0){ dwMatchProcessID = (DWORD)hWnd; if(EnumWindows(GetWindowFromProcessID, 0)) return FALSE; *(HWND *)lParam = hWnd2Find; } else{ DWORD dwPID = 0; GetWindowThreadProcessId(hWnd, &dwPID); if(dwMatchProcessID == dwPID) { hWnd2Find = hWnd; return FALSE; } } return TRUE; } int main(void) { HWND hWnd; DWORD dwPID = 3268; char szWindowTitle[500] = {0}; if(GetWindowFromProcessID((HWND)dwPID, (DWORD)&hWnd)) { GetWindowText(hWnd, szWindowTitle, sizeof(szWindowTitle)-1); printf("Window title is %s\n", szWindowTitle ); } else printf("No Window found\n"); return 0; }
Wow, That's pretty slick. However, it seems to be grabbing a random window instead of the parent/root window of the process. Any ideas for isolating it with this method? Is it just a matter of using/looping GetParent() or something?
The original code sample assumes that the first window found is the window of interest. This works fine with apps such as notepad. But it isn't appropriate for MDI apps such as MS Word etc. So, a modified solution would be to enumerate all windows for a particular process ID as follows:However, it seems to be grabbing a random window instead of the parent/root window of the process. Any ideas for isolating it with this method? Is it just a matter of using/looping GetParent() or something?
Code:#include <stdio.h> #include <stdlib.h> #include <windows.h> void ListChildWindows(HWND hWnd, DWORD dwPIDCheck) { DWORD dwPID; char szBuffer[1000] = {0}; GetWindowThreadProcessId(hWnd, &dwPID); GetWindowText(hWnd, szBuffer, 1000); if(dwPID == dwPIDCheck) printf("%-8X %-40s %-8d\n", hWnd, szBuffer,dwPID); for(hWnd = GetWindow(hWnd, GW_CHILD); hWnd != NULL; hWnd = GetWindow(hWnd, GW_HWNDNEXT)) ListChildWindows(hWnd, dwPIDCheck); } int main(int argc, char **argv) { HWND hDesktopWindow = NULL; if(argc == 1) { printf("Invalid arguments\n"); return 0; } hDesktopWindow = GetDesktopWindow(); ListChildWindows(hDesktopWindow, atoi(argv[1])); return 0; }