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
{
manifestStream = new FileStream(ManifestPath, FileMode.Open,
FileAccess.Read);
manifestReader = new BinaryReader(manifestStream);
manifestByteArray = manifestReader.ReadBytes( (int)manifestStream.Length );
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.