Click here to Skip to main content
11,434,520 members (55,986 online)
Click here to Skip to main content

Manifest File Injection

, 9 May 2004 CPOL
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

 
GeneralManifest Tool (mt.exe) in Microsoft Windows SDK Pin
mfhobbs24-Jul-07 7:40
membermfhobbs24-Jul-07 7:40 
GeneralRe: Manifest Tool (mt.exe) in Microsoft Windows SDK Pin
Acoustic24-Jul-07 8:40
memberAcoustic24-Jul-07 8:40 
GeneralXP Style affects performance? [modified] Pin
Conex19-Mar-07 3:10
memberConex19-Mar-07 3:10 
GeneralRe: XP Style affects performance? Pin
Acoustic19-Mar-07 18:35
memberAcoustic19-Mar-07 18:35 
GeneralEmbedding using IDE build events Pin
talon21128-May-06 11:29
membertalon21128-May-06 11:29 
GeneralRe: Embedding using IDE build events Pin
Acoustic10-May-06 4:37
memberAcoustic10-May-06 4:37 
GeneralAutomatic Build Pin
wolfgang_hg28-Dec-05 5:12
memberwolfgang_hg28-Dec-05 5:12 
Hi Tyler !

Great work !

I extended your sample and added a console application. This way I can call it from a visual studio project and inject a manifest file to a freshly compiled exe.
The call is put in the post build event. Here is part of the csproj file:
PostBuildEvent = "PATH_TO_MANIFESTINJECTOR\ManifestInjector.exe $(TargetPath) $(ProjectDir)\MY_EXECUTABLE.exe.manifest"


The code for the console app is this one:
static int Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine ("Usage: ManifestInjector.exe targetexe manifestfile");
//Failed...
return -1;
}

string strExe = args[0];
string strManifest = args[1];
Console.WriteLine("Inject manifest: '" + strManifest + "' into executable '" + strExe + "'");
//Do the files exist ?
if (File.Exists (strExe) == false)
{
Console.WriteLine ("Executable '" + strExe + "' does not exist !");
return -1;
}
if (File.Exists (strManifest) == false)
{
Console.WriteLine ("Manifest '" + strManifest + "' does not exist !");
return -1;
}

//Inject the manifest:
bool bolReturn = ManifestInjector.Inject (strExe, strManifest, 1);
if (bolReturn == true)
{
Console.WriteLine ("Injection succeeded !");
return 0;
}
else
{
Console.WriteLine ("Injection failed !");
return -1;
}
}

Best regards

Wolfgang

GeneralRe: Automatic Build Pin
Acoustic8-Jun-06 9:55
memberAcoustic8-Jun-06 9:55 
QuestionNo touch Deployment?? Pin
Geir Aamodt30-May-05 6:30
memberGeir Aamodt30-May-05 6:30 
AnswerRe: No touch Deployment?? Pin
Geir Aamodt1-Jun-05 1:07
memberGeir Aamodt1-Jun-05 1:07 
GeneralManifest in signed assemblies Pin
Louis-Philippe Carignan2-Feb-05 12:28
memberLouis-Philippe Carignan2-Feb-05 12:28 
GeneralRe: Manifest in signed assemblies Pin
Acoustic3-Feb-05 5:15
memberAcoustic3-Feb-05 5:15 
GeneralRe: Manifest in signed assemblies Pin
Paul Carroll18-Apr-05 2:52
memberPaul Carroll18-Apr-05 2:52 
GeneralRe: Manifest in signed assemblies Pin
Acoustic3-Feb-05 5:18
memberAcoustic3-Feb-05 5:18 
QuestionResource Types? Pin
Wayne Phipps30-Jan-05 0:32
memberWayne Phipps30-Jan-05 0:32 
AnswerRe: Resource Types? Pin
Acoustic8-Jun-06 10:05
memberAcoustic8-Jun-06 10:05 
GeneralInjecting manifest into dll Pin
xpit10-Jan-05 1:48
memberxpit10-Jan-05 1:48 
GeneralRe: Injecting manifest into dll Pin
Spikiermonkey26-Jan-05 12:06
memberSpikiermonkey26-Jan-05 12:06 
Generalthis code was decompiled from ThemeMe Pin
0x3D1-Jul-04 5:55
member0x3D1-Jul-04 5:55 
GeneralRe: this code was decompiled from ThemeMe Pin
Acoustic1-Jul-04 8:12
memberAcoustic1-Jul-04 8:12 
GeneralEnabling XP theme support Pin
BramH10-May-04 23:02
memberBramH10-May-04 23:02 
GeneralRe: Enabling XP theme support Pin
Oha Ooh10-May-04 23:58
memberOha Ooh10-May-04 23:58 
GeneralRe: Enabling XP theme support Pin
Acoustic11-May-04 14:23
memberAcoustic11-May-04 14:23 
GeneralRe: Enabling XP theme support Pin
jmt9n19-May-04 7:33
memberjmt9n19-May-04 7:33 
GeneralRe: Enabling XP theme support Pin
jgeer28-May-04 9:24
memberjgeer28-May-04 9:24 
GeneralRe: Enabling XP theme support Pin
jmt9n9-Dec-04 15:27
memberjmt9n9-Dec-04 15:27 
GeneralRe: Enabling XP theme support Pin
Jose A. Gonzalvo10-Dec-04 0:02
memberJose A. Gonzalvo10-Dec-04 0:02 
GeneralRe: Enabling XP theme support Pin
jmt9n10-Dec-04 2:46
memberjmt9n10-Dec-04 2:46 
GeneralRe: Enabling XP theme support Pin
Jose A. Gonzalvo10-Dec-04 4:54
memberJose A. Gonzalvo10-Dec-04 4:54 
GeneralRe: Enabling XP theme support Pin
jmt9n10-Dec-04 4:59
memberjmt9n10-Dec-04 4:59 

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 | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 10 May 2004
Article Copyright 2004 by TylerBrinks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid