Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C++

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

Rate me:
Please Sign up or sign in to vote.
4.17/5 (9 votes)
1 Dec 2007CPOL3 min read 55.9K   734   23   20
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)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIt doesn't work for cur (mouse resource) Pin
nenfa20-Jul-10 10:17
nenfa20-Jul-10 10:17 
GeneralConverting this to a dll to use in vb.net Pin
Blizo26-Apr-10 10:06
Blizo26-Apr-10 10:06 
GeneralCa'nt see resource file (Please Help!" Pin
R.B Developments20-Feb-10 8:35
R.B Developments20-Feb-10 8:35 
GeneralRe: Ca'nt see resource file (Please Help!" Pin
kamnas20-Feb-10 11:32
kamnas20-Feb-10 11:32 
GeneralRe: Ca'nt see resource file (Please Help!" Pin
R.B Developments20-Feb-10 20:33
R.B Developments20-Feb-10 20:33 
QuestionHaving trouble adding RT_MANIFEST resource Pin
Member 43978652-Dec-09 10:35
Member 43978652-Dec-09 10:35 
GeneralI am trying to add a resource file (test.resources) Pin
dbc user16-Sep-08 3:08
dbc user16-Sep-08 3:08 
GeneralRe: I am trying to add a resource file (test.resources) Pin
dbc user16-Sep-08 3:14
dbc user16-Sep-08 3:14 
GeneralRe: I am trying to add a resource file (test.resources) Pin
kamnas16-Sep-08 4:08
kamnas16-Sep-08 4:08 
GeneralRe: I am trying to add a resource file (test.resources) Pin
dbc user17-Sep-08 19:26
dbc user17-Sep-08 19:26 
GeneralRe: I am trying to add a resource file (test.resources) Pin
dbc user18-Sep-08 8:11
dbc user18-Sep-08 8:11 
QuestionPlus Pin
WillianBR21-May-08 5:30
WillianBR21-May-08 5:30 
AnswerRe: Plus Pin
kamnas21-May-08 5:33
kamnas21-May-08 5:33 
Generalbad default app icon, need help Pin
Xuefer9-Mar-08 20:56
Xuefer9-Mar-08 20:56 
i've updated exe with main.ico which has 3 icons inside it, let's say small medium and big, but when i browse it from explorer, when i switch to small icon view (show as list), it display an big icon scaled to small, when i switch to big icon view , it display an small icon scaled to big

then i used msdev (vc6) to open the exe as resource and changed the icon to another language and changed it back (no change to icon), or just draw a dot in the icon so it is modified, or change any other string resource, then, save. and the exe icon becomes fine. why? how strange. any idea on how to fix it?
GeneralRe: bad default app icon, need help Pin
Xuefer9-Mar-08 22:03
Xuefer9-Mar-08 22:03 
GeneralThis does not work for icons! Pin
Gernot Walterskirchen29-Nov-07 6:52
Gernot Walterskirchen29-Nov-07 6:52 
GeneralRe: This does not work for icons! Pin
kamnas29-Nov-07 21:15
kamnas29-Nov-07 21:15 
GeneralRe: This does not work for icons! Pin
Gernot Walterskirchen30-Nov-07 22:59
Gernot Walterskirchen30-Nov-07 22:59 
GeneralRe: This does not work for icons! Pin
kamnas1-Dec-07 12:02
kamnas1-Dec-07 12:02 
GeneralRe: This does not work for icons! Pin
Gernot Walterskirchen15-Dec-07 12:03
Gernot Walterskirchen15-Dec-07 12:03 

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

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