这几天一直在看计算机方面的书,准备考试软件工程师,但是看了看书,太恐怖了,很多我都不会,但是里面提醒了一个内容就是必须会使用一种语言(c++,java等)来编写程序。刚好今天看了一个杂志,里面有一片文章很好,我就开始学习了,所以就写了仿效写了下面的内容。
该工具是用来判断你是否具有管理员权限的工具,代码我就贴在这里。
#include <stdio.h>
#include <windows.h>
BOOL IsAdmin(void)
{
HANDLE hAccessToken;
BYTE *InfoBuffer;
PTOKEN_GROUPS ptgGroups;
DWORD dwInfoBufferSize;
PSID psidAdministrators;
SID_IDENTIFIER_AUTHORITY sidNtAuthority=SECURITY_NT_AUTHORITY;
UINT i;
BOOL bRet=FALSE;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken))
InfoBuffer=new BYTE[1024];
if(!InfoBuffer)
goto cleanup;
bRet=GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,1024,&dwInfoBufferSize);
CloseHandle(hAccessToken);
if(!bRet)
goto cleanup;
if(!AllocateAndInitializeSid(&sidNtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0,0,0,0,0,0,
&psidAdministrators));
goto cleanup;
bRet=FALSE;
ptgGroups=(PTOKEN_GROUPS)InfoBuffer;
for(i=0;i < ptgGroups-> GroupCount;i++)
{
if(EqualSid(psidAdministrators,ptgGroups->Groups[i].Sid))
{
bRet=TRUE;
break;
}
}
FreeSid(psidAdministrators);
cleanup:
if(InfoBuffer)
delete InfoBuffer;
return bRet;
}
main()
{
printf("+------------------------------------+\n");
printf("| ChkAdinistrator v0.1 |\n");
printf("| Write By AloneSword |\n");
printf("| 2005-9-14 |\n");
printf("| thx to sinister |\n");
printf("| http://blog.csdn.net/alonesword/ |\n");
printf("+-----------------------------------+\n");
if(IsAdmin) printf("You have got the Administrators rights.\n");
else printf("Sorry,administrator is not you.\n");
}