码迷,mamicode.com
首页 > Windows程序 > 详细

Windows判断是否为管理员

时间:2014-09-15 12:48:28      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   div   log   on   c   

// IsUserAdminTest.cpp : Defines the entry point for the console application.
//

#include <Windows.h>
#include <tchar.h>
#include <iostream>
BOOL IsUserAdmin();

int _tmain(int argc, _TCHAR* argv[])
{
	printf_s("%d\n",IsUserAdmin());
	return 0;
}

BOOL IsUserAdmin()
{
	BOOL b = FALSE;
	SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
	PSID AdministratorsGroup = NULL; 
	b = AllocateAndInitializeSid(
		&NtAuthority,
		2,
		SECURITY_BUILTIN_DOMAIN_RID,
		DOMAIN_ALIAS_RID_ADMINS,
		0, 0, 0, 0, 0, 0,
		&AdministratorsGroup); 
	if(b) 
	{
		HANDLE hThreadUserToken = NULL;
		if (!::OpenThreadToken(::GetCurrentThread(), 
			TOKEN_READ,//must have TOKEN_QUERY
			false,
			&hThreadUserToken))
		{
			if (!CheckTokenMembership( hThreadUserToken, AdministratorsGroup, &b)) 
			{
				b = FALSE;
			}
			if (AdministratorsGroup)
			{
				FreeSid(AdministratorsGroup); 
			}
		}
		if (hThreadUserToken)
		{
			CloseHandle(hThreadUserToken);
		}
	}
	return(b);
}

  

Windows判断是否为管理员

标签:blog   io   os   ar   for   div   log   on   c   

原文地址:http://www.cnblogs.com/2012harry/p/3972468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!