ProcessHandle to hWND (probably over the windowname)

Hello out there.

To get all Child-Controls (such as buttons) of a window form, I need to get the Window Handle of a process (without .NET).

The Windowname is dynamicly created, so I'm not able to use the ::FindWindow function.
So my first idea was to loop through all processes, and compare the processnames with my process that I'm looking for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
HWND MainWindowHandle(char ProcessName[])
{
	//CHAR bla[1024];

	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
	if (hSnapshot == (HANDLE)-1)
	{
		printf("Unable to create a processlist snapshot..\r\n");
	}

	PROCESSENTRY32 pe;

	pe.dwSize = sizeof(PROCESSENTRY32);

	printf("gathering processes....\r\n");

	BOOL retval = Process32First(hSnapshot, &pe);
	while(retval)	// loop each process
	{
		printf("Process ID  : %08X\n",pe.th32ProcessID);
		printf("Process Name: %s\n", pe.szExeFile);

		if (stricmp(pe.szExeFile, ProcessName) == 0)
		{	
			 //HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, FALSE, pe.th32ProcessID ); 
		}

		pe.dwSize=sizeof(PROCESSENTRY32);
		retval=Process32Next(hSnapshot,&pe);
	}

	return NULL;
}


This works very well, but the only problem is, that I get the Process HANDLE and not the Window Handle itself.
How can I "convert" the HANDLE to an hWND (or smth like this)?


Best regards,
Dane

PS: Sorry for my bad english. :)

The Windowname is dynamicly created, so I'm not able to use the ::FindWindow function.
I don't see why not.
1
2
3
//(UNICODE is not #defined)
std::string title=/*...*/;
HWND handle=FindWindow(0,title.c_str());
Ok, so let's say I don't know the Windowtitle..
In the internet I've found some hints that I can use the EnumThreadWindow Function.

But it's not working - Can you tell me why?

1
2
3
4
5
		if (stricmp(pe.szExeFile, ProcessName) == 0)
		{	
			::EnumThreadWindows(pe.th32ProcessID, EnumThreadWndProc, NULL);
			 //HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, FALSE, pe.th32ProcessID ); 
		}



1
2
3
4
5
6
7
BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam)
{
	char WindowTitle[1024];
	::GetWindowText(hwnd, WindowTitle, 1024);
	printf("%s", WindowTitle);
	return TRUE;
}


My program doesn't call the EnumThreadWndProc routine.
I need to get the Window Handle of a process

Are you sure you're thinking about the problem in the right way? A process can create zero or more top level windows, it isn't confined to creating one.
Topic archived. No new replies allowed.