Click here to Skip to main content
Click here to Skip to main content

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

, 1 Dec 2007
Rate this:
Please Sign up or sign in to vote.
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)

About the Author

kamnas

Romania Romania
No Biography provided

Comments and Discussions

 
GeneralIt doesn't work for cur (mouse resource) Pinmembernenfa20-Jul-10 10:17 
GeneralConverting this to a dll to use in vb.net PinmemberBlizo26-Apr-10 10:06 
GeneralCa'nt see resource file (Please Help!" Pinmemberroylab20-Feb-10 8:35 
GeneralRe: Ca'nt see resource file (Please Help!" Pinmemberkamnas20-Feb-10 11:32 
GeneralRe: Ca'nt see resource file (Please Help!" Pinmemberroylab20-Feb-10 20:33 
QuestionHaving trouble adding RT_MANIFEST resource PinmemberMember 43978652-Dec-09 10:35 
GeneralI am trying to add a resource file (test.resources) Pinmemberdbc user16-Sep-08 3:08 
GeneralRe: I am trying to add a resource file (test.resources) Pinmemberdbc user16-Sep-08 3:14 
GeneralRe: I am trying to add a resource file (test.resources) Pinmemberkamnas16-Sep-08 4:08 
GeneralRe: I am trying to add a resource file (test.resources) Pinmemberdbc user17-Sep-08 19:26 
GeneralRe: I am trying to add a resource file (test.resources) Pinmemberdbc user18-Sep-08 8:11 
QuestionPlus PinmemberWillian.BR21-May-08 5:30 
AnswerRe: Plus Pinmemberkamnas21-May-08 5:33 
Generalbad default app icon, need help Pinmembermoo.wejoin9-Mar-08 20:56 
GeneralRe: bad default app icon, need help Pinmembermoo.wejoin9-Mar-08 22:03 
GeneralThis does not work for icons! Pinmembergernotw29-Nov-07 6:52 
GeneralRe: This does not work for icons! Pinmemberkamnas29-Nov-07 21:15 
GeneralRe: This does not work for icons! PinmemberGernot Walterskirchen30-Nov-07 22:59 
GeneralRe: This does not work for icons! Pinmemberkamnas1-Dec-07 12:02 
GeneralRe: This does not work for icons! PinmemberGernot Walterskirchen15-Dec-07 12:03 

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 | Mobile
Web02 | 2.8.140709.1 | Last Updated 1 Dec 2007
Article Copyright 2007 by kamnas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid