Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC

Manifest File Injection

Rate me:
Please Sign up or sign in to vote.
4.08/5 (7 votes)
9 May 2004CPOL2 min read 149.8K   367   31   30
Embed a manifest into an executable as a resource for XP Theme support.

Sample Image - ManifestInjection.jpg

Introduction

It's easy enough to include a manifest file with your application to provide XP theme support, but it's one more file to worry about when distributing an application. A more graceful solution is to include the manifest in the executable as a resource. It's not possible to include the manifest as a resource using the IDE because of the way the IDE handles embedded resources. This project aims at “injecting” a manifest directly into the exe after the exe is compiled, to provide full XP theme support.

Using the code

There are only a few key steps to get your manifest into the executable. The first is to read in the manifest as an array of bytes. That array is passed to the UpdateResource API provided by the kernel32 DLL. The only other code to note here is that, before UpdateResource can be called, you must call BeginUpdateResource. Last, in the finally block, EndUpdateResource is called regardless of the update success. The sample project implements the code as a static method, requiring the assembly path, the manifest path, and the name of the manifest to be injected (typically an int).

C#
try
{
    // Read in the manifest as an array of byest to be injected to the 
    manifestStream = new FileStream(ManifestPath, FileMode.Open,
    FileAccess.Read);
    manifestReader = new BinaryReader(manifestStream);
    manifestByteArray = manifestReader.ReadBytes( (int)manifestStream.Length );
    // Begin the injection process
    updatePointer = (IntPtr)BeginUpdateResource(AssemblyPath, false);
    
    if (updatePointer == IntPtr.Zero)
    {
        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    }
    if (UpdateResource(updatePointer, 24, ResourceName, 0, manifestByteArray, 
                 (uint)manifestByteArray.Length) != 1)
    {
        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    }
}
catch    
{
    result = true;
}
finally
{
    if (updatePointer != IntPtr.Zero)
    {
        EndUpdateResource(updatePointer, result);
    }
    if (manifestReader != null)
    {
        manifestReader.Close();
    }
    if (manifestStream != null)
    {
        manifestStream.Close();
    }
}

The sample project implements the code as a static method requiring the assembly path, the manifest path, and the name of the manifest (typically an integer value) to be injected as arguments. Run the TestForms.exe to make sure it displays standard Windows controls. Use the sample app to browse for a .NET WinForms app (e.g., the TestForm.exe included with the project). Browse for a manifest file (also included in the sample project's directory). Inject the manifest into TestForm.exe and open TestForm.exe again.

For details on the API, see the MSDN article at: http://msdn.microsoft.com/en-us/library/ms648049(VS.85).aspx.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!

http://www.pagelabs.com

Comments and Discussions

 
GeneralManifest Tool (mt.exe) in Microsoft Windows SDK Pin
mfhobbs24-Jul-07 6:40
mfhobbs24-Jul-07 6:40 
GeneralRe: Manifest Tool (mt.exe) in Microsoft Windows SDK Pin
TylerBrinks24-Jul-07 7:40
TylerBrinks24-Jul-07 7:40 
GeneralXP Style affects performance? [modified] Pin
Conex19-Mar-07 2:10
Conex19-Mar-07 2:10 
GeneralRe: XP Style affects performance? Pin
TylerBrinks19-Mar-07 17:35
TylerBrinks19-Mar-07 17:35 
GeneralEmbedding using IDE build events Pin
talon21128-May-06 10:29
talon21128-May-06 10:29 
GeneralRe: Embedding using IDE build events Pin
TylerBrinks10-May-06 3:37
TylerBrinks10-May-06 3:37 
GeneralAutomatic Build Pin
wolfgang_hg28-Dec-05 4:12
wolfgang_hg28-Dec-05 4:12 
GeneralRe: Automatic Build Pin
TylerBrinks8-Jun-06 8:55
TylerBrinks8-Jun-06 8:55 
QuestionNo touch Deployment?? Pin
Geir Aamodt30-May-05 5:30
Geir Aamodt30-May-05 5:30 
AnswerRe: No touch Deployment?? Pin
Geir Aamodt1-Jun-05 0:07
Geir Aamodt1-Jun-05 0:07 
GeneralManifest in signed assemblies Pin
Louis-Philippe Carignan2-Feb-05 11:28
Louis-Philippe Carignan2-Feb-05 11:28 
GeneralRe: Manifest in signed assemblies Pin
TylerBrinks3-Feb-05 4:15
TylerBrinks3-Feb-05 4:15 
GeneralRe: Manifest in signed assemblies Pin
User 3660418-Apr-05 1:52
User 3660418-Apr-05 1:52 
GeneralRe: Manifest in signed assemblies Pin
TylerBrinks3-Feb-05 4:18
TylerBrinks3-Feb-05 4:18 
QuestionResource Types? Pin
Wayne Phipps29-Jan-05 23:32
Wayne Phipps29-Jan-05 23:32 
AnswerRe: Resource Types? Pin
TylerBrinks8-Jun-06 9:05
TylerBrinks8-Jun-06 9:05 
GeneralInjecting manifest into dll Pin
xpit10-Jan-05 0:48
xpit10-Jan-05 0:48 
Did anyone tried to inject manifest into dll file?
Is it possible and will it enable visual styles?

Or maybe there is some other solution if I have usercontrol compiled in dll and used by unamanaged application?

Any ideas?

anything would be helpful...

--
PiT
" I Code Therefore I Am..."
GeneralRe: Injecting manifest into dll Pin
Nithin Philips26-Jan-05 11:06
Nithin Philips26-Jan-05 11:06 
Generalthis code was decompiled from ThemeMe Pin
ctlajoie1-Jul-04 4:55
ctlajoie1-Jul-04 4:55 
GeneralRe: this code was decompiled from ThemeMe Pin
TylerBrinks1-Jul-04 7:12
TylerBrinks1-Jul-04 7:12 
GeneralEnabling XP theme support Pin
BramH10-May-04 22:02
BramH10-May-04 22:02 
GeneralRe: Enabling XP theme support Pin
Oha Ooh10-May-04 22:58
Oha Ooh10-May-04 22:58 
GeneralRe: Enabling XP theme support Pin
TylerBrinks11-May-04 13:23
TylerBrinks11-May-04 13:23 
GeneralRe: Enabling XP theme support Pin
jmt9n19-May-04 6:33
jmt9n19-May-04 6:33 
GeneralRe: Enabling XP theme support Pin
Jose A. Gonzalvo28-May-04 8:24
Jose A. Gonzalvo28-May-04 8:24 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.