请问各位高手,能不能通过截获API函数ExitWindowsEx的方式获知windows关机还是重启?是不是所有的关机或重启等操作都调用ExitWindowsEx函数?或者还有其它函数?通过消息WM_QUERYENDSESSION是不能区分关机与重启的,我以前也在CSDN上问过,当时的结论是不能区分。如果用截获API函数法能区分的话,烦请试一下。多谢!

解决方案 »

  1.   

    既然是拦截这个api 函数,那么当然去看一下相关的拦截api的资料了。当你拦截了api,看看这个函数的参数不就可以区别了吗
      

  2.   

    那么可能不需要拦截这个 api函数,是不是可以拦截消息了,因为windows重起或关机的时候应该 会给每个进程发送一条消息通知他们,具体什么消息呢,不好意思让我查查资料,呵呵
      

  3.   

    BOOL ExitWindowsEx(
      UINT uFlags,       // shutdown operation
      DWORD dwReserved   // reserved
    );
    uFlags 
    [in] Specifies the type of shutdown. This parameter must include one of the following values. Value Meaning 
    EWX_LOGOFF Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off. 
    EWX_POWEROFF Shuts down the system and turns off the power. The system must support the power-off feature. 
    Windows NT/2000: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section. 
     
    EWX_REBOOT Shuts down the system and then restarts the system. 
    Windows NT/2000: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section. 
     
    EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. 
    Windows NT/2000: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section. 
     
    This parameter can optionally include the following values. Value Meaning 
    EWX_FORCE Forces processes to terminate. When this flag is set, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency. 
    EWX_FORCEIFHUNG Windows 2000: Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message. This flag is ignored if EWX_FORCE is used. 
      

  4.   

    litsnake1(litsnake) :windows重起或关机的时候的确会给每个进程发送一条消息通知他们,是 WM_QUERYENDSESSION 以及WM_ENDSESSION,但当关机采用强制模式即调用ExitWindowsEx时使用EWX_FORCE参数,系统便不会发这两条消息。
    再者:即使系统发了消息,从消息的参数中也只能区分出“注销”来,关机和重启是区分不出来的,因为两者给的参数都一样,我试过。所以依我看还得截获API。
      

  5.   

    关机可以通过shell32.dll中的一个索引号为60的API函数调用来实现。
    typedef int (CALLBACK *SHUTDOWNDLG) int;//显示关机对话框的指针HINSTANCE hInst = LoadLibrary("shell32.dll");
    SHUTDOWNDLG ShutDownDialog;
    if(hInst!=NULL)
    {
        ShutDownDialog = (SHUTDOWNDLG) GetProcAddress(hInst,(LPSTR)60);
        (*ShutDownDialog(0));
    }
      

  6.   

    这个问题,也困扰了我N久,希望楼主做完之后,也给我发一份,thx.
    [email protected]
      

  7.   

    还有一个函数可以关机。
    InitiateSystemShutdown(Ex)
      

  8.   

    截取API函数取得了一定进展,但还存在一定问题,win98下操作异常,会出错。