关闭计算机就这几招(一) [转]

shanjuhua 2004-10-27 08:59:50
注:完全翻译整理自MSDN。非常简单. .

“系统ShutDown”属于Windows系统的一种基本服务。功能上有“关闭系统”,“注销用户”,“锁定工作站”3中操作。SDK中提供了几个函数,来对此服务进行调用。

“关闭系统”功能使计算机可以被安全的关闭。所有在文件系统里缓冲的内容都被强制写入磁盘。然后,显示相应的对话框,提示用户计算机将被关闭或者已经准备好被关闭。可选的情况一般是计算机在关闭后重起,而不是直接切断电源。

如果一个进程调用“注销”功能函数,则该进程所在的安全环境范围内的所有进程都被终止,使当前的用户退出系统。一个登陆对话框被显示,期待新用户的登陆。

“锁定工作站”功能使你可以在离开计算机的时候,保护计算机屏幕不被未授权的用户看到。要解除锁定,必须用管理员或着授权用户的帐号和密码重新登陆。

如何关闭系统:

程序可以用两种方式关闭本地或远程计算机
直接关闭系统
关闭系统并重启
Windows NT/2000 及后续版本: 程序必须拥有SE_SHUTDOWN_NAME权限才能成功调用关闭函数。


ExitWindowsEx函数可以用来关闭系统。如函数成功调用,系统对每个窗口发送WM_QUERYENDSESSION 消息,询问窗口所属的程序是否可以被终止。收到此消息的程序应该进行响应,清除环境释放资源,然后返回TRUE表示自己可以被终止。然而调用ExitWindowEx的时候如果指定了EXW_FORCE,则系统强行终止相关的进程并关闭,这样可能导致数据的丢失。

这是一段在NT/2000中调用ExitWindowEx关闭系统的程序(强制关闭所有程序)。

在windows95/98/me中直接调用ExitWindowEx即可。
-----------------------------------------------------------------------------------------------------------


HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.

if (!OpenProcessToken(GetCurrentProcess(),

TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))


Error("OpenProcessToken");

// Get the LUID for the shutdown privilege.

LookupPrivilegevalue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set

tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the 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)

error("AdjustTokenPrivileges");

// Shut down the system and force all applications to close.

if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))

error("ExitWindowsEx");

-----------------------------------------------------------------------------------------------------------

Windows NT/2000以及后续版本:

InitiateSystemShutdown函数可以指定一段延时,在进行延时计数的时候,在将被关闭的目标计算机上显示一个对话框,提示用户尽快注销。一旦计数结束,系统则立刻被关闭。在此之前,可以调用AbortSystemShutdown函数停止计数,取消相应的关闭操作。InitiateSystemShutdown也可以指定让系统重启。


InitiateSystemShutdown有一个参数LPTSTR lpMachineName,可以指定为网络上的计算机名字,也就是说,可以关闭网络上的他计算机(如果你的用户在该计算机上有足够的权限的话)。


以下这个例子调用InitiateSystemShutdown函数关闭用户已经登陆的本地计算机(要关闭远程计算机将InitSystemShutdown第一个参数由NULL改为正确的计算机名字或)。同样的,也需要先获得SE_SHUTDOWN_NAME权限。


---------------------------------------------------------------------------------------------------------

HANDLE hToken; // handle to process 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.");

// Get the LUID for shutdown privilege.

LookupPrivilegevalue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set

tkp.Privileges[0].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.");

// Display the shutdown dialog box and start the time-out countdown.

fResult = InitiateSystemShutdown( NULL, // shut down local 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.Privileges[0].Attributes = 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 shutdown

// 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.Privileges[0].Luid);


tkp.PrivilegeCount = 1; // one privilege to set


tkp.Privileges[0].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 shutting down.

fResult = AbortSystemShutdown(NULL);

if (!fResult)

{

ErrorHandler("AbortSystemShutdown failed.");

}

// Disable shutdown privilege.

tkp.Privileges[0].Attributes = 0;

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,

(PTOKEN_PRIVILEGES) NULL, 0);

if (GetLastError() != ERROR_SUCCESS)

{

ErrorHandler("AdjustTokenPrivileges disable failed.");

}

break;

关于用户权限(Privileges)的详细信息,可以参见MSDN

...全文
163 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
shanjuhua 2004-10-27
  • 打赏
  • 举报
回复
..

2,748

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧