现在有两个应用程序app1和app2
app1得到app2的主窗体句柄后,用sendmessage或postmessage发送向app2发送
WM_QueryEndSession消息。app2可以响应WM_QueryEndSession消息。我的意图是通过发送WM_QueryEndSession让app2结束。但两个应用程序都失去了控制权。都不能继续运行了。
而且app2也并没有结束。两个程序都只能通过任务管理器关闭了。不知是什么原因。请各位帮忙
解决以下!谢谢!

解决方案 »

  1.   


    发个WM_CLOSE信息试试
      

  2.   

    楼主可以看看MSDN中WM_QueryEndSession的说明The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls the ExitWindows function. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero. After processing this message, the system sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message. A window receives this message through its WindowProc function. LRESULT CALLBACK WindowProc(
      HWND hwnd,          // handle to window
      WM_QUERYENDSESSION, // the message to send
      WPARAM wParam,      // not used
      LPARAM lParam       // logoff option
    );
    Parameters
    wParam 
    This parameter is reserved for future use. 
    lParam 
    Specifies whether the user is logging off or shutting down the system. If this parameter includes the ENDSESSION_LOGOFF value, the user if logging off. (Note that this parameter is a bit mask. To test for this value, use a bit-wise operation; do not test for equality.) 
    Windows 2000/XP: If this parameter is zero, the system is shutting down. Return Values
    If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. Res
    By default, the DefWindowProc function returns TRUE for this message. Windows NT/2000/XP: When an application returns TRUE for this message, it receives the WM_ENDSESSION message and it is terminated, regardless of how the other applications respond to the WM_QUERYENDSESSION message. Windows 95/98/Me: After all applications return TRUE for this message, they receive the WM_ENDSESSION and they are terminated. Requirements 
      Windows NT/2000/XP: Included in Windows NT 3.1 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Winuser.h; include Windows.h.See Also
    System Shutdown Overview, System Shutdown Messages, DefWindowProc, ExitWindows, WM_ENDSESSION 
      

  3.   

    The WM_CLOSE message is sent as a signal that a window or an application should terminate.The WM_QUIT message indicates a request to terminate an application and is generated when the application calls the PostQuitMessage function. It causes the GetMessage function to return zero. 
      

  4.   

    如果app2没有什么要保存的话,建议干脆用 TerminateProcess 把它干掉算了。
      

  5.   

    有数据要保存的。不能用TerminateProcess干掉!呵呵!
      

  6.   

    WM_QueryEndSession是来通知应用程序操作系统即将关闭,现在得赶快存储数据是通知性的,不是命令性的WM_CLOSE才是命令性的
      

  7.   

    哦.我试过了发送wm_close
    情况还是那样。两个程序都失去了控制权。
    是不是我关闭进程的方法有问题呀!
    应该如何让一个进程通知另一个进程自己结束呢?
      

  8.   

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Const WM_CLOSE = &H10Private Sub Command1_Click()
        Dim mhwnd As Long
        mhwnd = FindWindow("ThunderRT6FormDC", "form1")
        SendMessage mhwnd, WM_CLOSE, 0, 0
    End Sub