Aug 10, 2021 at 3:06pm UTC
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 UTC
Aug 10, 2021 at 3:45pm UTC
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 UTC
Aug 10, 2021 at 5:26pm UTC
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 UTC
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 UTC
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 11, 2021 at 3:25pm UTC
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 UTC
Aug 11, 2021 at 4:16pm UTC
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 UTC
Aug 12, 2021 at 10:28am UTC
thanks so much Ganado <3
thanks seeplus <3
example not work ,
Last edited on Aug 12, 2021 at 11:19am UTC
Aug 12, 2021 at 10:58pm UTC
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 13, 2021 at 2:54am UTC
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 UTC
Aug 13, 2021 at 8:23am UTC
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 UTC
malibor working thanks so much ,
Ganado thanks for help <3 ,
seeplus okay i'll update thanks so much ,