Savoir si la session en cours est administrateur

cs_sephiro Messages postés 101 Date d'inscription dimanche 1 avril 2001 Statut Membre Dernière intervention 18 février 2008 - 6 juil. 2005 à 11:27
cs_sephiro Messages postés 101 Date d'inscription dimanche 1 avril 2001 Statut Membre Dernière intervention 18 février 2008 - 8 juil. 2005 à 20:57
Il est important pour l'un de mes programmes de savoir si l'utilisateur est admin ou non.
J'ai recherché sur ce site des informations sans rien trouver, j'ai fini par trouver une fonction qui marche sur plusieurs sites et forum.
Je l'ai testé sous Win XP Pro, avec 2 logins, l'un admin l'autre non. Elle retourne TRUe si administrateur. Je ne l'ai pas testé sous Win XP Home.
/////////////////////////////////////SOMMES NOUS ADMINISTRATEUR ///////////////////////////////
BOOL RunningAsAdministrator( VOID)
{
BOOL fAdmin;
HANDLE hThread;
TOKEN_GROUPS *ptg = NULL;
DWORD cbTokenGroups;
DWORD dwGroup;
PSID psidAdmin;
SID_IDENTIFIER_AUTHORITY SystemSidAuthority=
SECURITY_NT_AUTHORITY;


// First we must open a handle to the access token for this thread.
if ( !OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &hThread))
{
DWORD Error = GetLastError();
if ( Error == ERROR_NO_TOKEN)
{
// If the thread does not have an access token, we'll examine the
// access token associated with the process.
if (!OpenProcessToken ( GetCurrentProcess(), TOKEN_QUERY, &hThread))
return ( FALSE);
}
else
{//Phil: If not a Local Administrator, it seem Error=5
return ( FALSE);
}
}
// Then we must query the size of the group information associated with
// the token. Note that we expect a FALSE result from GetTokenInformation
// because we've given it a NULL buffer. On exit cbTokenGroups will tell
// the size of the group information.
if (GetTokenInformation ( hThread, TokenGroups, NULL, 0, &cbTokenGroups))
return ( FALSE);

// Here we verify that GetTokenInformation failed for lack of a large
// enough buffer.
if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return ( FALSE);

// Now we allocate a buffer for the group information.
// Since _alloca allocates on the stack, we don't have
// to explicitly deallocate it. That happens automatically
// when we exit this function.
if ( ! ( ptg = (TOKEN_GROUPS *) _alloca ( cbTokenGroups)))
return ( FALSE);

// Now we ask for the group information again.
// This may fail if an administrator has added this account
// to an additional group between our first call to
// GetTokenInformation and this one.
if ( !GetTokenInformation ( hThread, TokenGroups, ptg,cbTokenGroups, &cbTokenGroups) )
return ( FALSE);


// Now we must create a System Identifier for the Admin group.
if ( ! AllocateAndInitializeSid ( &SystemSidAuthority, 2,SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin) )
return (FALSE);


// Finally we'll iterate through the list of groups for this access
// token looking for a match against the SID we created above.
fAdmin= FALSE;
for ( dwGroup= 0; dwGroup < ptg->GroupCount; dwGroup++)
{ if ( EqualSid ( ptg->Groups[dwGroup].Sid, psidAdmin))
{ fAdmin = TRUE;
break;
}
}


// Before we exit we must explicity deallocate the SID we created.
FreeSid ( psidAdmin);



return ( fAdmin);


}

2 réponses

vecchio56 Messages postés 6535 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 22 août 2010 13
6 juil. 2005 à 18:57
Tu as un problème avec le code ou tu le donnes juste pour info?
0
cs_sephiro Messages postés 101 Date d'inscription dimanche 1 avril 2001 Statut Membre Dernière intervention 18 février 2008
8 juil. 2005 à 20:57
Aucun probleme, je le donne pour info
De plus je l'ai testé sur XP Home il marche
J'allais poser des questions sur ce probleme dans le forum et j'ai trouvé ma réponse entre temps.
0