Cita de: Eleкtro en 25 Febrero 2014, 06:39 AM
Si devuelve Falso es porque no eres Admin, quizás sólamente eres usuario con privilegios elevados, pero no Administrador.
PD: Yo uso Windows 8.
Saludos
Estoy probando tu script, y ejecutandolo en Windows XP, desde la cuenta de administrador me devuelve Falso...
Cita de: mDrinky en 25 Febrero 2014, 09:56 AM
La función IsUserAnAdmin solo funciona desde XP a vista. Te recomiendo que uses CheckTokenMembership, que funciona desde XP hasta 8.
He encontrado esta función, pero veo mucho código y no se muy bien que es lo que hace la función en sí (Utiliza la función que me has dicho)
Código (vbnet) [Seleccionar]
Option Explicit
Private Const TOKEN_DUPLICATE = &H2&
Private Const TOKEN_QUERY = &H8&
Private Const ERROR_NO_TOKEN = 1008
Private Const SECURITY_BUILTIN_DOMAIN_RID = &H20&
Private Const DOMAIN_ALIAS_RID_ADMINS = &H220&
Private Const SECURITY_NT_AUTHORITY = &H5&
Private Type SID_IDENTIFIER_AUTHORITY
Value(6) As Byte
End Type
Private Enum SECURITY_IMPERSONATION_LEVEL
SecurityAnonymous
SecurityIdentification
SecurityImpersonation
SecurityDelegation
End Enum
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, ByRef TokenHandle As Long) As Long
Private Declare Function OpenThreadToken Lib "advapi32" (ByVal ThreadHandle As Long, ByVal DesiredAccess As Long, ByVal OpenAsSelf As Long, ByRef TokenHandle As Long) As Long
Private Declare Function AllocateAndInitializeSid Lib "advapi32" (ByRef pIdentifierAuthority As SID_IDENTIFIER_AUTHORITY, ByVal nSubAuthorityCount As Byte, ByVal nSubAuthority0 As Long, ByVal nSubAuthority1 As Long, ByVal nSubAuthority2 As Long, ByVal nSubAuthority3 As Long, ByVal nSubAuthority4 As Long, ByVal nSubAuthority5 As Long, ByVal nSubAuthority6 As Long, ByVal nSubAuthority7 As Long, ByRef lpPSid As Long) As Long
Private Declare Function CheckTokenMembership Lib "advapi32" (ByVal TokenHandle As Long, ByVal SidToCheck As Long, ByRef IsMember As Long) As Long
Private Declare Function DuplicateToken Lib "advapi32" (ByVal ExistingTokenHandle As Long, ByVal ImpersonationLevel As Long, ByRef DuplicateTokenHandle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Sub FreeSid Lib "advapi32.dll" (ByVal pSid As Long)
Public Function IsInRoleAdmin() As Boolean
Dim NtAuthority As SID_IDENTIFIER_AUTHORITY
Dim AdminGroup As Long
Dim Success As Long
Dim hToken As Long
Dim hTokenDup As Long
If OpenThreadToken(GetCurrentThread, TOKEN_DUPLICATE Or TOKEN_QUERY, True, hToken) = 0 Then
OpenProcessToken GetCurrentProcess, TOKEN_DUPLICATE Or TOKEN_QUERY, hToken
End If
If DuplicateToken(hToken, SecurityImpersonation, hTokenDup) Then
' Well-known SIDs
NtAuthority.Value(5) = SECURITY_NT_AUTHORITY
' allocates and initializes a security identifier (SID)
Success = AllocateAndInitializeSid(NtAuthority, 2, _
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, AdminGroup)
If Success Then
If CheckTokenMembership(hTokenDup, AdminGroup, Success) = 0 Then
Success = 0
End If
FreeSid AdminGroup
End If
End If
IsInRoleAdmin = Success
CloseHandle hToken
CloseHandle hTokenDup
End Function
Es decir, por ejemplo este trozo:
Código (vbnet) [Seleccionar]
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, ByRef TokenHandle As Long) As Long
Private Declare Function OpenThreadToken Lib "advapi32" (ByVal ThreadHandle As Long, ByVal DesiredAccess As Long, ByVal OpenAsSelf As Long, ByRef TokenHandle As Long) As Long
Private Declare Function AllocateAndInitializeSid Lib "advapi32" (ByRef pIdentifierAuthority As SID_IDENTIFIER_AUTHORITY, ByVal nSubAuthorityCount As Byte, ByVal nSubAuthority0 As Long, ByVal nSubAuthority1 As Long, ByVal nSubAuthority2 As Long, ByVal nSubAuthority3 As Long, ByVal nSubAuthority4 As Long, ByVal nSubAuthority5 As Long, ByVal nSubAuthority6 As Long, ByVal nSubAuthority7 As Long, ByRef lpPSid As Long) As Long
Private Declare Function CheckTokenMembership Lib "advapi32" (ByVal TokenHandle As Long, ByVal SidToCheck As Long, ByRef IsMember As Long) As Long
Private Declare Function DuplicateToken Lib "advapi32" (ByVal ExistingTokenHandle As Long, ByVal ImpersonationLevel As Long, ByRef DuplicateTokenHandle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Para que necesita esas declaraciones? OpenProccess, OpenThread...
Saludos