依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.
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
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); //重启
……
……
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);
可以读取注册表
"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.
继续等答案