Click here to Skip to main content
Click here to Skip to main content
Go to top

Manifest File Injection

, 9 May 2004
Rate this:
Please Sign up or sign in to vote.
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).

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)

Share

About the Author

TylerBrinks
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
Follow on   Twitter

Comments and Discussions

 
GeneralInjecting manifest into dll Pinmemberxpit10-Jan-05 0:48 
GeneralRe: Injecting manifest into dll PinmemberSpikiermonkey26-Jan-05 11:06 
Generalthis code was decompiled from ThemeMe Pinmember0x3D1-Jul-04 4:55 
GeneralRe: this code was decompiled from ThemeMe PinmemberAcoustic1-Jul-04 7:12 
I’m not surprised they look the same. For one, it looks like this tool does the same thing mine does, only in c++. The similarities are due to the fact that the external DLL’s being called all share the same common names. “BeginUpdateResource,” “EndUpdateResource,” and “UpdateResource” would be the same for anyone. The COM Marshaling is also standard, and would be the same when no HRESULT is returned (i.e. Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); ) That’s all the major code in the method, so I’m not surprised that they look so similar. Plus, I got my idea from the MSDN article (wish it was original) I linked to in this article. I imagine this other project came from the same source. I’m going to have to email this guy and show him your post – what a cool observation!!
GeneralEnabling XP theme support PinmemberBramH10-May-04 22:02 
GeneralRe: Enabling XP theme support PinmemberOha Ooh10-May-04 22:58 
GeneralRe: Enabling XP theme support PinmemberAcoustic11-May-04 13:23 
GeneralRe: Enabling XP theme support Pinmemberjmt9n19-May-04 6:33 
GeneralRe: Enabling XP theme support Pinmemberjgeer28-May-04 8:24 
GeneralRe: Enabling XP theme support Pinmemberjmt9n9-Dec-04 14:27 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 10 May 2004
Article Copyright 2004 by TylerBrinks
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid