title: winapi - GetMessage() crashing when is called from QObject derived class
body: <p>I have a sample of code taken from <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363432.aspx" rel="nofollow noreferrer">Registering for Device Notification</a> to detect if a USB device is removed. This code uses the Win32 API and I have built and test it with success.</p>
  
  <p>When I try to integrate this functionality into a <code>QObject</code>-derived class, I'm getting a crash in the <code>MessagePump()</code> method: </p>
  
  <pre><code>void QObjectDerivedClass::MessagePump() {
      MSG message;
      int retVal;
  
      if (!m_hWnd) {
          return;
      }
  
      qDebug() &lt;&lt; Q_FUNC_INFO;
  
      // Get all messages for any window that belongs to this thread,
      // without any filtering. Potential optimization could be
      // obtained via use of filter values if desired.
  
      while ((retVal = GetMessage(&amp;message, m_hWnd,
                               MSG_FILTER_MIN, MSG_FILTER_MAX)) != 0) {
          if (retVal == -1) {
              break;
          } else {
              TranslateMessage(&amp;message);
              DispatchMessage(&amp;message);
          }
      }
  }
  </code></pre>
  
  <p>As you can imagine, I have to modify the <code>WndProc()</code> callback in the example to use a static member for this class and satisfy <code>WNDCLASS</code> object as follow:</p>
  
  <pre><code>BOOL QObjectDerivedClass::InitWindowClass() {
      WNDCLASSEX wndClass;/* = {0};*/
  
      wndClass.cbSize = sizeof(WNDCLASSEX);
      wndClass.style = 0;
      wndClass.hInstance = reinterpret_cast&lt;HINSTANCE&gt;(GetModuleHandle(0));
      // WndProcThunk is an static member of QObjectDerivedClass
      wndClass.lpfnWndProc = reinterpret_cast&lt;WNDPROC&gt;(WndProcThunk);
      wndClass.cbClsExtra = 0;
      wndClass.cbWndExtra = 0;
      wndClass.hIcon = NULL;
      wndClass.hIconSm = NULL;
      wndClass.hbrBackground = NULL;
      wndClass.hCursor = NULL;
      wndClass.lpszClassName = WND_CLASS_NAME;
      wndClass.lpszMenuName = NULL;
  
      if (!RegisterClassEx(&amp;wndClass)) {
          qDebug() &lt;&lt; Q_FUNC_INFO &lt;&lt; "Unable to register window class. Error:"
               &lt;&lt; QString::number(GetLastError());
  
          return FALSE;
      }
  
      m_instance = wndClass.hInstance;
  
      return TRUE;
  }
  </code></pre>
  
  <p>The call stack when the application crashes is:</p>
  
  <pre>
  1                                                                0xd26128   
  2  SetManipulationInputTarget             USER32                 0x7709d2b3 
  3  DispatchMessageW                       USER32                 0x7707e88a 
  4  DispatchMessageW                       USER32                 0x7707e4c0 
  5  RealGetWindowClassW                    USER32                 0x7708a64f 
  6  KiUserCallbackDispatcher               ntdll                  0x772e08c6 
  7  QObjectDerivedClass::MessagePump       qobjectderived.cpp 165 0xa52e88   
  8  QObjectDerivedClass::Start             qobjectderived.cpp 346 0xa52bc2   
  9  wWinMain                               main.cpp           259 0xa525ff   
  10 invoke_main                            exe_common.inl     118 0xa5516e   
  11 __scrt_common_main_seh                 exe_common.inl     253 0xa54fd0   
  12 __scrt_common_main                     exe_common.inl     296 0xa54e6d   
  13 wWinMainCRTStartup                     exe_wwinmain.cpp   17  0xa55188   
  14 BaseThreadInitThunk                    KERNEL32               0x73d862c4 
  15 RtlSubscribeWnfStateChangeNotification ntdll                  0x772d0fd9 
  16 RtlSubscribeWnfStateChangeNotification ntdll                  0x772d0fa4
  </pre>
  
  <p>Any idea how to solve this crash? </p>
  
  <hr>
  
  <p>EDIT: Posted standard output:</p>
  
  <pre>
  int __stdcall wWinMain(struct HINSTANCE__ *,struct HINSTANCE__ *,wchar_t *,int)
  int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long)
  int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long)
  int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long)
  int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long)
  void __thiscall QObjectDerivedClass::MessagePump(void)
  </pre>