• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 21次 | 进入软件测试论坛讨论

领测软件测试网


主页:
邮件:


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

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网