Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Command line tool with source code to add native resources to any executable

4.17/5 (9 votes)
1 Dec 2007CPOL3 min read 1   739  
A command line tool with source code to add any native Win32 resource (HTML, Icons, BMPs, etc.) to an executable.

Introduction

This command line tool adds any file as a resource to another executable you specify. The tool accepts arguments like a file or directory with files to be added as a resource, with the type and name for each of the resource. The complete source code is provided.

Background

This tool can be helpful for executables produced with managed technologies like .NET, where the resource files are not natively embedded. There are some situations when you do want that your managed application have native resources.

For example, if you wish to use the res:// protocol to load an HTML file from your managed executable (assembly). You can create a post build stepn and run this tool to add the native resources to the managed application.

Using the tool

Command line syntax:

AddResource.exe ExePath ResFile [ResName] [ResType]

Arguments description:

  • ExePath - path of the executable file to add resources to.
  • ResFile - path of the file or directory of files to be added as a resource(s).
  • ResName - (optional) name of the resource; if omitted, the application will use the filename as the resource name. If ResFile denotes a directory, then ResName is ignored.
  • ResType - (optional) type of the resource; if omitted, the application will try to use one of the default resource types based on the ResFile file extension. For example, an 'index.html' file will use the RT_HTML resource type.

Note that if ResName or ResType refers to a resource ID (satisfies the IS_INTRESOURCE(x) macro), it will be used like a resource ID not as a resource string. For example, if (for a strange reason) you want to have a resource type of 23 as string, you won't be able to do that. If you don't know understand what this note says about the difference between a resource name as string and resource name as integer, ignore it, as 99.99% you won't need it.

Example

AddResource.exe myManagedApp.exe Help.htm help.htm 23

It is equivalent to omitting the resource type:

AddResource.exe myManagedApp.exe Help.htm help.htm

because the value of RT_HTML is 23.

If you have a directory with files you want to add as a resource, you pass the directory path.

AddResource.exe myManagedApp.exe U:\Projects\myManagedApp/Resources

Each file from the directory will be added as a resource using the resource name, the filename, and the resource type will be determined based on the file type.

The application automatically maps HTML files to RT_HTML, BMP files to RT_BITMAP, and ICO files to RT_ICON. The file type is determined by its extension. Any other type of file is automatically added to the RT_HTML type.

For example:

Let's say you have in a directory the following files: Index.htm, Image1.jpg, Image2.bmp. Index.htm is mapped to RT_HTM, Image1.jpg to RT_HTML, and Image2.bmp to RT_BITMAP.

Points of interest

The AddResource tool maps the source resource file in its address space using the Win32 file mapping API, and calls Win32 API resource functions to update the target executable. It uses a mutex to synchronize the update on the same executable. Check the source code for more.

Please feel free to add any comments \ suggestions, I'll be happy to help.

License

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