1、Window关机函数ExitWindowsEx详解Window关机函数ExitWindowsEx详解 “系统ShutDown”属于Windows系统的一种基本服务。功能上有“关闭系统”,“注销用户”,“锁定工作站”3中操作。SDK中提供了几个函数,来对此服务进行调用。“关闭系统”功能使计算机可以被安全的关闭。所有在文件系统里缓冲的内容都被强制写入磁盘。然后,显示相应的对话框,提示用户计算机将被关闭或者已经准备好被关闭。可选的情况一般是计算机在关闭后重起,而不是直接切断电源。如果一个进程调用“注销”功能函数,则该进程所在的安全环境范围内的所有进程都被终止,使当前的用户退出系统。一个登陆对话框被显
2、示,期待新用户的登陆。“锁定工作站”功能使你可以在离开计算机的时候,保护计算机屏幕不被未授权的用户看到。要解除锁定,必须用管理员或着授权用户的帐号和密码重新登陆。如何关闭系统: 程序可以用两种方式关闭本地或远程计算机 直接关闭系统 关闭系统并重启 Windows NT/2000 及后续版本: 程序必须拥有SE_SHUTDOWN_NAME权限才能成功调用关闭函数。 ExitWindowsEx函数可以用来关闭系统。如函数成功调用,系统对每个窗口发送WM_QUERYENDSESSION 消息,询问窗口所属的程序是否可以被终止。收到此消息的程序应该进行响应,清除环境释放资源,然后返回TRUE表示自己可
3、以被终止。然而调用ExitWindowEx的时候如果指定了EXW_FORCE,则系统强行终止相关的进程并关闭,这样可能导致数据的丢失。 这是一段在NT/2000中调用ExitWindowEx关闭系统的程序(强制关闭所有程序)。在windows95/98/me中直接调用ExitWindowEx即可。 - HANDLE hToken; TOKEN_PRIVILEGES tkp; / Get a token for this process. (得到得到进程的令牌句柄-可参阅AdjustTokenPrivileges(进程权限))if (!OpenProcessToken(GetCurrentPro
4、cess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) Error(OpenProcessToken); / Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges0.Luid); / (查询进程的权限-可参阅AdjustTokenPrivileges(进程权限)tkp.PrivilegeCount = 1; / one privilege to set tkp.Privileges0.Att
5、ributes = SE_PRIVILEGE_ENABLED; / Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); /调整令牌权限-可参阅AdjustTokenPrivileges(进程权限/ Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) error(AdjustTok
6、enPrivileges); / Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0) error(ExitWindowsEx);-Windows NT/2000以及后续版本: InitiateSystemShutdown函数可以指定一段延时,在进行延时计数的时候,在将被关闭的目标计算机上显示一个对话框,提示用户尽快注销。一旦计数结束,系统则立刻被关闭。在此之前,可以调用AbortSystemShutdown函数停止计数,取消相应的关闭操
7、作。InitiateSystemShutdown也可以指定让系统重启。InitiateSystemShutdown有一个参数LPTSTR lpMachineName,可以指定为网络上的计算机名字,也就是说,可以关闭网络上的他计算机(如果你的用户在该计算机上有足够的权限的话)。 以下这个例子调用InitiateSystemShutdown函数关闭用户已经登陆的本地计算机(要关闭远程计算机将InitSystemShutdown第一个参数由NULL改为正确的计算机名字或)。同样的,也需要先获得SE_SHUTDOWN_NAME权限。 - HANDLE hToken; / handle to proce
8、ss token TOKEN_PRIVILEGES tkp; / pointer to token structure BOOL fResult; / system shutdown flag / Get the current process token handle so we can get shutdown / privilege. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) ErrorHandler(OpenProcessToken failed.
9、); / Get the LUID for shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges0.Luid); tkp.PrivilegeCount = 1; / one privilege to set tkp.Privileges0.Attributes = SE_PRIVILEGE_ENABLED; / Get shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (P
10、TOKEN_PRIVILEGES) NULL, 0); / Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) ErrorHandler(AdjustTokenPrivileges enable failed.); / Display the shutdown dialog box and start the time-out countdown. fResult = InitiateSystemShutdown( NULL, / shut down local
11、computer Click on the main window and press the Escape key to cancel shutdown., / message to user 20, / time-out period FALSE, / ask user to close apps TRUE); / reboot after shutdown if (!fResult) ErrorHandler(InitiateSystemShutdown failed.); / Disable shutdown privilege. tkp.Privileges0.Attributes
12、= 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); if (GetLastError() != ERROR_SUCCESS) ErrorHandler(AdjustTokenPrivileges disable failed.); - 而使用AbortSystemShutoown取消InitialSystemShutdown操作的代码如下(记住要在延时结束前执行才能起作用 / Get the current process token handle so we can get shut
13、down / privilege. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) ErrorHandler(OpenProcessToken failed.); / Get the LUID for shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges0.Luid); tkp.PrivilegeCount = 1; / one privilege to
14、set tkp.Privileges0.Attributes = SE_PRIVILEGE_ENABLED; / Get shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); / Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) ErrorHandler(AdjustTokenPrivileges enable failed.); / Prevent the system from s
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1