使用API调用Windows“注销/重启/关机”功能

王朝vc·作者佚名  2006-01-08
宽屏版  字体: |||超大  

主页:http://www.maxss.net

邮件:maxchou@163.com

用过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);

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有