Article View: borland.public.cppbuilder.nativeapi
Article #3627Re: WHat is my account type
From: Colin B Maharaj
Date: Tue, 23 May 2006 22:16
Date: Tue, 23 May 2006 22:16
127 lines
4895 bytes
4895 bytes
Jos� wrote:
> Is there a system call which tells me whether I am signed on as an
> administrator or as a limited user?
No coded by Me but works for me...for the last 5 years on WinXP,NT, NT
Server, 2000 - NOT ME,98,95.
//------------------------------------------------------------
BOOL IsAdminLoggedOn(void)
{
HANDLE hToken;
DWORD dwStatus;
DWORD dwAccessMask;
DWORD dwAccessDesired;
DWORD dwACLSize;
DWORD dwStructureSize = sizeof(PRIVILEGE_SET);
PACL pACL = NULL;
PSID psidAdmin = NULL;
BOOL bReturn = FALSE;
PRIVILEGE_SET ps;
GENERIC_MAPPING GenericMapping;
PSECURITY_DESCRIPTOR psdAdmin = NULL;
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = {SECURITY_NT_AUTHORITY};
int FM_ACCESS_READ = 1;
int FM_ACCESS_WRITE= 2;
try
{
// AccessCheck() requires an impersonation token.
ImpersonateSelf(SecurityImpersonation);
if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE,
&hToken))
{
if (GetLastError() != ERROR_NO_TOKEN)
return false;
// If the thread does not have an access token, we'll
// examine the access token associated with the process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY,
&hToken))
return false;
}
if (!AllocateAndInitializeSid(&SystemSidAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &psidAdmin))
return false;
psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (psdAdmin == NULL)
return false;
if (!InitializeSecurityDescriptor(psdAdmin,
SECURITY_DESCRIPTOR_REVISION))
return false;
// Compute size needed for the ACL.
dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) +
GetLengthSid(psidAdmin) - sizeof(DWORD);
// Allocate memory for ACL.
pACL = (PACL)LocalAlloc(LPTR, dwACLSize);
if (pACL == NULL)
return false;
// Initialize the new ACL.
if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2))
return false;
dwAccessMask= FM_ACCESS_READ | FM_ACCESS_WRITE;
// Add the access-allowed ACE to the DACL.
if (!AddAccessAllowedAce(pACL, ACL_REVISION2,
dwAccessMask, psidAdmin))
return false;
// Set our DACL to the SD.
if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE))
return false;
// AccessCheck is sensitive about what is in the SD; set
// the group and owner.
SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE);
SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE);
if (!IsValidSecurityDescriptor(psdAdmin))
return false;
dwAccessDesired = FM_ACCESS_READ;
//
// Initialize GenericMapping structure even though we
// won't be using generic rights.
//
GenericMapping.GenericRead = FM_ACCESS_READ;
GenericMapping.GenericWrite = FM_ACCESS_WRITE;
GenericMapping.GenericExecute = 0;
GenericMapping.GenericAll = FM_ACCESS_READ | FM_ACCESS_WRITE;
if (!AccessCheck(psdAdmin, hToken, dwAccessDesired,
&GenericMapping, &ps, &dwStructureSize, &dwStatus,
&bReturn)) {
printf("AccessCheck() failed with error %lu\n", GetLastError());
return false;
}
RevertToSelf();
}
__finally
{
// Cleanup
if (pACL) LocalFree(pACL);
if (psdAdmin) LocalFree(psdAdmin);
if (psidAdmin) FreeSid(psidAdmin);
}
return bReturn;
}
//------------------------------------------------------------
Technical Details
Message-ID:
<447389a3@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<lko372tv8d3pmfa8aljurjaqc4st3e5g9n@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,283096f94514b8ce X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII Date: Tue, 23 May 2006 18:16:05 -0400 From: Colin B Maharaj <noreply@myhost.com> User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: WHat is my account type References: <lko372tv8d3pmfa8aljurjaqc4st3e5g9n@4ax.com> In-Reply-To: <lko372tv8d3pmfa8aljurjaqc4st3e5g9n@4ax.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Antivirus: avast! (VPS 0621-2, 05/23/2006), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 200.108.22.244 Message-ID: <447389a3@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1148422563 200.108.22.244 (23 May 2006 15:16:03 -0700) Lines: 127 Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail