Click here to Skip to main content
Licence 
First Posted 7 Sep 2003
Views 105,273
Bookmarked 50 times

UpdateResource

By | 7 Sep 2003 | Article
How to manipulate raw resources (RT_RCDATA) using UpdateResource().

Introduction

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.

HANDLE hFile;
DWORD dwFileSize,      
      dwBytesRead;
LPBYTE lpBuffer;

hFile = CreateFile(L"C:\\Winnt\\System32\\calc.exe", GENERIC_READ, 
                   0,
                   NULL,
                   OPEN_EXISTING,
                   FILE_ATTRIBUTE_NORMAL,
                   NULL);

if (INVALID_HANDLE_VALUE != hFile)
{
    dwFileSize = GetFileSize(hFile, NULL);

    lpBuffer = new BYTE[dwFileSize];

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

    delete [] lpBuffer;        
    
    CloseHandle(hFile);
}

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, 
        RT_RCDATA, 
        MAKEINTRESOURCE(104), 
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
        (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            
            }
        }    
    }

    FreeLibrary(hLibrary);
}

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,
      dwBytesWritten;
HANDLE hFile;

dwFileSize = SizeofResource(hLibrary, hResource);

hFile = CreateFile(L"C:\\Winnt\\Temp\\calc2.exe",
                   GENERIC_WRITE,
                   0,
                   NULL,
                   CREATE_ALWAYS,
                   FILE_ATTRIBUTE_NORMAL,
                   NULL);

if (INVALID_HANDLE_VALUE != hFile)
{
    WriteFile(hFile, lpBuffer, dwFileSize, &dwBytesWritten, NULL);

    CloseHandle(hFile);
}

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.

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

About the Author

DavidCrow

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

Member


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


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCursor Resource Pinmembernenfa3:57 23 Jul '10  
AnswerRe: Cursor Resource PinmvpDavidCrow4:51 23 Jul '10  
GeneralRe: Cursor Resource Pinmembernenfa22:52 26 Jul '10  
QuestionRe: Cursor Resource PinmvpDavidCrow2:56 27 Jul '10  
GeneralBITMAP Pinmemberfilipe_madureira5:27 29 May '09  
GeneralRe: BITMAP PinmvpDavidCrow10:49 29 May '09  
GeneralRe: BITMAP Pinmemberfilipe_madureira22:31 1 Jun '09  
QuestionIs it possible to execute the data? Pinmemberflaushi2:30 20 Mar '09  
AnswerRe: Is it possible to execute the data? PinmvpDavidCrow2:47 20 Mar '09  
GeneralRe: Is it possible to execute the data? Pinmembermarc ochsenmeier2:01 6 Jul '10  
QuestionRe: Is it possible to execute the data? PinmvpDavidCrow3:29 6 Jul '10  
AnswerRe: Is it possible to execute the data? Pinmembermarc ochsenmeier3:39 6 Jul '10  
GeneralThe article helped a lot Pinmemberadenissov12:30 18 Nov '08  
AnswerA way for updating application ICON (from .ICO file) PinmemberElad raz6:35 15 Oct '08  
QuestionRe: A way for updating application ICON (from .ICO file) PinmvpDavidCrow6:42 15 Oct '08  
AnswerRe: A way for updating application ICON (from .ICO file) PinmemberElad raz6:48 15 Oct '08  
GeneralRe: A way for updating application ICON (from .ICO file) PinmvpDavidCrow6:55 15 Oct '08  
GeneralBe sure to apply your checks.. PinmemberChizI5:47 2 May '08  
GeneralRe: Be sure to apply your checks.. PinmvpDavidCrow5:53 2 May '08  
QuestionIcon resources PinmemberArnorBld20:13 11 Dec '06  
AnswerRe: Icon resources PinmemberDavidCrow4:19 13 Dec '06  
GeneralRe: Icon resources PinmemberArnorBld7:05 14 Dec '06  
GeneralJust cant get any better PinmemberRajesh R. Subramanian1:14 28 Jun '06  
GeneralRe: Just cant get any better PinmemberDavidCrow2:28 28 Jun '06  
GeneralUpdateResource RT_ICON PinmemberCG2i033:13 31 May '06  
Hi !
 
How to make with an icon RT_ICON ?
The resource is written, but not correctly.
 
Thanks !

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 8 Sep 2003
Article Copyright 2003 by DavidCrow
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid