Click here to Skip to main content
6,630,586 members and growing! (16,170 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

Manifest File Injection

By Acoustic

Embed manifest into an executable as a resource for XP Theme support
C++, C#.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, MFC, VS.NET2003, Dev
Posted:9 May 2004
Views:94,641
Bookmarked:24 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.45 Rating: 4.08 out of 5

1

2
1 vote, 14.3%
3
1 vote, 14.3%
4
5 votes, 71.4%
5

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 (i.e. the
TestForm.exe included with the project). Browse for a manifest file (also included in the
sample project�s directory. Inject the manifest into the TestForm.exe and open TestForm.exe again.

For details on the API see the MSDN article at:

http://msdn.microsoft.com/library/en-us/winui/WinUI/.../ResourceFunctions/UpdateResource.asp

License

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

About the Author

Acoustic


Member
Acoustic has been designing and developing applications using C# and the .NET framework since the 1.0 Beta release in 2001, and has over 8 years of software development experience. Recently he's been enjoying C# 4, MVC, and other emerging toys.

When he's not writing code, you can catch him fly fishing or wondering how he lost the last chess game.
Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
GeneralManifest Tool (mt.exe) in Microsoft Windows SDK Pinmembermfhobbs7:40 24 Jul '07  
GeneralRe: Manifest Tool (mt.exe) in Microsoft Windows SDK PinmemberAcoustic8:40 24 Jul '07  
GeneralXP Style affects performance? [modified] PinmemberConex3:10 19 Mar '07  
GeneralRe: XP Style affects performance? PinmemberAcoustic18:35 19 Mar '07  
GeneralEmbedding using IDE build events Pinmembertalon211211:29 8 May '06  
GeneralRe: Embedding using IDE build events PinmemberAcoustic4:37 10 May '06  
GeneralAutomatic Build Pinmemberwolfgang_hg5:12 28 Dec '05  
GeneralRe: Automatic Build PinmemberAcoustic9:55 8 Jun '06  
GeneralNo touch Deployment?? PinmemberGeir Aamodt6:30 30 May '05  
GeneralRe: No touch Deployment?? PinmemberGeir Aamodt1:07 1 Jun '05  
GeneralManifest in signed assemblies PinmemberLouis-Philippe Carignan12:28 2 Feb '05  
GeneralRe: Manifest in signed assemblies PinmemberAcoustic5:15 3 Feb '05  
GeneralRe: Manifest in signed assemblies PinmemberPaul Carroll2:52 18 Apr '05  
GeneralRe: Manifest in signed assemblies PinmemberAcoustic5:18 3 Feb '05  
GeneralResource Types? PinmemberWayne Phipps0:32 30 Jan '05  
GeneralRe: Resource Types? PinmemberAcoustic10:05 8 Jun '06  
GeneralInjecting manifest into dll Pinmemberxpit1:48 10 Jan '05  
GeneralRe: Injecting manifest into dll PinmemberSpikiermonkey12:06 26 Jan '05  
Generalthis code was decompiled from ThemeMe Pinmember0x3D5:55 1 Jul '04  
GeneralRe: this code was decompiled from ThemeMe PinmemberAcoustic8:12 1 Jul '04  
GeneralEnabling XP theme support PinmemberBramH23:02 10 May '04  
GeneralRe: Enabling XP theme support PinmemberOha Ooh23:58 10 May '04  
GeneralRe: Enabling XP theme support PinmemberAcoustic14:23 11 May '04  
GeneralRe: Enabling XP theme support Pinmemberjmt9n7:33 19 May '04  
GeneralRe: Enabling XP theme support Pinmemberjgeer9:24 28 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 May 2004
Editor: Heath Stewart
Copyright 2004 by Acoustic
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project