Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsUserAnAdmin? 1

Status
Not open for further replies.

mingis

Programmer
Jan 23, 2002
475
0
0
LT
At first, may be somebody knows some easy way to know, am I privileged user or not? Until now I tryed to write to certain registry key in HKEY_LOCAL_MACHINE, in case of success I decided I am an admin. Now, in Vista, it does not work - Vista allows for everybody to write to system area of registry - to virtual registry file. I have found API IsUserAnAdmin(), but it does work only in Vista. Then I have found an example with CheckTokenMembership(). The API is present in mingw libraries I got recently, in libadvapi32.lib, the actual name of the entry is
_CheckTokenMembership@12
There is no definition of it in mingw headers, so I define it locally as
extern BOOL WINAPI CheckTokenMembership(HANDLE, PSID, PBOOL);
or
WINUSERAPI BOOL WINAPI CheckTokenMembership(HANDLE, PSID, PBOOL);
Compiler generates such a reference to the different external name:
_Z20CheckTokenMembershipPvS_Pi@12
How to force compiler not to generate these additional pre/suffixes? Other API calls are compiled without any suffixes, for example,
WINUSERAPI UINT WINAPI SendInput(UINT,LPINPUT,int);
compiles to
_SendInput@12

Compiler mingw GNU GCC 3.4.4, got together with Code::Blocks v1.0 rc2 IDE, OS Windows Vista.

Any ideas?
Thank you.
 
You have to tell the compiler not to use C++ name decoration. Unforunately I don't know gcc.

In MS VC++ you should write :

extern "C" BOOL ... etc;
instead of
extern BOOL ... etc;

So give that a try.


Marcel
 
avanderlaan said:
IsUserAnAdmin() is available in Win2K, WinXP and Vista.

It should be according to msdn specifications. Unfortunatelly, neither VC 6.0, nor Watcom, nor Mingw libraries do not contain such enter point. Is that probably a macro definition?

I found it under different name - IsUserAdmin(), undocumented though.
 
IsUserAdmin if an example code from the MSDN CheckTokenMembership article:
Code:
BOOL IsUserAdmin(VOID) 

/*++ 
Routine Description: This routine returns TRUE if the caller's process is a member of the Administrators local group. Caller is NOT expected to be impersonating anyone and is expected to be able to open its own process and process token. 
Arguments: None. 
Return Value: 
   TRUE - Caller has Administrators local group. 
   FALSE - Caller does not have Administrators local group. --
*/ 
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup; 
b = AllocateAndInitializeSid(
    &NtAuthority,
    2,
    SECURITY_BUILTIN_DOMAIN_RID,
    DOMAIN_ALIAS_RID_ADMINS,
    0, 0, 0, 0, 0, 0,
    &AdministratorsGroup); 
if(b) 
{
    if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
    {
         b = FALSE;
    } 
    FreeSid(AdministratorsGroup); 
}

return(b);
}
Get Windows SDK if you have not it in VC++ 6.0 old installation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top