Compiling an Application to require UAC Rights Elevation


Recently I was developing an application that played around with my services.  I kept running into the access denied message.  After some research I found out it was related to UAC.

After compiling the application and right-clicking to run as administrator, I wanted something that would force my app to be run as administrator automatically…. thus my search began:

The solution came from Judah Himango on the following post:  http://stackoverflow.com/questions/227187/uac-need-for-console-application

To implement what he’s talking about do the following:

1. Download and install the Windows SDK from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=11310

This SDK contains the MT command referenced in Judah’s response.

 

2. Add a program manifest to your application:

image

 

3. Modify the manifest to reflect the new “requireAdministrator” status as below:

image

 

2. Paste in the following line in your projects post-build step under Visual Studio:

(for 64-bit systems)

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\mt.exe" -manifest "$(ProjectDir)$(TargetName).exe.manifest" -updateresource:"$(TargetDir)$(TargetName).exe;#1"

 

(for 32-bit systems)

"C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\mt.exe" -manifest "$(ProjectDir)$(TargetName).exe.manifest" -updateresource:"$(TargetDir)$(TargetName).exe;#1"

 

image

 

Compile and you’re done.  On Windows 7, vista and 2008 (r2), you should see a little shield next to your icon indicating that administrator privileges are required to run your app.

image

 

Hope this helps others out there!   Let me know by posting your comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s