if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
//
//ExitWindowsEx(EWX_FORCE|EWX_REBOOT,0)是重启。
//ExitWindowsEx(EWX_FORCE|EWX_LOGOFF,0);是注销
// EWX_FORCE参数是指强行关闭所有程序,不会弹出任何对话框。
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_POWEROFF, 0))
return FALSE;
int main()
{
char msg[256],cname[256];
int timeout;
bool ret;
printf("1----Shut down local computer.\n2----Reboot local computer.\n");
printf("3----Shut down remote computer.\n4----Reboot remote computer.\n\n");
switch(getch())
{
case '1':
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(NULL,SE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(NULL,msg,timeout,TRUE,FALSE);
//五个参数分别是:1-计算机名,
// 2-提示消息,
// 3-从函数执行到关机的时间(单位是秒),
// 4-是否强制关闭应用程序,
// 5-关机还是重启
break;
case '2':
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(NULL,SE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(NULL,msg,timeout,TRUE,TRUE);
break;
case '3':
printf("Input the remote computer name:");
scanf("%s",cname);
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(cname,SE_REMOTE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(cname,msg,timeout,TRUE,FALSE);
break;
case '4':
printf("Input the remote computer name:");
scanf("%s",cname);
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(cname,SE_REMOTE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(cname,msg,timeout,TRUE,TRUE);
break;
default:
break;
}
if(ret)
printf("\nSuccess!\n");
else
printf("\nFail.\n");
return ret;
}
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Windows NT: The ExitWindows function asks applications if they want to terminate by sending the WM_QUERYENDSESSION message to the main window of all running applications.
Windows 95: The ExitWindows function sends the WM_QUERYENDSESSION message to all applications except the one that called ExitWindows.
An application agrees to terminate by returning TRUE when it receives this message (or by allowing the DefWindowProc function to process the message). If any application returns FALSE when it receives the WM_QUERYENDSESSION message, the shutdown is canceled.
After Windows processes the results of the WM_QUERYENDSESSION message, it sends the WM_ENDSESSION message with the wParam parameter set to TRUE if the system is shutting down and to FALSE if it is not.
Windows does not allow new applications to start up during the shutdown process.