Click here to Skip to main content
Click here to Skip to main content

Making Your Application UAC Aware

By , 10 Mar 2007
 

Introduction

This article is a guide to making applications UAC aware for Vista, and aims to explain some of the pitfalls of failing to do so. An application can be made Vista aware through the use of an embedded manifest. Without this, Windows may virtualize access to resources which affects both file system and registry access.

Screenshot - uacdialog.png

Background

As you may know, permission control in Vista has changed radically. This is partly due to the realization that the vast majority of Windows users login to their machines with an administrator account, rather than using the recommended limited account which has more restrictive permissions. Applications running on the machine inherit these user permissions and therefore are usually running with full administrative rights. This obviously makes the operating system much more vulnerable to malicious software and viruses than was originally intended.

The answer that the Windows team came up with is UAC, or User Account Control. With UAC it doesn't matter if you login as administrator, all accounts run under the same limited set of permissions and are not permitted to access protected resources unless explicitly allowed. Instead applications must now ask permission from the user in order to gain access to these resources, known as elevation.

Elevation happens when an application that is flagged as needing administrative permissions is first launched, or when an application is explicitly launched with a run as administrator command by the user. This is the only time applications can do this as applications cannot change their level later and decide to elevate once running. To do this, the process must be terminated and relaunched under a new set of permissions, requesting authorization from the user to do so. The only exception to this is if an application is launched from an existing elevated process. In this case the second process inherits the privileges from the first and the user is not prompted.

The elevation request takes the form of a dialog box that consumes the desktop and is secured against inter-process communication. The user is required to make a choice to allow the application the requested elevation rights or to refuse it. Applications cannot elevate without this user interaction, and this helps secure the desktop from unsecure access to resources.

The impact of UAC is huge, and even affects something as insignificant as the task tray clock. In Vista you will notice you can no longer change the system time by simply clicking on the clock, this is an administrative task and requires elevation.

Virtualization

One of the problems that arise from the introduction of UAC is that many legacy applications still require access to folders and registry settings that are now protected; for instance applications which store configuration data in the Program Files directory instead of using the inbuilt Common AppData path. This is no longer allowed and leads to compatibility issues.

In order to improve compatibility and allow legacy applications to run in a UAC enabled environment, the operating system uses virtualization. This creates a virtualized view of file system and registry resources accessed by the application, and redirects the requests transparently behind the scenes to alternative sandboxed locations. In most cases this can allow a legacy application to function as normal, however this comes with a performance overhead and is only a short term solution. These applications ultimately need to be rewritten to remove their reliance on any protected resources, and instead use the recommended locations to store application and user data.

File system virtualization

Real path
C:\Program Files\Foo\Foo bar\config.ini

Virtual path
C:\Users\<account>\AppData\Local\VirtualStore\Program Files\Foo\Foo bar\config.ini  

Registry virtualization

Real path
HKEY_LOCAL_MACHINE\Software\FooKey

Virtual path
HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software\FooKey    

It goes without saying that a developer should never reference these virtual locations in code. The correct locations for storing application and user data are determined by the environment variables and Windows API. Legacy applications that already use these to read/write application data to the Common AppData folder for instance, won't require virtualization. On Vista, this path would correctly resolve to C:\ProgramData.

The Manifest

The manifest is an XML resource file that can be embedded into the application. In terms of UAC, this serves 2 purposes. Firstly it tells the operating system that the application has been designed with UAC in mind, and that it therefore should not attempt to virtualize any folders or registry settings. If the application still attempts to access protected resources after making its declaration, then these requests will simply fail rather than virtualize. The other thing it does is allow the application to state the privilege level at which it needs to run, and whether it requires elevation.

An example manifest file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>    

These files have been around for a while, and the new section added for UAC control is the requestedExecutionLevel element. The level attribute may be one of the following values:

Level Description
asInvoker Does not require elevation, runs without requesting elevation using privileges of its parent process.
highestAvailable Requests the highest available privilege tokens of its parent process. An administrator account will attempt to elevate to full administrator level, but a standard account will only request elevation to its own highest set of access tokens.
requireAdministrator Requires elevation to full administrator privileges.

Most applications should use the asInvoker level, as this will not prompt the user for elevation every time it is run, and will use the standard set of privileges to access the unprotected resources it needs. Only applications that require access to protected resources should use the higher access levels.

The uiAccess attribute determines whether the application requires access to any protected UI elements, such as system dialog boxes or higher-level processes. Only signed applications may do this as it requires additional security tokens. This value defaults to false.

Techniques for adding the manifest vary between technologies and languages. For .NET managed applications, the following command lines can be used in a post-build step, or via a command prompt using the Microsoft MT.exe tool. Note that the final parameter changes depending on whether the application is a library or an executable.

In this example a manifest file saved as Foobar.exe.manifest is added to an application named Foobar.exe. Note the #1 for application.

mt.exe -manifest "Foobar.exe.manifest" -outputresource:"Foobar.exe";#1    

In this example a manifest file saved as Foobar.dll.manifest is added to a class library named Foobar.dll. Note the #2 for a code library.

mt.exe -manifest "Foobar.dll.manifest" -outputresource:"Foobar.dll";#2    

Other Considerations

By default the elevation dialog is an orange box. To turn this into a friendly blue dialog, the application needs to be code signed. See further reading for links to articles regarding this.

Elevation procedures surrounding installers are a bit too complex to go into detail here, however be warned that the requiresAdministrator flag on an MSI package does not actually provide full administrator permissions. This actually behaves slightly differently and runs as a subset of the full privileges unless you use a bootstrapper to gain full elevation. It is rare that installers would require these additional tokens as the installer's administrator permissions are generally sufficient.

Summary

To make an application UAC aware for Vista, a manifest file should be embedded into the executable or code library with the requestedExecutionLevel element. This prevents virtualization of data access requests and improves performance. It also ensures that application data intended for application scope is not limited to the current user scope.

When referencing file system and registry data within the application/user scopes, ensure that the relevant environment variables are used to correctly locate the unprotected resources.

If your application requires access to protected resources, then set the requestedExecutionLevel element to an appropriate value (other than asInvoker), and consider signing with an Authenticode certificate. This will ensure that the user is prompted to elevate the process, and ensure that the application has the required level of privileges to perform its tasks.

Further Reading

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

TheCodeKing
Architect
United Kingdom United Kingdom
Mike Carlisle - Technical Architect with over 10 years experience in a wide range of technologies.
 
@TheCodeKing
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Question+5memberUmer Aziz Malik3-Nov-12 6:05 
A nice detailed article. Really helped.
Thanks.
Thanks & Best Regards,
Umer Aziz Malik
Senior Strategy Developer
Aurora Solutions (Pvt) Ltd.

GeneralMy vote of 5memberpavanbarot16-Nov-11 3:34 
Simple language for beginner who starts their carrier. excellent article dude.
QuestionDoes file system calls be updated, and what about dependency dll's?memberLarioso27-Mar-11 22:26 
Hi
thanks for a very straight forward article.
 
#1. Does all dll's included for a project need to be marked with manifest?
Or is launching app only necessary?
 
I'm thinking of openssl stuff etc.
 
#2. Does deprecated functions calls need to be updated to run properly?
#3. Can a marked app use short file names(8+3) on long file name places?
 
I have an app only to run asInvoker.
A dll requires short file names(8+3).
This runs fine with unmarked file and virtualization, and runs fine as administrator and no virtualization.
The directories are the proper ones under \ProgramData\appname
 
But it's refusing to open files when marked?
 
So thereby my questions, what is going on?
 
Thanks.
QuestionHow to make a COM+ UAC aware?memberhebeh19-Aug-10 1:02 
Hi all.
I can say I have good knowledge in UAC and virtualization, but experience problem with one COM+ server.
 
I have a COM+ server that runs on Windows 2008 x32. I have an MMC snap-in that is calling this server to write configuration.
 
The problem is that for some reason I cannot grant the COM+ administrative privilege, this is needed since I want to write to C:|Program Files\ when the UAC is on. I do not want virtualization being turned on - this for now is present.
 
What I tried was to add manifest to the dll which functionality is called and also to add in the registry for this interface the Elevation, Enabled = 0x1. But I still do not have access granted. What is the right approach to get access, writing in other location is not solution in my case.
AnswerRe: How to make a COM+ UAC aware?memberTheCodeKing19-Aug-10 7:08 
I'm not an expert on Com+ exactly but I think the issue is that you are trying to write to program files (sorry I know it's not a solution you want). Ever since Vista, the best practice is to write to locations defined by the appropriate environment variable. The OS will then decide where to store the data and it won't require elevation. For application data, this will most likely end up in C:\ProgramData (but of course you would never reference that path directly).
 
Giving your assemblies and applications an appropriate manifest is a way of telling the OS you are compliable with the new rules, and will prevent virtualisation of the filesystem and registry which exists for backwards compatibity (instead of access denied for legacy apps). It's also a way of telling the OS whether you require any specific elevation, which will then result in a UAC prompt when started.
 
It's important to note that a process (or child process) cannot change it's elevation state once it is already running. There is no way to automatically elevate permissions for a DLL for instance. If the process is already running then the process is limited by to the permission tokens granted at startup.
 
For this reason you'll notice certain Windows dialogs such as task manager actually have to shut down and relaunch if u user needs to elevate.

GeneralRe: How to make a COM+ UAC aware? [modified]memberhebeh20-Aug-10 3:38 
Thanks for contribution. I see my approach is wrong.
 
I have the COM+ server and there is activation tab. There is Application Root Directory under which I can place the .manifest file (in my case requesting elevation of privileges). The application.manifest is fine since I have it tested on an exe application. But for my COM+ it does not work
 
Regards,
Neven

modified on Monday, August 23, 2010 4:58 AM

GeneralEasier solution in Visual Studio 2008memberSire40424-Mar-09 2:10 
Read here (at the bottom):
 
http://www.professionalvisualstudio.com/blog/2007/10/05/enabling-your-application-for-uac-on-vista/[^]
 
--------------------
No one is perfect. Welll, there was this guy... but we killed him.
coolquotescollection.com

GeneralUAC disabled , still errors launching programmemberDeepen_3k3-Dec-08 18:55 
After adding the manifest with the administrator privileges the program runs fine for the standard user with UAC enabled , but when I disable the UAC the program does not runs in administrator mode even though the manifest is added.
The program cannot read form the LOCAL_MACHINE registry and other locations for file writing.
 
I thought that diasbling the UAC completly removes the access right , but strangely I cannot run my program.
 
My question is that why the program runs perfeclty with UAC enabled under standard user but does not run if the UAC is disabled for standard user??
QuestionCOM dll solution???memberT800G17-Nov-08 10:57 
After digging through some COM dll examples I found today on the web, I monkey-see-monkey-do added the following to the rgs file of my context menu extension dll:
 
HKLM
{
    NoRemove Software
    {
        NoRemove Microsoft
        {
            NoRemove Windows
            {
                NoRemove CurrentVersion
                {
                    NoRemove 'Shell Extensions'
                    {
                        NoRemove Approved
                        {
                            val {A5096FA1-32DC-4392-A829-445558F0F4B5} = s 'ChangeExtMenuExt Class'
                        }
            ...}...
    }
}
 
and after registering dll through elevated command prompt, it seems that Explorer now loads and executes it succesfully, no manifest needed. Is this the only requirement/registry keys needed?Confused | :confused:
It would be good if someone else can confirm this.
I did turn UAC within 2 hours when my Vista finished installation and updating (OEM preinstall), but didn't made any other similar changes.
QuestionWhat to do with Explorer COM dll (pure C++)? [modified]memberT800G27-Jul-08 11:07 
The more I read, the more I get confused with all that UAC bloat D'Oh! | :doh: .
Does anyone know what should I do with manifest to make a Windows Explorer thumbnail handler COM dll integrate correctly?
I didn't notice that Adobe's pdf thumbnail handler has any problems, but then, I did turned UAC off within first 2 hoursCool | :cool: .
 
Has anyone even tried to make an UAC-aware COM dll? (I have a thumbnail and infotip shell extensions that are ready to roll). Running a control panel extension is a separated process (mentioned in a previous post, but there was no report if there is a correct solution).Confused | :confused:
 
modified on Monday, November 17, 2008 4:55 PM

Questionhow to handle app.config file?memberSouthmountain3-Jul-08 5:50 
I have app.config file residing the same folder as my executable.
My program will write some info into app.config file. What shall I do?
thanks
AnswerRe: how to handle app.config file?memberTheCodeKing3-Jul-08 11:25 
In .Net 2.0 Settings(.settings) should be used to provide a read/write way of storing custom config data. The actual settings are scoped to the current user and are stored within "documents and settings", though this is completely transparent to the application. This way the application never needs to phyisically write to the directory where the application is actually running.
 
Take a look here for more information.
 
http://msdn.microsoft.com/en-us/library/k4s6c3a0(VS.80).aspx[^]
 

GeneralRe: how to handle app.config file?memberSouthmountain3-Jul-08 15:32 
Thank you for the info.
My real question is how to create app.config in Vista? If this app.config resides in the program files folder it needs elevation. It shall be created in %APPDATA% folder. I can create this file for
my program in Visual Studio but how can I deploy this app.config in %APPDATA%?
GeneralRe: how to handle app.config file?memberTheCodeKing3-Jul-08 20:47 
Deploy your app.config file to the same directory as your application as normal. You don't need elevation to read from the configuration file, only if you are writting (in which case use settings).
 

QuestionRegarding UAC and drag and drop and other problems.memberna.nu19-Nov-07 17:39 
Hi,
 
Read your article and used "run as" in installer and added manifests to a application executables that I am working on. Am able to get the elevation prompt on launch of the application. I am facing some problems though:
 
1) The application (win32) I am working on uses drag and drop. It a legacy application and I still use an old IDE for its development. The problem is that elevation has worked but drag and drop does not work any more. I read that since explorer and the desktop work at a lower priviledge level (medium) my application cannot receive any messages related to drag and drop. How do I resolve this? I have tried adding drag and drop messages to the application message filter using an API Changewindowmessagefilter(). This caused the application to crash.
 
2) I require to map a drive for my application to use. If I use Explorer to map the drive then my application does not recieve any messages regarding the added network drive. If I map using my application then only does my application becomes aware of the network drive.
 
3) All folders in a drive are set to read only so I cant send files to these drives as I get write protection status on checking the drive before copying.
 
Please help. Thanks.
 
Cry | :((
GeneralThanks - But be careful with mt.exe versions!memberKevin Gutteridge19-Sep-07 2:09 
Excellent article; got me started with Vista UAC.
 
After reading several similar articles it seemed to me the easiest way of embedding the manifest in C++/VS2005 was by simply creating a .XML document and pointing the project property pages at it (Tools/Project Properties/Manifest Tool/Input and Output/Additional Manifest Files/ and set Embed Manifest to Yes) - The foolish programmer was too simplistic!
 
Using the sample manifest in this article, the error:
manifest authoring warning 81010002: Unrecognized Element "requestedPrivileges" in namespace "urn:schemas-microsoft-com:asm.v3". is generated.
 
It seems the version of mt.exe that is shipped with VS2005 and VS2005 SP1 6.0.4071.0 (and VS2008 beta - the version number of this one is even older 5.2.3790.2014) contains the same error. My installation of VS2005 has 3 (yes 3!) separate mt.exe's!
 
After tracking back, it seems that in order for this to work, you need mt.exe from the SDK kits (subscribe to MSDN to be prepared to download 1GB to get it!).
 
So for anyone like me who has struggled with this one.....
 
Get yourself mt.exe version 5.2.3790.2075 / 727,552 bytes dated 24.01.2007 and search/replace all your copies and all will work fine!
 
Regards,
Kevin.
 
Mad | :mad: Mad | :mad: Mad | :mad: but now Big Grin | :-D Big Grin | :-D Big Grin | :-D
QuestionUAC aware on the in-direct launch?memberHieu Ho19-Aug-07 23:47 
Hi all,
 
I've got a stuck on my application in Vista like this:
 
- I have a general .exe file called Start.exe
- Based on parameter put to the file, it will call another file such as: Uninstall.exe, RegDir.exe or Cleanup.exe... etc
- My Uninstall.exe file has a shield on its icon, means need to allowed from user to launch
- When I call Start.exe ==> Uninstall.exe, it can not launch the Uninstall.exe
 
Solution:
- I put a file call Start.exe.manifest with Start.exe. When launch Start, it ask user to allow and Uninstall can run well after that
 
Problem:
- Now, everytime call RegDir or Cleanup via Start, it always ask the allowance.
 
Please tell me how to ask the alowance when Start==>Uninstall only.
 
Many thanks

 
Hieu Ho
QuestionHow it work with CPL applets ?memberMaxRottenberg5-Jun-07 5:12 
WTF | :WTF: Hi,
 
Did somebody know how to do embedded manifest
at old cpl-applett (win95) for Vista Control
Panel and how its can force to work ?
 
Thanks for any info or references..
 
Evgeniy
 
Romalanno

AnswerRe: How it work with CPL applets ?memberJonathan [Darka]1-Aug-07 3:50 
Add this to your resource file, changing myapp.cpl.manifest to whatever your manifest is called.
 
/////////////////////////////////////////////////////////////////////////////
//
// RT_MANIFEST
//

2                       RT_MANIFEST             "myapp.cpl.manifest"

 

Jonathan Wilkes
Darka[Xanya.net]

GeneralNo admin rights for DLLmemberDeepen_3k3-Jun-07 19:36 
As per your reply i have successfully added the manifest file for the dll.
 

When we add manifest file to the exe it shows the UAc dialog & after clicking that it give full access , how can we achieve that for the dll ?
 
For dll it does not show that UAC dialog , then even if we add the manifest file to the dll it will not give full rights . Is there any way to solve that ?
 

 
Thanks in advance
 


GeneralRe: No admin rights for DLLmemberTheCodeKing3-Jun-07 22:46 
Privilege tokens are only assigned once for a process at start up, so the application invoking the dll needs to be elevated prior to making the calling to a dll that requires elevation. Vista applications simply cannot change elevation level at will once running, and hence why some applications might restart their process in order to elevate after initial launch (task manager).
 
There is one alternative I've come across but not tried, and that's using an out-of-process COM moniker. With this it looks like it's possible to execute the dll from a separate elevated process. This is discussed here and you may find useful.
 
http://msdn2.microsoft.com/en-us/library/ms679687.aspx
 

 

 

Generaladding manifest to a dllmemberDeepen_3k31-May-07 20:05 
Is there any possibility to give admin rights to a dll?
Using the mt.exe tool externally it does not give any rights.
 
Is there any way to do that for VISTA ?

GeneralRe: adding manifest to a dllmemberTheCodeKing1-Jun-07 14:02 
You can add the manifest to a dll as detailed above using #2 (for dll)...
 
[code]
mt.exe -manifest "Foobar.dll.manifest" -outputresource:"Foobar.dll";#2
[/code]
 
However, the user will still need to elevate permissions in the calling process in order to execute with additional rights.
 

GeneralRe: adding manifest to a dllmemberMember 322113115-May-08 10:46 
Yes I've tried to embed a manifest in a dll with a resource type (24) and id (2), it does *not* activate the UAC dialog box.
 
So, embedding a manifest in a dll is completely useless ;-(
GeneralRe: adding manifest to a dllmemberTheCodeKing15-May-08 13:06 
I believe you need to embed the manifest in the exe for the elevation to work, it will not elevate from a referenced dll containing a manifest.
 

GeneralRe: adding manifest to a dllmemberMember 322113115-May-08 13:28 
I guess in VISTA if the referenced dll can be elevated then the whole process will get the high privilege than needed which breaks the security border by design. After all, the exe and dll live in the same process space and share almost everything, the Microsoft virtualization layer cannot tell easily who is calling the APIs Wink | ;-)
GeneralAdding manifest to VC 6.0memberDeepen_3k31-May-07 0:03 
The problem I face is that I have an application which is in VC++ 6.0 which requires an admin rights to execute correctly in VISTA OS.
 

What should i do in VC6.0 to add the manifest file which will be UAC aware & will work in VISTA OS

GeneralRe: Adding manifest to VC 6.0memberTheCodeKing1-Jun-07 13:59 
You will still need to create the manifest file and add it to the application, however you won't be able to use the mt tool as it's not a .net application.
 
Instead you'll need to follow the same process as adding a manifest for XP styles. There's an article here which explains this in more detail which you might find helpful.
 
http://lutrov.com/blog/xp-manifest-files/
 

GeneralSendKeys Errormembervkuttyp14-Mar-07 6:17 
Under Vista SendKeys rise error. Is there any alternative for SendKeys.
Please help.
 
Kutty

GeneralRe: SendKeys Error [modified]memberTheCodeKing14-Mar-07 9:21 
You should add a manifest file with uiAccess set to true.
 
If the application you are trying to send keys to is running at a higher level, you would also need to specify elevation in your own application, otherwise it will not have permission.
 
By the way you'll need to code sign your application in order to use uiAccess=true.

 

GeneralRe: SendKeys Errormembervkuttyp15-Mar-07 8:12 
I tried to add a manifest file using the example in the article.
But the application does not compile. Event Viewer says theres is an error in the first line of the manifest file.
Please help.
 
Kutty

GeneralRe: SendKeys ErrormemberTheCodeKing15-Mar-07 11:06 
It sounds like you're trying to compile the manifest as part of the project which is wrong. You need to compile the application seperately, and then afterwards add the manifest using the MT.exe command line tool as in the example.
 
As I mentioned setting uiAccess to true won't work anyway unless you Code sign the application using an Authenticode certificate, which is another step after compilation and after adding the manifest. Best advise is don't bother trying to use SendKeys, find an alternative.
 

GeneralRe: SendKeys ErrormemberDaveGDaveG30-May-07 5:08 
We had a similar problem with SendKeys using the .Net 1.1 framework. If it's any use, Microsoft have a patch at:
 
http://www.microsoft.com/downloads/details.aspx?FamilyID=59B18749-74F9-4891-8CB5-B22970B58AA9&displaylang=en
 
Dave.
GeneralRe: SendKeys ErrormemberRajeesh.C.V23-Jun-07 17:32 
If you don't want to install a patch to run your application, then use keybd_event function instead of the SendKeys.

 
Rajeesh
 
http://technology.plugai.info/
GeneralPost Build event in VS.NET 2005memberStefan Prodan13-Mar-07 22:15 
Create the manifest in your project root and set up the post build event like this:
 
"$(DevEnvDir)..\..\SDK\v2.0\bin\mt.exe" -manifest "$(ProjectDir)$(TargetName).exe.manifest" –outputresource:"$(TargetDir)$(TargetFileName)";#1
 
http://stefanprodan.wordpress.com

GeneralMikememberGreg Cadmes12-Mar-07 6:24 
Great informative artical! Thanks for posting this. At my company, we are currently resolving this issue with some legacy apps as well as some of our current ones.
 
Greg Cadmes

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 10 Mar 2007
Article Copyright 2007 by TheCodeKing
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid