Mon application Vista doit savoir si l'utilisateur l'a lancé "en tant qu'administrateur" (surélevé) ou en tant qu'utilisateur standard (sans surélévation). Comment puis-je détecter cela à l'exécution?Comment puis-je détecter si mon processus exécute UAC-élevé ou non?
Répondre
La fonction C++ suivant peut le faire:
HRESULT GetElevationType(__out TOKEN_ELEVATION_TYPE * ptet);
/*
Parameters:
ptet
[out] Pointer to a variable that receives the elevation type of the current process.
The possible values are:
TokenElevationTypeDefault - This value indicates that either UAC is disabled,
or the process is started by a standard user (not a member of the Administrators group).
The following two values can be returned only if both the UAC is enabled
and the user is a member of the Administrator's group:
TokenElevationTypeFull - the process is running elevated.
TokenElevationTypeLimited - the process is not running elevated.
Return Values:
If the function succeeds, the return value is S_OK.
If the function fails, the return value is E_FAIL. To get extended error information, call GetLastError().
Implementation:
*/
HRESULT GetElevationType(__out TOKEN_ELEVATION_TYPE * ptet)
{
if (!IsVista())
return E_FAIL;
HRESULT hResult = E_FAIL; // assume an error occurred
HANDLE hToken = NULL;
if (!::OpenProcessToken(
::GetCurrentProcess(),
TOKEN_QUERY,
&hToken))
{
return hResult;
}
DWORD dwReturnLength = 0;
if (::GetTokenInformation(
hToken,
TokenElevationType,
ptet,
sizeof(*ptet),
&dwReturnLength))
{
ASSERT(dwReturnLength == sizeof(*ptet));
hResult = S_OK;
}
::CloseHandle(hToken);
return hResult;
}
Pour la fonction IsVista (et plus de détails sur GetElevationType), voir l'article de blog d'Andrei: http : //www.softblog.com/2008-02/vista-tools/ –
Pour ceux d'entre nous qui travaillent en C#, dans le SDK Windows, il y a une application "UACDemo" comme une partie des "échantillons Cross Technology". Ils trouvent si l'utilisateur actuel est un administrateur en utilisant cette méthode:
private bool IsAdministrator
{
get
{
WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsPrincipal wp = new WindowsPrincipal(wi);
return wp.IsInRole(WindowsBuiltInRole.Administrator);
}
}
(Note: Je refactorisé le code d'origine pour être une propriété, plutôt que d'une déclaration « si »)
Question, cela fera-t-il de la sécurité de domaine? (MYDOMAIN \ Administrators) Ou est-ce la sécurité locale seulement? – mattlant
WindowsBuiltInRole.Administrator est le groupe d'administration local – Ryan
Si votre compte de domaine est un administrateur local pour cette machine, ou un administrateur de domaine - il sera dans ce groupe local par défaut, afaik. –
Je ne pense pas que l'altitude tapez est la réponse que vous voulez. Vous voulez juste savoir s'il est élevé. Utilisez TokenElevation au lieu de TokenElevationType lorsque vous appelez GetTokenInformation. Si la structure renvoie une valeur positive, l'utilisateur est admin. Si la valeur est zéro, l'utilisateur a une élévation normale.
Voici une solution Delphi:
function TMyAppInfo.RunningAsAdmin: boolean;
var
hToken, hProcess: THandle;
pTokenInformation: pointer;
ReturnLength: DWord;
TokenInformation: TTokenElevation;
begin
hProcess := GetCurrentProcess;
try
if OpenProcessToken(hProcess, TOKEN_QUERY, hToken) then try
TokenInformation.TokenIsElevated := 0;
pTokenInformation := @TokenInformation;
GetTokenInformation(hToken, TokenElevation, pTokenInformation, sizeof(TokenInformation), ReturnLength);
result := (TokenInformation.TokenIsElevated > 0);
finally
CloseHandle(hToken);
end;
except
result := false;
end;
end;
est ici un processus est élevé
Option Explicit
'--- for OpenProcessToken
Private Const TOKEN_QUERY As Long = &H8
Private Const TokenElevation As Long = 20
Private Declare Function GetCurrentProcess Lib "kernel32"() As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function IsElevated(Optional ByVal hProcess As Long) As Boolean
Dim hToken As Long
Dim dwIsElevated As Long
Dim dwLength As Long
If hProcess = 0 Then
hProcess = GetCurrentProcess()
End If
If OpenProcessToken(hProcess, TOKEN_QUERY, hToken) Then
If GetTokenInformation(hToken, TokenElevation, dwIsElevated, 4, dwLength) Then
IsElevated = (dwIsElevated <> 0)
End If
Call CloseHandle(hToken)
End If
End Function
si un (courant) la mise en œuvre de VB6 d'un chèque
- 1. Comment détecter si un utilisateur exécute IE 6?
- 2. Comment détecter si mon programme fonctionne sous Windows?
- 3. Comment détecter si l'imprimante est connectée à votre ordinateur ou non en Java?
- 4. Comment puis-je détecter si sys.stdout est connecté au terminal ou non?
- 5. Identifiez si un fichier (dll/exe) est verrouillé ou non par un processus ou une bibliothèque
- 6. Comment détecter si le port série est utilisé ou non dans .net
- 7. window.onbeforeunload détecter si POST ou GET
- 8. Comment détecter si Vista UAC est activé?
- 9. Détecter si mon application s'exécute sous l'EDI "Delphi 2007 .Net"
- 10. détecter si un tableau est indexé ou Associatif
- 11. Comment déterminer si un ordinateur exécute XP Service Pack 3
- 12. Détecter si IIS est activé
- 13. Comment détecter l'OpenJDK dans mon applet (Propriétés système)?
- 14. si l'utilisateur est déjà connecté ou non?
- 15. Vérifiez si ftp est complet ou non?
- 16. Vérifiez si le fichier existe ou non?
- 17. Comment détecter si un graphique sera tramé?
- 18. iPhone: Comment détecter si iTunes joue?
- 19. Détecter si IP est statique ou obtenu à partir de DHCP sur busybox
- 20. Comment vérifier si un lien est actif ou non?
- 21. Comment vérifier si une méthode python est liée ou non?
- 22. Est-il possible dans DB2 ou dans une base de données de détecter si la table est verrouillée ou non?
- 23. Comment détecter si une barre de défilement se trouve ou non à la fin d'une zone de texte enrichi (vb.net)
- 24. En PHP 5.x, comment puis-je détecter si une classe est abstraite ou non au moment de l'exécution?
- 25. Comment détecter que pour les paramètres régionaux en cours si pack MUI d'Office est installé ou non de C#
- 26. Détecter les privilèges de débogage sur un processus (Windows, C)
- 27. Comment détecter si une page Web distante est disponible
- 28. Comment détecter si NSDrawer va s'ouvrir
- 29. Comment détecter si un moniteur est en mode veille?
- 30. Comment détecter si un moniteur est grand écran sous Windows
La fonction IsUserAnAdmin pourrait également être utile. http://msdn.microsoft.com/en-us/library/windows/desktop/bb776463%28v=vs.85%29.aspx – jmnben