简体   繁体   中英

How to elevate application when UAC is turned Off?

I am developing C#/.NET application that needs administrative privileges. It will run under WinXP and Win7. In order to request elevation of privileges when user is running as Standar user (not member of Administrators group), I embed manifest with line:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

And, that works fine in Win7 when in UAC is turned ON. User gets prompted for elevated rights, enter credentials etc.
Problem is when in Win7 when in UAC is turned OFF. Application starts without UAC prompt and WITHOUT elevated rights.

Is there a way to request elevation and pop-up UAC prompt when UAC is turned OFF?

No, but there is a workaround.

UAC being off does not preclude an application from being run as Administrator (assuming you have an administrator password, as it seems you do), just makes it harder. As you rightfully pointed out, with UAC disabled and requireAdministrator set in the manifest, right-clicking and selecting Run as administrator does not actually elevate the process, as Microsoft indicates: " Application might launch but will fail later "

Two Steps:

1) Hold Shift while Right-clicking on the application and select Run as a different user . Then simply use your Administrator user name and password to authenticate, and your application should run as Administrator. It worked for me.

截图

2) Build a small executable that runs asInvoker and checks for Administrative privileges. When it is run without them, warn the user and tell them to Shift-Right Click, then Run as a different user . If your small program has administrator access, then use ShellExecute to invoke your primary requireAdministrator application. See Figure 9 here for a flow diagram. You are basically replacing the built-in UAC dialog with your own, because, hey, UAC is off.

Here is a small code sample in C++ from somewhere on StackOverflow that checks for administrator access:

BOOL IsUserAdmin(VOID)
{
   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(true==b) 
   {
      if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
      {
         b = FALSE;
      } 
      FreeSid(AdministratorsGroup); 
   }
   return(b);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM