04 April, 2011

How to add a manifest to a legacy application.

Copied verbatim from Klaus Bjorn Jensen's blog 

How to add a manifest to a legacy application (which you cannot or will not recompile)

I used mt.exe from Windows SDK
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\mt.exe"
to add a manifest to an existing legacy exe to make it Run As Administrator (with elevated privileges) on UAC enabled systems (Vista, Win7 , Win 2008 ...). Here's how:

1)Got a legacy exe file named KSDiag.exe
2)In the same folder create a text file named KSDiag.exe.manifest
3)Put the following lines into the text 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="requireAdministrator"
uiAccess="False"/]
[/requestedPrivileges]
[/security]
[/trustInfo]
[/assembly]

Replacing [ with LessThan and ] with GreaterThan



4) Run command:
mt.exe /manifest KSDiag.exe.manifest /outputresource:KSDiag.exe;1
(1 for an EXE, 2 for a DLL
mt.exe –manifest MyApp.exe.manifest -outputresource:MyApp.exe;1
or
mt.exe –manifest MyLibrary.dll.manifest -outputresource:MyLibrary.dll;2
See also: http://msdn.microsoft.com/en-us/library/ms235591.aspx
http://msdn.microsoft.com/en-us/library/aa375649(VS.85).aspx )
Now KSDiag.exe has the manifest added to its resource section. On a UAC enabled OS it gets the shield icon and you get the administrator prompt when you try to run it.
Of course if the file was signed to start with you'll have to resign it.