Thursday, December 31, 2015

Find if User is An Admin in Windows 7 (Visual Foxpro Function)

The Windows API IsUserAnAdmin was deprecated in Windows 7. It did not always work correctly in Windows Vista. This will correctly determine if the current application is running with administrative privileges. It returns the same value as the original API does. 1 = True, 0 = False Author: Tracy Pearson  Freeware 0.7K Last updated:2013.01.09

#DEFINE SECURITY_NT_AUTHORITY 5
#DEFINE SECURITY_BUILTIN_DOMAIN_RID 32
#DEFINE DOMAIN_ALIAS_RID_ADMINS 544

Declare FreeSid In Advapi32.Dll Long pSid
Declare Long CheckTokenMembership In Advapi32.Dll Long TokenHandle, Long pSid, Long @ IsMember
DECLARE Integer AllocateAndInitializeSid IN Advapi32.dll ;
   string pIdentifierAuthority, integer nSubAuthorityCount, integer dwSubAuthority0, integer dwSubAuthority1, integer dwSubAuthority2, ;
   integer dwSubAuthority3, integer dwSubAuthority4, integer dwSubAuthority5, integer dwSubAuthority6, integer dwSubAuthority7, long @ pSid

Declare Long HeapAlloc In WIN32API Long, Long, Long
Declare Long GetProcessHeap In WIN32API
Declare Long HeapFree In WIN32API Long, Long, Long

Local ;
   cIdentifierAuthority, ;
   nSubAuthorityCount, ;
   dwSubAuthority0, ;
   dwSubAuthority1, ;
   dwSubAuthority2, ;
   dwSubAuthority3, ;
   dwSubAuthority4, ;
   dwSubAuthority5, ;
   dwSubAuthority6, ;
   dwSubAuthority7, ;
   pSid, ;
   pTokenHandle, ;
   lIsMember

cIdentifierAuthority = Replicate(Chr(0),5)+Chr(5)
nSubAuthorityCount = 2
dwSubAuthority0 = SECURITY_BUILTIN_DOMAIN_RID 
dwSubAuthority1 = DOMAIN_ALIAS_RID_ADMINS 
dwSubAuthority2 = 0
dwSubAuthority3 = 0
dwSubAuthority4 = 0
dwSubAuthority5 = 0
dwSubAuthority6 = 0
dwSubAuthority7 = 0
pSid = HeapAlloc( GetProcessHeap(), 0, 254 )
Sys(2600, pSid, 254, Replicate(Chr(0), 254))
If AllocateAndInitializeSid( ;
   cIdentifierAuthority, ;
   nSubAuthorityCount, ;
   dwSubAuthority0, ;
   dwSubAuthority1, ;
   dwSubAuthority2, ;
   dwSubAuthority3, ;
   dwSubAuthority4, ;
   dwSubAuthority5, ;
   dwSubAuthority6, ;
   dwSubAuthority7, ;
   @pSid) == 0
   Return .F.
EndIf
If CheckTokenMembership( ;
   pTokenHandle, ;
   pSid, ;
   @lIsMember) == 0
   Return .F.
EndIf
FreeSid(pSid)
HeapFree( GetProcessHeap(), 0, pSid)


Return IIF(lIsMember, 1, 0)




No comments:

Post a Comment