Click here to Skip to main content
11,929,675 members (50,628 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked


, 7 Sep 2003
Rate this:
Please Sign up or sign in to vote.
How to manipulate raw resources (RT_RCDATA) using UpdateResource().


Over the years, I've seen various flavors of the question "How do I include a file or data with my program that I can extract at run-time and do something with it?" For example, the asker might have had a .WAV file or a .MDB (MS Access) file that s/he wanted to include in the actual executable itself. For one reason or another, they do not want to package the two files separately. One advantage this has over a separate data/configuration file is that it'll never get lost. If the user decides they want to start fresh, they can remove everything but the application itself, and when the application is started up again, it extracts an empty database, all without the user knowing the behind-the-scenes details.

So thre are really two questions on the table here: "How to include the file?" and "Once the file is included, how to extract it and 'use' it?" Let's look at a simple example.

Including the file

In this example, I'll show the necessary code to take the entire Windows calculator (calc.exe) and add it as a resource to some test .EXE. The .EXE could just as easily be the application where these code snippets reside. The first thing we must do is open calc.exe in read-only mode, and read the entire file into a buffer.

DWORD dwFileSize,      
LPBYTE lpBuffer;

hFile = CreateFile(L"C:\\Winnt\\System32\\calc.exe", GENERIC_READ, 

    dwFileSize = GetFileSize(hFile, NULL);

    lpBuffer = new BYTE[dwFileSize];

    if (ReadFile(hFile, lpBuffer, dwFileSize, &dwBytesRead, NULL) != FALSE)
        // do something with lpBuffer here

    delete [] lpBuffer;        

Updating the resource data

Now it's simply a matter of getting access to the resource data of the test file and updating it with the data pointed to by lpBuffer. The third parameter to UpdateResource() is the name we want to give the resource. I just chose an arbitrary name of 104.

HANDLE hResource;

hResource = BeginUpdateResource(L"C:\\...\\t3.exe", FALSE);
if (NULL != hResource)
    if (UpdateResource(hResource, 
        (LPVOID) lpBuffer, 
        dwFileSize) != FALSE)
        EndUpdateResource(hResource, FALSE);

At this point, you can actually open the test file with Visual Studio and see the addition of our data in the "Data" section. Be sure to open the file as a "resource"!

Extracting the data

So now your application has been delivered, and you need to extract the resource data.

Extracting the resource data uses functions from the same family as UpdateResource(). The required steps are to load the file containing the resource data, find the desired resource, load the resource, and then lock it.

HMODULE hLibrary;
HRSRC hResource;
HGLOBAL hResourceLoaded;
LPBYTE lpBuffer;

hLibrary = LoadLibrary(L"C:\\...\\t3.exe");
if (NULL != hLibrary)
    hResource = FindResource(hLibrary, MAKEINTRESOURCE(104), RT_RCDATA);
    if (NULL != hResource)
        hResourceLoaded = LoadResource(hLibrary, hResource);
        if (NULL != hResourceLoaded)        
            lpBuffer = (LPBYTE) LockResource(hResourceLoaded);            
            if (NULL != lpBuffer)            
                // do something with lpBuffer here            


Saving the data to a file

All that's left is to create a file with the data pointed to by lpBuffer. Nothing special needs to be done to the data. It's the entire Windows calculator!

DWORD dwFileSize,

dwFileSize = SizeofResource(hLibrary, hResource);

hFile = CreateFile(L"C:\\Winnt\\Temp\\calc2.exe",

    WriteFile(hFile, lpBuffer, dwFileSize, &dwBytesWritten, NULL);


Now you should be able to run the newly created file, calc2.exe! As I indicated at the beginning of the article, this can be done with any sort of file that you want to include along with your main application or DLL.


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


About the Author

Software Developer (Senior) Pinnacle Business Systems
United States United States

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

HTTP 404 - File not found
Internet Information Services

You may also be interested in...

Comments and Discussions

AnswerRe: Icon resources Pin
DavidCrow13-Dec-06 5:19
memberDavidCrow13-Dec-06 5:19 
GeneralRe: Icon resources Pin
ArnorBld14-Dec-06 8:05
memberArnorBld14-Dec-06 8:05 
GeneralJust cant get any better Pin
Rajesh R. Subramanian28-Jun-06 2:14
memberRajesh R. Subramanian28-Jun-06 2:14 
GeneralRe: Just cant get any better Pin
DavidCrow28-Jun-06 3:28
memberDavidCrow28-Jun-06 3:28 
GeneralUpdateResource RT_ICON Pin
CG2i0331-May-06 4:13
memberCG2i0331-May-06 4:13 
GeneralRe: UpdateResource RT_ICON Pin
DavidCrow31-May-06 5:28
memberDavidCrow31-May-06 5:28 
QuestionUpdate Resource within yourself? Pin
Persetech1-Feb-06 8:22
memberPersetech1-Feb-06 8:22 
AnswerRe: Update Resource within yourself? Pin
DavidCrow1-Feb-06 8:46
memberDavidCrow1-Feb-06 8:46 
Persetech wrote:
...but I'd like for the changes to the resource to actually be written to disk and persist across the next execution of the program.

If the process is running, and thus locked, I don't know how to do this.

"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

GeneralGreat! Works PERFECTLY!! Pin
eliran115-Apr-05 5:57
membereliran115-Apr-05 5:57 
GeneralHello Sir ! How To... Pin
ThatsAlok20-Jan-05 2:47
memberThatsAlok20-Jan-05 2:47 
GeneralRe: Hello Sir ! How To... Pin
kurackurcevic4-Dec-09 18:03
memberkurackurcevic4-Dec-09 18:03 
GeneralGreat, this really helps a lot! Pin
Paul.Huang29-Jan-04 23:23
memberPaul.Huang29-Jan-04 23:23 
GeneralArticle editing suggestion Pin
Gary Wheeler9-Sep-03 4:52
memberGary Wheeler9-Sep-03 4:52 
Generalyes, but Pin
eco8-Sep-03 22:04
membereco8-Sep-03 22:04 
GeneralRe: yes, but Pin
DavidCrow9-Sep-03 3:06
memberDavidCrow9-Sep-03 3:06 
GeneralRe: yes, but Pin
IGx8918-Sep-03 16:24
memberIGx8918-Sep-03 16:24 
GeneralRe: yes, but Pin
DavidCrow19-Sep-03 3:04
memberDavidCrow19-Sep-03 3:04 
GeneralRe: yes, but Pin
IGx8919-Sep-03 3:56
memberIGx8919-Sep-03 3:56 
GeneralRe: yes, but Pin
Furer Alexander27-Jan-04 3:35
memberFurer Alexander27-Jan-04 3:35 
GeneralRe: yes, but Pin
DavidCrow27-Jan-04 7:51
memberDavidCrow27-Jan-04 7:51 
GeneralRe: yes, but Pin
Michael R27-Jan-04 8:29
memberMichael R27-Jan-04 8:29 
GeneralRe: yes, but Pin
CPAVG23-Feb-05 22:24
memberCPAVG23-Feb-05 22:24 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 8 Sep 2003
Article Copyright 2003 by DavidCrow
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid