how to convert HANDLE hProcess to Hwnd

Aug 10, 2021 at 3:06pm
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 Aug 10, 2021 at 3:06pm
Aug 10, 2021 at 3:45pm
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 Aug 10, 2021 at 4:23pm
Aug 10, 2021 at 5:26pm
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
Aug 10, 2021 at 5:59pm
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.
Aug 10, 2021 at 10:47pm
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?





Aug 10, 2021 at 11:57pm
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.
Aug 11, 2021 at 10:30am
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;
}




Aug 11, 2021 at 3:25pm
You need to delete[] the buffer after line 9 (after the memory is copied into the std::string).
Last edited on Aug 11, 2021 at 3:25pm
Aug 11, 2021 at 4:16pm
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 Aug 11, 2021 at 4:20pm
Aug 12, 2021 at 10:28am
thanks so much Ganado <3

thanks seeplus <3
example not work ,
Last edited on Aug 12, 2021 at 11:19am
Aug 12, 2021 at 2:11pm
example not work ,

What is the error?
Aug 12, 2021 at 2:59pm
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;
}


Aug 12, 2021 at 10:58pm
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 .




Aug 12, 2021 at 11:13pm
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;
}
Aug 13, 2021 at 2:54am
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 Aug 13, 2021 at 2:55am
Aug 13, 2021 at 8:23am
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...
Aug 13, 2021 at 8:48am
malibor working thanks so much ,

Ganado thanks for help <3 ,

seeplus okay i'll update thanks so much ,




Topic archived. No new replies allowed.