欢迎访问:常州市武进区嘉泽中心小学网站 !今天是:
栏目列表
您现在的位置是:首页>>教师>>计算机技术>>程序设计>>杂项>>文章内容
使用API调用Windows“注销/重启/关机”功能
发布时间:2008-11-20   点击:   来源:本站原创   录入者:佚名
 


主页:
邮件:maxchou@{域名已经过期}


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

附件:
    关闭窗口
    打印文档
    账号登录
    保持登录 忘记密码?
    账号与武进教师培训平台同步