| 使用API调用Windows“注销/重启/关机”功能 |
|
使用API调用Windows“注销/重启/关机”功能 |
| 作者:佚名 来源:CSDN 作者: maxchou 更新:2006-8-25 21:05:35 错误报告 我要投稿 |
|
用过FlashGet的人应该都知道,在FlashGet中有一个下载完成后关闭计算机的功能,这种功能是如何实现的呢?其实通过调用一个Windows API函数ExitWindowsEx()就可以完成。但在Windows 98和Windows 2000实现稍有不同,下面将会续个进行介绍。
ExitWindowsEx()函数定义格式:
BOOL ExitWindowsEx(
UINT uFlags, // shutdown operation
DWORD dwReason // shutdown reason
); | 更详细的参数说明请参阅MSDN。
在Windows 98实现“注销/重启/关机”功能代码:
ExitWindowsEx(EWX_LOGOFF, 0); // 注销
ExitWindowsEx(EWX_REBOOT, 0); // 重启
ExitWindowsEx(EWX_SHUTDOWN, 0); // 关机
| 在Windows 2000实现“注销/重启/关机”功能代码:
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (FForce) // Forces processes to terminate
FFlag |= EWX_FORCE;
// Get version info to determine operation
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&osvi) == 0)
return false;
// Determine the platform
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
// Windows NT 3.51, Windows NT 4.0, Windows 2000,
// Windows XP, or Windows .NET Server
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
}
ExitWindowsEx(FFlag, 0); |
|
|
| 文章录入:skyuu 责任编辑:skyuu |
|
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |