16,472
社区成员
发帖
与我相关
我的任务
分享
#pragma comment(lib,"advapi32")
#include <windows.h>
#include <stdio.h>
BOOL IsAdmin(void) {
HANDLE hAccessToken;
BYTE *InfoBuffer=NULL;
PTOKEN_GROUPS ptgGroups;
DWORD dwInfoBufferSize;
PSID psidAdministrators;
SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
UINT i;
BOOL bRet = FALSE;
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hAccessToken)) goto cleanup;
InfoBuffer = new BYTE[1024];
if (!InfoBuffer) goto cleanup;
bRet = GetTokenInformation(hAccessToken,
TokenGroups,
InfoBuffer,
1024,
&dwInfoBufferSize);
CloseHandle( hAccessToken );
if (!bRet) goto cleanup;
if (!AllocateAndInitializeSid(&siaNtAuthority,
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;
}
int main() {
if (IsAdmin()) {printf("IsAdmin - TRUE\n");return 0;}
else {printf("IsAdmin - FALSE\n");return 1;}
}
SC_HANDLE schSCManager = OpenSCManager(
NULL,
NULL,
SC_MANAGER_CREATE_SERVICE
);
if (schSCManager == NULL)
{
//非管理员
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentityversion="1.0.0.0" processorArchitecture="X86" name="VistaLogoDemo" type="win32"/>
<description>Description of your application</description>
<!-- Identify the application security requirements.-->
<trustInfoxmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevellevel="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>