有没有达人知道什么方法获得的关机事件是关机还是重起事件。

解决方案 »

  1.   

    关键是hook什么.要准确的,重启还是关机。
      

  2.   

    http://www.cnblogs.com/daoluanxiaozi/archive/2011/10/12/2208932.html
      

  3.   

    如果 WM_QUERYENDSESSION 能区分重启还是关机还有必要在这里问吗?
      

  4.   


    WM_QUERYENDSESSION  lParam 按位判别,定义如下#define ENDSESSION_LOGOFF    0x80000000
    #define EWX_LOGOFF          0
    #define EWX_SHUTDOWN        0x00000001
    #define EWX_REBOOT          0x00000002
    #define EWX_FORCE           0x00000004
    #define EWX_POWEROFF        0x00000008
    #define EWX_FORCEIFHUNG     0x00000010
      

  5.   


    static HANDLE hToken;
    static TOKEN_PRIVILEGES tp;
    static LUID luid;
    OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken); LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);
    ExitWindowsEx(EWX_POWEROFF,0);    //关机
    ExitWindowsEx(EWX_REBOOT,0);       //重启
      

  6.   

    static int USER_EVENT;
    ……
    ……
    switch(user_selection)
    {
    case:USER_EWX_POWEROFF
         USER_EVENT = EWX_POWEROFF;
         break;
    case:USER_EWX_REBOOT
         USER_EVENT = EWX_REBOOT;
         break;
    default:
         USER_EVENT = EWX_POWEROFF;
         break;
    }
    ExitWindowsEx(USER_EVENT ,0);
      

  7.   

    依MSDN解释,WM_QUERYENDSESSION 确实无法区分。
    可以读取注册表
    "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shutdown Setting" 下DWORD值,来确定用户在关机对话框时的选择
    如果没有此键,可新建一个,记住类型为DWORD。官方对此值解释如下:
    This entry stores the setting selected most recently from the list on the Shut Down Windows dialog box. This setting is displayed as the default the next time the dialog box appears.
    0x1
    Log off. Ends the session, leaving the computer running on full power.
    0x2
    Shut down. Ends the session and shuts down Windows 2000 so that the power can be turned off safely.
    0x4
    Restart. Ends the session, shuts down Windows 2000, and starts Windows 2000 again.
      

  8.   

    LS的方法98,2000的OK,不支持XP 2003以上的系统,早测试过了。多谢了.
    继续等答案