<p>I want to debug a windows C++ application I've written to see why it isn't responding to WM_QUERYENDSESSION how I expect it to. Clearly it's a little tricky to do this by just shutting the system down. Is there any utility or code which I can use to send a fake WM_QUERYENDSESSION to my application windows myself?</p>

[{'answer_id': 77208, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 0, 'selected': False, 'text': "<p>Yes. If you can get the window handle (maybe using FindWindow()), you can send/post any message to it as long as the WPARAM &amp; LPARAM aren't pointers.</p>\n"}, {'answer_id': 77210, 'author': 'ChrisN', 'author_id': 3853, 'author_profile': 'https://Stackoverflow.com/users/3853', 'pm_score': 2, 'selected': True, 'text': '<p>I\'ve used the <a href="http://winguitest.sourceforge.net" rel="nofollow noreferrer">Win32::GuiTest</a> Perl module to do this kind of thing in the past.</p>\n'}, {'answer_id': 77218, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The Windows API SendMessage can be used to do this.\n<a href="http://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspx</a></p>\n\n<p>IS ti possible it\'s not responding because some other running process has responded with a zero (making the system wait on it.)</p>\n'}, {'answer_id': 77496, 'author': 'Bob Moore', 'author_id': 9368, 'author_profile': 'https://Stackoverflow.com/users/9368', 'pm_score': 1, 'selected': False, 'text': '<p>Yes of course, it possible. I faced a similar issue some months ago where some (unknown, but probably mine) app was preventing shutdown, so I wrote some quick code that used EnumWindows to enumerate all the top level windows, sent each one a WM_QUERYENDSESSION message, noted what the return value from SendMessage was and stopped the enumeration if anyone returned FALSE. Took about ten minutes in C++/MFC. This was the guts of it:</p>\n\n<pre><code>void CQes_testDlg::OnBtnTest()  \n{  \n   // enumerate all the top-level windows.  \n   m_ctrl_ListMsgs.ResetContent();  \n   EnumWindows (EnumProc, 0);  \n}  \n\n\nBOOL CALLBACK EnumProc (HWND hTarget, LPARAM lParam)  \n{  \n   CString csTitle;  \n   CString csMsg;  \n   CWnd *  pWnd = CWnd::FromHandle (hTarget);  \n   BOOL    bRetVal = TRUE;  \n   DWORD   dwPID;  \n\n   if (pWnd)  \n   {  \n      pWnd-&gt;GetWindowText (csTitle);  \n      if (csTitle.GetLength() == 0)  \n      {  \n         GetWindowThreadProcessId (hTarget, &amp;dwPID);  \n         csTitle.Format ("&lt;PID=%d&gt;", dwPID);  \n      }  \n\n      if (pWnd-&gt;SendMessage (WM_QUERYENDSESSION, 0, ENDSESSION_LOGOFF))  \n      {  \n         csMsg.Format ("window 0x%X (%s) returned TRUE", hTarget, csTitle);  \n      }  \n      else   \n      {    \n         csMsg.Format ("window 0x%X (%s) returned FALSE", hTarget, csTitle);  \n         bRetVal = FALSE;  \n      }  \n\n      mg_pThis-&gt;m_ctrl_ListMsgs.AddString (csMsg);\n   }\n   else  \n   {  \n      csMsg.Format ("Unable to resolve HWND 0x%X to a CWnd", hTarget);  \n      mg_pThis-&gt;m_ctrl_ListMsgs.AddString (csMsg);  \n   }  \n   return bRetVal;  \n}\n</code></pre>\n\n<p>mg_pThis was just a local copy of the dialog\'s this pointer, so the helper callback could access it. I told you it was quick and dirty :-)</p>\n'}]
['https://Stackoverflow.com/questions/77133', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1849/']