Window关机函数ExitWindowsEx详解.docx
《Window关机函数ExitWindowsEx详解.docx》由会员分享,可在线阅读,更多相关《Window关机函数ExitWindowsEx详解.docx(8页珍藏版)》请在冰豆网上搜索。
![Window关机函数ExitWindowsEx详解.docx](https://file1.bdocx.com/fileroot1/2022-11/17/790134a9-e6e8-4513-be50-2743df3143b7/790134a9-e6e8-4513-be50-2743df3143b71.gif)
Window关机函数ExitWindowsEx详解
Window关机函数ExitWindowsEx详解
“系统ShutDown”属于Windows系统的一种基本服务。
功能上有“关闭系统”,“注销用户”,“锁定工作站”3中操作。
SDK中提供了几个函数,来对此服务进行调用。
“关闭系统”功能使计算机可以被安全的关闭。
所有在文件系统里缓冲的内容都被强制写入磁盘。
然后,显示相应的对话框,提示用户计算机将被关闭或者已经准备好被关闭。
可选的情况一般是计算机在关闭后重起,而不是直接切断电源。
如果一个进程调用“注销”功能函数,则该进程所在的安全环境范围内的所有进程都被终止,使当前的用户退出系统。
一个登陆对话框被显示,期待新用户的登陆。
“锁定工作站”功能使你可以在离开计算机的时候,保护计算机屏幕不被未授权的用户看到。
要解除锁定,必须用管理员或着授权用户的帐号和密码重新登陆。
如何关闭系统:
程序可以用两种方式关闭本地或远程计算机
直接关闭系统
关闭系统并重启
WindowsNT/2000及后续版本:
程序必须拥有SE_SHUTDOWN_NAME权限才能成功调用关闭函数。
ExitWindowsEx函数可以用来关闭系统。
如函数成功调用,系统对每个窗口发送WM_QUERYENDSESSION消息,询问窗口所属的程序是否可以被终止。
收到此消息的程序应该进行响应,清除环境释放资源,然后返回TRUE表示自己可以被终止。
然而调用ExitWindowEx的时候如果指定了EXW_FORCE,则系统强行终止相关的进程并关闭,这样可能导致数据的丢失。
这是一段在NT/2000中调用ExitWindowEx关闭系统的程序(强制关闭所有程序)。
在windows95/98/me中直接调用ExitWindowEx即可。
-----------------------------------------------------------------------------------------------------------
HANDLEhToken;
TOKEN_PRIVILEGEStkp;
//Getatokenforthisprocess.(得到得到进程的令牌句柄------可参阅AdjustTokenPrivileges(进程权限))
if(!
OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
Error("OpenProcessToken");
//GettheLUIDfortheshutdownprivilege.
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);//(查询进程的权限-------可参阅AdjustTokenPrivileges(进程权限)
tkp.PrivilegeCount=1;//oneprivilegetoset
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
//Gettheshutdownprivilegeforthisprocess.
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);//调整令牌权限-------可参阅AdjustTokenPrivileges(进程权限
//CannottestthereturnvalueofAdjustTokenPrivileges.
if(GetLastError()!
=ERROR_SUCCESS)
error("AdjustTokenPrivileges");
//Shutdownthesystemandforceallapplicationstoclose.
if(!
ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0))
error("ExitWindowsEx");
-----------------------------------------------------------------------------------------------------------
WindowsNT/2000以及后续版本:
InitiateSystemShutdown函数可以指定一段延时,在进行延时计数的时候,在将被关闭的目标计算机上显示一个对话框,提示用户尽快注销。
一旦计数结束,系统则立刻被关闭。
在此之前,可以调用AbortSystemShutdown函数停止计数,取消相应的关闭操作。
InitiateSystemShutdown也可以指定让系统重启。
InitiateSystemShutdown有一个参数LPTSTRlpMachineName,可以指定为网络上的计算机名字,也就是说,可以关闭网络上的他计算机(如果你的用户在该计算机上有足够的权限的话)。
以下这个例子调用InitiateSystemShutdown函数关闭用户已经登陆的本地计算机(要关闭远程计算机将InitSystemShutdown第一个参数由NULL改为正确的计算机名字或)。
同样的,也需要先获得SE_SHUTDOWN_NAME权限。
---------------------------------------------------------------------------------------------------------
HANDLEhToken;//handletoprocesstoken
TOKEN_PRIVILEGEStkp;//pointertotokenstructure
BOOLfResult;//systemshutdownflag
//Getthecurrentprocesstokenhandlesowecangetshutdown
//privilege.
if(!
OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
ErrorHandler("OpenProcessTokenfailed.");
//GettheLUIDforshutdownprivilege.
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount=1;//oneprivilegetoset
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
//Getshutdownprivilegeforthisprocess.
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,
(PTOKEN_PRIVILEGES)NULL,0);
//CannottestthereturnvalueofAdjustTokenPrivileges.
if(GetLastError()!
=ERROR_SUCCESS)
ErrorHandler("AdjustTokenPrivilegesenablefailed.");
//Displaytheshutdowndialogboxandstartthetime-outcountdown.
fResult=InitiateSystemShutdown(NULL,//shutdownlocalcomputer
"Clickonthemainwindowandpress\
theEscapekeytocancelshutdown.",//messagetouser
20,//time-outperiod
FALSE,//askusertocloseapps
TRUE);//rebootaftershutdown
if(!
fResult)
{
ErrorHandler("InitiateSystemShutdownfailed.");
}
//Disableshutdownprivilege.
tkp.Privileges[0].Attributes=0;
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,
(PTOKEN_PRIVILEGES)NULL,0);
if(GetLastError()!
=ERROR_SUCCESS)
{
ErrorHandler("AdjustTokenPrivilegesdisablefailed.");
}
---------------------------------------------------------------------------------------------
而使用AbortSystemShutoown取消InitialSystemShutdown操作的代码如下(记住要在延时结束前执行才能起作用
//Getthecurrentprocesstokenhandlesowecangetshutdown
//privilege.
if(!
OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
ErrorHandler("OpenProcessTokenfailed.");
}
//GettheLUIDforshutdownprivilege.
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount=1;//oneprivilegetoset
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
//Getshutdownprivilegeforthisprocess.
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,
(PTOKEN_PRIVILEGES)NULL,0);
//CannottestthereturnvalueofAdjustTokenPrivileges.
if(GetLastError()!
=ERROR_SUCCESS)
{
ErrorHandler("AdjustTokenPrivilegesenablefailed.");
}
//Preventthesystemfroms