how to convert HANDLE hProcess to Hwnd

how to convert HANDLE hProcess to Hwnd ,
I want use it in this function SetForegroundWindow();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void killProcess(const char *filename)
{
    HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
    PROCESSENTRY32 pEntry;
    pEntry.dwSize = sizeof (pEntry);
    BOOL hRes = Process32First(hSnapShot, &pEntry);
    while (hRes)
    {
        if (strcmp(pEntry.szExeFile, filename) == 0)
        {
            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
                                          (DWORD) pEntry.th32ProcessID);
            if (hProcess != NULL)
            {
                //TerminateProcess(hProcess, 9);
                CloseHandle(hProcess);
            }
        }
        hRes = Process32Next(hSnapShot, &pEntry);
    }
    CloseHandle(hSnapShot);
}
Last edited on
See comments for explanation, I didn't test it but it should work.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        // handle to the window you want to put foreground
	HWND window_handle = NULL;

        // enumeration callback to find window handle (HWND)
	BOOL __stdcall EnumWindowsCallback(HWND hwnd, LPARAM lParam)
	{
		DWORD process;
		GetWindowThreadProcessId(hwnd, &process);

		if (process == lParam)
		{
			window_handle = hwnd;
			return FALSE;
		}

		return TRUE;
	}

	// Converts process ID to HWND
	void GetWindowHandle(LPARAM processID)
	{
		EnumWindows(EnumWindowsCallback, processID);
	}


	void killProcess(const char* filename)
	{
		HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
		PROCESSENTRY32 pEntry;
		pEntry.dwSize = sizeof(pEntry);
		BOOL hRes = Process32First(hSnapShot, &pEntry);

		while (hRes)
		{
			if (strcmp(pEntry.szExeFile, filename) == 0)
			{
                                // if this is wanted process, get it's HWND
				GetWindowHandle(pEntry.th32ProcessID);

                                // set process window to foreground
				if (window_handle != NULL)
				{
					SetForegroundWindow(window_handle);

                                        // reset it for subsequent calls
                                        window_handle  = NULL;
				}
				else
				{
					MessageBoxA(NULL, "window handle not found", "Information", MB_ICONINFORMATION);
				}
                           
                                break;
			}

			hRes = Process32Next(hSnapShot, &pEntry);
		}

		CloseHandle(hSnapShot);
	}
Last edited on
thanks so much ,code is work
Please I have a problem , i run game 3 times how can get hwnd to each window by while loop
It may help if you explain what are you trying to do.

You already have the code to get window handle, with a bit of modifications you can get handles for all game instances.

But it sounds like you're attempting to write game cheats, and it may not work by just calling functions with HWND's of another processes.
It may help if you explain what are you trying to do.

i run 3x calc
calc.exe
calc.exe
calc.exe
how can get hwnd to each calc by loop?





see this: https://stackoverflow.com/questions/11711417/get-hwnd-by-process-id-c

you can't do it directly. your program knows the pointers / addresses for its own stuff, but has no clue about another program's internal workings. You can sometimes lift them with tools as shown in this post, and sometimes it can be more troubling.
Thanks all i found this example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>
#include <Windows.h>

static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam) {
    int length = GetWindowTextLength(hWnd);
    char* buffer = new char[length + 1];
    GetWindowText(hWnd, buffer, length + 1);
    std::string windowTitle(buffer);

    // List visible windows with a non-empty title
    if (IsWindowVisible(hWnd) && length != 0) {
        std::cout << hWnd << ":  " << windowTitle << std::endl;
    }
    return TRUE;
}

int main() {
    std::cout << "Enmumerating windows..." << std::endl;
    EnumWindows(enumWindowCallback, NULL);;
    return 0;
}




You need to delete[] the buffer after line 9 (after the memory is copied into the std::string).
Last edited on
L6-12 can be simply (not tried):

1
2
3
4
std::string windowTitle (GetWindowTextLength(hWnd) + 1, 0);
GetWindowText(hWnd, windowTitle.data(), windowTitle.size());

if (IsWindowVisible(hWnd) && !windowTitle.empty()) {


Last edited on
thanks so much Ganado <3

thanks seeplus <3
example not work ,
Last edited on
example not work ,

What is the error?
Well I said it wasn't tried!

OK. This issue is that GetWindowText() always returns a null-terminated string - even when there's no title! So the if condition also needs to test for initial null. Ahhh...

1
2
3
4
5
6
7
8
9
10
11
static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam) {

	std::string windowTitle(GetWindowTextLength(hWnd) + 1, 0);
	GetWindowText(hWnd, windowTitle.data(), windowTitle.size());

	// List visible windows with a non-empty title
	if (IsWindowVisible(hWnd) && !windowTitle.empty() && windowTitle.front() != 0)
		std::cout << hWnd << ":  " << windowTitle << '\n';

	return TRUE;
}


Ganado Error here windowTitle.data() line 2
[Error] invalid conversion from 'const char*' to 'LPSTR {aka char*}' [-fpermissive]

Seeplus

Same error as in the previous example error line 4
[Error] invalid conversion from 'const char*' to 'LPSTR {aka char*}' [-fpermissive]

thanks any way .




This modified sample of yours should work:

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
#include <string>
#include <iostream>
#include <Windows.h>

#ifndef UNICODE
#error "Please set your project to 'Use unicode character set'"
#endif

static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam)
{
	const int length = GetWindowTextLengthW(hWnd);
	wchar_t* buffer = new wchar_t[static_cast<std::size_t>(length) + 1];
	GetWindowTextW(hWnd, buffer, length + 1);
	std::wstring windowTitle(buffer);
	delete[] buffer;

	// List visible windows with a non-empty title
	if ((IsWindowVisible(hWnd) != FALSE) && (length != 0))
	{
		std::wcout << hWnd << L":  " << windowTitle << std::endl;
	}

	return TRUE;
}

int main()
{
	std::wcout << L"Enmumerating windows..." << std::endl;
	EnumWindows(enumWindowCallback, NULL);;
	return 0;
}
Same error as in the previous example error line 4
[Error] invalid conversion from 'const char*' to 'LPSTR {aka char*}' [-fpermissive]
Since C++17, string.data() no longer gives a const char* and gives a char* instead. (c_str() gives a const char*)
Last edited on
You need to compile as C++17. As it's now over half way through 2021 with C++20 now the current standard, I don't believe that assuming you're compiling for at least C++17 is unreasonable...
malibor working thanks so much ,

Ganado thanks for help <3 ,

seeplus okay i'll update thanks so much ,




Topic archived. No new replies allowed.