Is it possible to send WM_QUERYENDSESSION messages to a window in a different process?

后端 未结 4 365
太阳男子
太阳男子 2021-01-20 00:16

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

相关标签:
4条回答
  • 2021-01-20 00:43

    I've used the Win32::GuiTest Perl module to do this kind of thing in the past.

    0 讨论(0)
  • 2021-01-20 00:46

    The Windows API SendMessage can be used to do this. http://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspx

    IS ti possible it's not responding because some other running process has responded with a zero (making the system wait on it.)

    0 讨论(0)
  • 2021-01-20 01:02

    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:

    void CQes_testDlg::OnBtnTest()  
    {  
       // enumerate all the top-level windows.  
       m_ctrl_ListMsgs.ResetContent();  
       EnumWindows (EnumProc, 0);  
    }  
    
    
    BOOL CALLBACK EnumProc (HWND hTarget, LPARAM lParam)  
    {  
       CString csTitle;  
       CString csMsg;  
       CWnd *  pWnd = CWnd::FromHandle (hTarget);  
       BOOL    bRetVal = TRUE;  
       DWORD   dwPID;  
    
       if (pWnd)  
       {  
          pWnd->GetWindowText (csTitle);  
          if (csTitle.GetLength() == 0)  
          {  
             GetWindowThreadProcessId (hTarget, &dwPID);  
             csTitle.Format ("<PID=%d>", dwPID);  
          }  
    
          if (pWnd->SendMessage (WM_QUERYENDSESSION, 0, ENDSESSION_LOGOFF))  
          {  
             csMsg.Format ("window 0x%X (%s) returned TRUE", hTarget, csTitle);  
          }  
          else   
          {    
             csMsg.Format ("window 0x%X (%s) returned FALSE", hTarget, csTitle);  
             bRetVal = FALSE;  
          }  
    
          mg_pThis->m_ctrl_ListMsgs.AddString (csMsg);
       }
       else  
       {  
          csMsg.Format ("Unable to resolve HWND 0x%X to a CWnd", hTarget);  
          mg_pThis->m_ctrl_ListMsgs.AddString (csMsg);  
       }  
       return bRetVal;  
    }
    

    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 :-)

    0 讨论(0)
  • 2021-01-20 01:03

    Yes. If you can get the window handle (maybe using FindWindow()), you can send/post any message to it as long as the WPARAM & LPARAM aren't pointers.

    0 讨论(0)
提交回复
热议问题