DWORD pid = GetWindowThreadProcessId(hWnd, NULL);
But that should return a thread ID, not a process ID.
(If you want a process ID, you need to specify a variable in the 2nd parameter, which above is NULL.)
Code: Select all
void Util_WinKill(HWND hWnd)
{
DWORD_PTR dwResult;
// Use WM_CLOSE vs. SC_CLOSE in this case, since the target window is slightly more likely to
// respond to that:
if (!SendMessageTimeout(hWnd, WM_CLOSE, 0, 0, SMTO_ABORTIFHUNG, 500, &dwResult)) // Wait up to 500ms.
{
// Use more force - Mwuahaha
DWORD pid = GetWindowThreadProcessId(hWnd, NULL);
HANDLE hProcess = pid ? OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid) : NULL;
if (hProcess)
{
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
}
}