System Shutdown Messages The following messages are used with system shutdown. WM_ENDSESSION WM_QUERYENDSESSION 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 UINT uMsg, // WM_QUERYENDSESSION WPARAM wParam, // not used LPARAM lParam // logoff option ); Return Values If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. Windows NT/2000: 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: After all applications return TRUE for this message, they receive the WM_ENDSESSION and they are terminated.
保你满意的答案:自动关机: ::ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,NULL); //关机在NT或2000下,需在此之前得到一定的权限方可: OSVERSIONINFO osinfo; osinfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); ::GetVersionEx(&osinfo); if(osinfo.dwPlatformId==VER_PLATFORM_WIN32_NT) {//系统是NT,则有必要获取关机权限 HANDLE hToken; TOKEN_PRIVILEGES tkp; //SeShutDownPrivilege must be enabled if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) return ; //Get a LUID for SeShutdownPrivilege LookupPrivilegeValue(NULL,TEXT("SeShutdownPrivilege"), &tkp.Privileges[0].Luid); //PrivilegeCount enables more than one privilege to bo set tkp.PrivilegeCount=1; tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken,false,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0); //确定AdjustTokenPrivileges执行是否成功。 if (GetLastError() != ERROR_SUCCESS) AfxMessageBox("权限调整失败."); } 2. 如何知道要关机了? BOOL CYourApp::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message==WM_QUERYENDSESSION||pMsg->message==WM_ENDSESSION) { AfxMessageBox("要关机了,请作好准备"); return CWinApp::PreTranslateMessage(pMsg); }
上面最后一段代码拷贝时出了点小错:BOOL CYourApp::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message==WM_QUERYENDSESSION¦¦pMsg->message==WM_ENDSESSION) { AfxMessageBox("要关机了,请作好准备"); }//if return CWinApp::PreTranslateMessage(pMsg); }//PreTranslateMessage
The following messages are used with system shutdown. WM_ENDSESSION
WM_QUERYENDSESSION
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
UINT uMsg, // WM_QUERYENDSESSION
WPARAM wParam, // not used
LPARAM lParam // logoff option
);
Return Values
If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. Windows NT/2000: 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: After all applications return TRUE for this message, they receive the WM_ENDSESSION and they are terminated.
它首先发出WM_QUERYENDSESSION 消息,返回的结果用于设置WM_ENDSESSION消息
如果所有的程序都关闭了,则执行关机
也可以在这个函数中使用EWX_FORCE参数,则系统不会发出WM_QUERYENDSESSION
或者WM_ENDSESSION消息,而直接关机。
CWnd类中已经封装该两个消息,查一下MSDN
MFC,使用消息映射,在WizardBar || ClassWizard
急>>>>>>>>>>>>>>>如何实现直接关机???????????而不是调出"关闭Windows"对话框
急>>>>>>>>>>>>>>>如何实现直接关机???????????而不是调出"关闭Windows"对话框
急>>>>>>>>>>>>>>>如何实现直接关机???????????而不是调出"关闭Windows"对话框
::ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,NULL); //关机在NT或2000下,需在此之前得到一定的权限方可:
OSVERSIONINFO osinfo;
osinfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
::GetVersionEx(&osinfo);
if(osinfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
{//系统是NT,则有必要获取关机权限
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
//SeShutDownPrivilege must be enabled
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
return ;
//Get a LUID for SeShutdownPrivilege
LookupPrivilegeValue(NULL,TEXT("SeShutdownPrivilege"),
&tkp.Privileges[0].Luid); //PrivilegeCount enables more than one privilege to bo set
tkp.PrivilegeCount=1;
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken,false,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
//确定AdjustTokenPrivileges执行是否成功。
if (GetLastError() != ERROR_SUCCESS)
AfxMessageBox("权限调整失败.");
}
2. 如何知道要关机了?
BOOL CYourApp::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_QUERYENDSESSION||pMsg->message==WM_ENDSESSION)
{
AfxMessageBox("要关机了,请作好准备");
return CWinApp::PreTranslateMessage(pMsg);
}
如果使用SDK,则在那个处理消息的switch里面添加这两个消息的处理。
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_QUERYENDSESSION¦¦pMsg->message==WM_ENDSESSION)
{
AfxMessageBox("要关机了,请作好准备");
}//if
return CWinApp::PreTranslateMessage(pMsg);
}//PreTranslateMessage
我用的是delphi所以可能不好做到两个消息同时比较,但是你的方法,我会参考的,我试通了就给你加分!
if(pMsg->message==WM_QUERYENDSESSION¦¦pMsg->message==WM_ENDSESSION)也就是说,只要判断到一个消息就行了。
我不相信了,if(pMsg->message==WM_QUERYENDSESSION||pMsg->message==WM_ENDSESSION)
很简单嘛!
用程序说话吧,在关机时程序会详细告诉你问题答案(附源码)
http://www.csdn.net/filebbs/read_topic.asp?id=528
56K