VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
使用API调用Windows“注销/重启/关机”功能

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站


主页:http://www.maxss.net
邮件:[email protected]


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



相关文章

相关软件