IsAdministrator
See Also: Miscellaneous Functions
Purpose
Returns True if the user is currently running with elevated, administrator privileges.
Return Type
Syntax
Use GlobalFunctionsProcedures.pkg
(IsAdministrator())
What it Does
IsAdministrator is called to determine if a user is running at an administrator level. Prior to Vista, this would indicate that the user is logged in with administrator rights. In Vista, this also indicates if the user is currently running this program as an administrator. While this can be used on any platform to verify that a user is an administrator, it was created to provide an easy way under Vista to determine if a failed process occurred because the user was not running with elevated rights.
In the following example, we will assume that the configuration file is being saved under "Program Files". Under Vista, you cannot do this without running the program as an administrator. Under XP, this would work as long as you are logged in as an administrator. If the save fails, we will test to see if the user is running as an administrator. If not, we will provide them with an error explaining why this failed.
Function SaveTheConfigFile returns Boolean
Boolean bIsAdmin bError
Get SaveConfig to bError
// If this failed, it might have failed for some unknown reason or it might have failed
// because this particular file is saved under program files and this requires elevated
// rights under Vista.
If (bError) Begin
// Is user running the program as an administrator?
Move (IsAdministrator()) to bIsAdmin
If (not (bIsAdmin)) Begin
Send ReportError ("Could not save this file. It is likely that this operation " +;
"requires elevated administrator rights. You are currently not " +;
"running as an administrator. Try closing this application, running it " +
"as an administrator and try this operation again.")
End
Else Begin
Send ReportError "Could not save the file. I have no idea what that happened"
End
End
Function Return bError
End_Function
Important Notes
-
We would only perform a test like this if we suspected that the operation was going to require administrator rights.
-
We perform the administrator test after the failure and not before. That way this does not have to be a Vista-specific test.
-
The best way to avoid this situation entirely is to not write to areas that require administrator rights. Under Vista, everything runs much better if you can run the application with standard user privileges.
Note
IsAdministrator is not supported in Windows 2000 (and other Windows revisions older than Windows 2000). On Windows 2000 and prior, IsAdministrator always returns True.