App needs to be executed with admin privileges
February 23rd, 2011
It has been some weeks ago when I needed to find a way to make to be executed with full rights. There are several reason why you got to do so e.g. get write-access to C:
It was my aim to show that nice UAC Dialog from Windows itself when the rights are needed.
And this is how it works:
Create a manifest for you executable which is simple xml:
<?xml version="1.0" encoding="utf-8" ?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="yourApp" type="win32" /> <description>Your App Description</description> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"><security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" /> </requestedPrivileges> </security> </trustInfo></assembly>
this is the important line:
<requestedExecutionLevel level="requireAdministrator" />Now you can rename it to (YourApp).manifest. The .NET Framework when executing the file will see the Manifest and handle its contents.
Or you can, which I regard as better, embed the .manifest file into you executable.
mt -manifest YourApp.exe.manifest -outputresource:YourApp.exe
The mt.exe come with Visual Studio SDK which can be downloaded at Microsoft’s.
To simplify this I created a Post-Build event which looks like this:
“C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mt.exe” -manifest “$(ProjectDir)$(TargetName).exe.manifest” –outputresource:”$(TargetDir)$(TargetFileName)”;#1
An important note:
If your assembly is strong named, you will be unable to embed the manifest into it as it would invalidate the strong naming.
Some source:
Every assembly, whether static or dynamic, contains a collection of data that describes how the elements in the assembly relate to each other. The assembly manifest contains this assembly metadata. An assembly manifest contains all the metadata needed to specify the assembly’s version requirements and security identity, and all metadata needed to define the scope of the assembly and resolve references to resources and classes. The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a standalone PE file that contains only assembly manifest information. (http://msdn.microsoft.com/en-us/library/bb756929.aspx)






informative post, thank you for sharing.
Comment by outdated driver — May 23, 2011 @ 5:13 am
Your Blog is Fabulous. Good article rather. Very interesting.
Keep inspiring your readers, helpful for lots of peoples. thank you!
Comment by GED Online — July 16, 2011 @ 9:58 am
You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject. Your blog is very unique, thanks for taking the time to share your view with us.
Comment by Corporate Logo Design — July 26, 2011 @ 10:13 am
Your blog really attractive, it is inspiration and useful.
thanks
Comment by Dissertation Writing — August 1, 2011 @ 2:43 pm