Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++
Article

Selectively copy resources between binary (EXE/DLL) files

Rate me:
Please Sign up or sign in to vote.
3.38/5 (6 votes)
21 Dec 20052 min read 56.8K   1.2K   18   11
A small program to selectively copy resources from a binary (EXE/DLL) file into a target binary (EXE/DLL) file. Can be used for resource based localization purposes.

Sample Image

Introduction

This program demonstrates how you can selectively copy some resources from other binary (EXE/DLL) files into a target binary (EXE/DLL) file. The best place it can fit in might be in application localization (if the localization is based on resource modification). If you have other nice usages, please share the information with me.

Background

I was assigned to do the localization for a WinXP 64 bit version application (based on resource modification). The guy who did the 32 bit version localization told me I need to manually adjust the dialog box size to avoid cutting off strings for some languages. Basically, I hate this kind of work, so I wanted to find some other solution.

The original idea was: the 32 bit localization was done, why not just dump all 32 bit application resources into a 64 bit program? After I read MSDN for a little while, I found that there were two exciting examples in MSDN, and the combination of them is exactly what I wanted. And I did so.

After I tried to localize the first round, I found I only wanted to selectively copy those localized resources from the 32 bit program. Else I'd end up screwing up something, for instance, the version number.

Using the code

You'll need to run this program from the command line. To get localized resources from Target_LC.exe and put them into Target.exe will roughly be:

CopyRes.exe Target_LC.exe Target.exe

The code itself is very straightforward with comments. Here is an example for those steps inside a C code:

For example, we have : Target_64bit.exe[English Version - EN] 
        and Target_32bit.exe[Localized Version - LC]
Want to generate: Target_64bit.exe[Localized Version - LC]

Step 1. copy Target_64bit.exe[EN] to Target_64bit.exe.tmp[EN]
Step 2. Use LoadLibraryEx() to load 
            Target_64bit.exe.tmp[EN] & Target_32bit.exe[LC]
Step 3. Use BeginUpdateResource() to clean all 
        existing resources in Target_64bit.exe[EN]
Step 4. Foreach Resource Types in Target_64bit.exe.tmp[EN]
         if Resource Type is DIALOG/MENU/STRING
         // Localized Resources
           Use UpdateResource() to copy all that type resources
           From Target_32bit.exe[LC] into Target_64bit.exe[EN]
         else  // Keep other resources
           Use UpdateResource() to copy all that type resources
           From Target_64bit.exe.tmp[EN] into Target_64bit.exe[EN]
Step 5. Clean up activities.

Comments for some functions

You need LoadLibraryEx with DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE flags for maximum compatibilities. For example, if you want to replace a 64 bit application's resources: applying TRUE to BeginUpdateResource() will clean the existing resources.

All those callback functions (EnumTypesFunc, EnumNamesFunc, EnumLangsFunc) have a handy extra parameter that can be used for your own purpose.

Points of Interest

You actually not only can copy the resource, but also will be able to modify those resources on the fly. Read more in MSDN. One of the key ideas I learned was: it seems that a system does not allow you to modify the resource memory block (returned from LockResource()) directly. What I tried was copy that memory block first, then update it inside my own memory block, and then use it as the parameter for UpdateResource().

History

Shouldn't have any update since it's a small utility program and you can modify it for your own purposes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions

 
Generalquestion Pin
danamaria20-Oct-06 4:46
danamaria20-Oct-06 4:46 
GeneralRe: question Pin
Hao Hu20-Oct-06 12:06
Hao Hu20-Oct-06 12:06 
GeneralHi, I am quite interested in your topic Pin
xiongzhend19-Sep-06 20:50
xiongzhend19-Sep-06 20:50 
GeneralRe: Hi, I am quite interested in your topic Pin
Hao Hu19-Sep-06 22:26
Hao Hu19-Sep-06 22:26 
GeneralRe: Hi, I am quite interested in your topic Pin
Portatofe2-Oct-08 6:12
Portatofe2-Oct-08 6:12 
GeneralDialogs not copied Pin
bruce.james21-Mar-06 13:59
bruce.james21-Mar-06 13:59 
GeneralRe: Dialogs not copied Pin
Hao Hu22-Mar-06 9:24
Hao Hu22-Mar-06 9:24 
GeneralRe: Dialogs not copied Pin
bruce.james22-Mar-06 10:58
bruce.james22-Mar-06 10:58 
GeneralRe: Dialogs not copied Pin
Hao Hu22-Mar-06 11:49
Hao Hu22-Mar-06 11:49 
GeneralRe: Dialogs not copied Pin
bruce.james22-Mar-06 13:54
bruce.james22-Mar-06 13:54 
GeneralRe: Dialogs not copied Pin
Hao Hu22-Mar-06 17:02
Hao Hu22-Mar-06 17:02 

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.