how to a detect my process has superuser privileges in windows os. for exa开发者_如何转开发mple i would like to detect if my ie explore is using superuser privilege my account is administrators groups or normal user groups
Use the IsUserAnAdmin function.
Edit: Alternatively, based on the reading of that API it might be better to directly use CheckTokenMembership since the future availability of IsUserAnAdmin seems to be in doubt; there is an example on that page.
To get the groups info, you may use GetUserName
and then NetUserGetInfo function, but by your question it wasn't evident if you are looking for such thing or not.
Here's a VB6 solution to check if the current process is elevated; should be easy enough to translate to C++.
Public Function IsCurrentProcessElevated() As Boolean
Dim lRet As Long, pAdministratorsGroup As Long
Dim udtSidIdentifierAuthority As SID_IDENTIFIER_AUTHORITY
udtSidIdentifierAuthority.Value(5) = 5 ' SECURITY_NT_AUTHORITY
lRet = AllocateAndInitializeSid(udtSidIdentifierAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, pAdministratorsGroup)
If lRet <> 0 Then
If CheckTokenMembership(0, pAdministratorsGroup, lRet) <> 0 Then ' Use 0 to check the calling thread
IsCurrentProcessElevated = (lRet <> 0)
End If
' Note: This line was often crashing in Windows 7, fix was to change the API declare to recieve argument ByVal
' http://stackoverflow.com/questions/1913087/checktokenmembership-in-vb6-crashing-on-freesid-on-windows-7-and-windows-2008
Call FreeSid(pAdministratorsGroup)
End If
End Function
精彩评论