title: How do I prevent a Win32 application from running in the background after the main window is closed?
body: <p>I'm a beginner to the Win32 API, but I am intermediately experienced with C++. For learning purposes, I created, according to references, tutorials, and examples, a very simple Win32 application.</p>
  
  <p>The problem is, after the main window is closed, its process still runs in the background. How can I prevent this? In my WndProc function, I do have a WM_DESTROY case with DestroyWindow, but it doesn't seem to be doing the trick. The code is below:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  
  #ifdef UNICODE
  #include &lt;tchar.h&gt;
  #endif
  
  #include &lt;Windows.h&gt;
  
  HINSTANCE hinst;
  HWND hwnd;
  
  LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  
  #ifdef UNICODE
  int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  #else
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  #endif
  {
      MSG msg;
      WNDCLASSEX mainclass;
      BOOL bRet;
      UNREFERENCED_PARAMETER(lpCmdLine);
  
      mainclass.cbSize =          sizeof(WNDCLASSEX);
      mainclass.style =           CS_VREDRAW | CS_HREDRAW;
      mainclass.lpfnWndProc =     (WNDPROC) WndProc;
      mainclass.cbClsExtra =      NULL;
      mainclass.cbWndExtra =      NULL;
      mainclass.hInstance =       hInstance;
      mainclass.hIcon =           NULL;
      mainclass.hCursor =         LoadCursor(NULL, IDC_ARROW);
      mainclass.hbrBackground =   (HBRUSH) COLOR_WINDOW;
      mainclass.lpszMenuName =    NULL;
      mainclass.lpszClassName =   TEXT("MainWindowClass");
      mainclass.hIconSm =         NULL;
  
      if (!RegisterClassEx(&amp;mainclass))
          return FALSE;
  
      hinst = hInstance;
  
      hwnd = CreateWindowEx(
          WS_EX_WINDOWEDGE,
          TEXT("MainWindowClass"),
          TEXT("Test Window"),
          WS_CAPTION | WS_VISIBLE | WS_SIZEBOX | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
          CW_USEDEFAULT,
          CW_USEDEFAULT,
          CW_USEDEFAULT,
          CW_USEDEFAULT,
          NULL,
          NULL,
          hinst,
          NULL);
  
      ShowWindow(hwnd, nCmdShow);
      UpdateWindow(hwnd);
  
      while ((bRet = GetMessage(&amp;msg, NULL, 0, 0)) != 0)
      {
          if (bRet != -1)
          {
              TranslateMessage(&amp;msg);
              DispatchMessage(&amp;msg);
          }
      }
      return (int) msg.wParam;
  }
  
  LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  {
      PAINTSTRUCT ps;
      switch(uMsg)
      {
      case WM_DESTROY:
          DestroyWindow(hwnd);
          break;
      case WM_PAINT:
          BeginPaint(hwnd, &amp;ps);
          EndPaint(hwnd, &amp;ps);
          break;
      default:
          return DefWindowProc(hwnd, uMsg, wParam, lParam);
      }
      return 0;
  }
  </code></pre>