Thread: Asking for Admin-Rights

  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738

    Asking for Admin-Rights

    I was wondering: How can a Windows program ask for permission from the user to get administrator rights? I know there's an option when you right-click the exe, but can the program do it from the inside?

    I'm asking because I want to create a Setup-like program but I can't write to Program Files if i don't have administrator rights...

    Anyways, thanks for all those future answers!
    Devoted my life to programming...

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to include a manifest in the installer program's resources...
    Or, for console proggys, in a file with the program name + .manifest (eg. FixUp.exe.Manifest)
    Put the file in the same folder as the executable.

    Code:
      <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
         - <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
              <assemblyIdentity type="win32" 
                                 name="Program name" 
                                version="1.0.0.0" 
                                processorArchitecture="X86" /> 
             <description>Program Description</description> 
    - <dependency>
         - <dependentAssembly>
                <assemblyIdentity type="win32" 
                             name="Microsoft.Windows.Common-Controls" 
                             version="6.0.0.0" 
                             processorArchitecture="X86" 
                             publicKeyToken="6595b64144ccf1df" language="*" /> 
            </dependentAssembly>
        </dependency>
    - <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
         - <security>
             - <requestedPrivileges>
                  <requestedExecutionLevel level="asInvoker" uiAccess="false" /> 
                  </requestedPrivileges>
           </security>
        </trustInfo>
      </assembly>
    You should copy this more or less as is... You will need to change the parts in red to describe your own program and version... You may also have to change the processor architectures (eg. x64 for 64bit, but there are others).

    The part marked in blue is the "desired access"... in an installer you should be ok with "asInvoker" but you might also try "HighestAvailable".

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Thanks!!
    Devoted my life to programming...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Thanks!!
    You're welcome...

    I forgot to mention that you'll need a copy of this in your runtime EXEs as well, unless you want them triggering the UAC all the time....

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    "asInvoker" won't give you admin rights unless Explorer (or whatever app they use to run yours) is already running with admin rights. "HighestAvailable" will only prompt for admin consent if the user is part of the admin group, otherwise it'll run with standard permissions. The only reliable way is to use requireAdministrator, which does what it says.

    If you don't have that section in a manifest and put the word 'setup' in the filename, Windows will sort it all out for you in the name of compatability. Of course, that's not recommended but it exists.

    Or, for console proggys, in a file with the program name + .manifest (eg. FixUp.exe.Manifest)
    You can use both methods for each type of program. All exes are created equal.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adeyblue View Post
    "asInvoker" won't give you admin rights unless Explorer (or whatever app they use to run yours) is already running with admin rights. "HighestAvailable" will only prompt for admin consent if the user is part of the admin group, otherwise it'll run with standard permissions. The only reliable way is to use requireAdministrator, which does what it says.

    If you don't have that section in a manifest and put the word 'setup' in the filename, Windows will sort it all out for you in the name of compatability. Of course, that's not recommended but it exists.
    Excellent.... then "requireAdministrator" in the installer and "asInvoker" in the programs...

    You can use both methods for each type of program. All exes are created equal.
    That depends on the compiler... not all of them will let you add resources to console apps.

    Thanks for the extra info.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Thank you both. I believe i have enough info to proceed.
    Devoted my life to programming...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries...

    And thanks to AdeyBlue for "requireAdministrator" ... ya learns something new every day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Digital Rights
    By MadCow257 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-21-2006, 07:05 PM
  3. Started Again The Cry for Human Rights by Crocodiles
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-24-2003, 10:02 AM
  4. Rights of Particles
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-10-2002, 08:42 PM
  5. human rights - how far do they extend
    By iain in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-20-2002, 09:08 AM