Closing the active window

Oct 10, 2009 at 1:20pm
I used GetForegroundWindow() to get the handle to the current active window, but I need to know how to exit that window. Does anyone know?

EDIT:
I tried a different approach, using GetProcessId(hWnd).
hWnd = GetForegroundWindow();
PID = GetProcessId(hWnd);
system("taskkill /PID" + PID)

or something of that sort. The problem is PID is always returning 0, and I don't know what's wrong.
Last edited on Oct 10, 2009 at 1:40pm
Oct 10, 2009 at 3:39pm
Can't you just use DestroyWindow(hWnd)? In programs I write I usually put memory de-allocation code in a WM_CLOSE handler. So I usually SendMessage() a WM_CLOSE to the hWnd and let the default processing do the DestroyWindow() call.
Oct 10, 2009 at 8:04pm
Using your method,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BOOL TerminateWindow(__in HWND hWnd)
{
  DWORD   dwProcessId;
  HANDLE  hProcess;
  BOOL    bRet = FALSE;

  GetWindowThreadProcessId(hWnd, &dwProcessId);
  hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
  if(hProcess != NULL)
  {
    if(TerminateProcess(hProcess, 0))
      bRet = TRUE;
    CloseHandle(hProcess);
  }

  return bRet;
}
Topic archived. No new replies allowed.