A Simple Installer

Environment: VC6 Win9x

For an application that consists of more than one file, it is often more convenient to distribute it as a single executable. This article demonstrates how to embed any file as a resource and how to turn that resource back into a file.

For each file you want to include, add a line to resource.h as follows:

  #define IDR_MYFILE 103

Remember to give each file a unique resource ID.

Close all resource files and open your .rc file as "text." Add the line

  IDR_MYFILE RCDATA DISCARDABLE "res\\myfile.ext"

A good place to add this line is immediately after the line which defines the IDR_MAINFRAME icon. If you haven't placed "myfile.ext" in your "res" subdirectory, adjust the path accordingly. Save the .rc file. Close it and open ResourceView. You should see something like this:

There are more convenient ways of inserting a custom data resource, but they either embed the file as raw hex data in the .rc file, inflating it to several times the size of the included file, or neglect to define the resource ID in resource.h so that ResourceView shows the ID in inverted commas.

To turn a resource back into a file, load it into memory using LoadResource(). This returns a BYTE pointer that CFile can use to write that file to disc.

HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_MYFILE),
                                                RT_RCDATA);

// Load the resource and save its total size.
DWORD dwSize = SizeofResource(NULL , hRes);
HGLOBAL MemoryHandle = LoadResource(NULL, hRes);
if(MemoryHandle != NULL){

  // LockResource returns a BYTE pointer to the raw data in
  // the resource
  BYTE *MemPtr = (BYTE *)LockResource(MemoryHandle);


  CFile file("C:\_my_path_to_file\myfile.ext", CFile::modeCreate |
                                               CFile::modeWrite);
  file.Write(MemoryHandle,dwSize);

}
FreeResource((HANDLE)hRes);

The demo project writes a .wav file to the Windows directory and changes a Registry key to make this .wav your default e-mail notification sound.

Downloads

Download source InstallDemo.zip - 108Kb


Comments

  • It does what it aims to do.....

    Posted by Legacy on 04/25/2003 12:00am

    Originally posted by: Richard 'vajuras' Osborne

    Works incredibly well if all you require is a simple uninstaller that uses custom code, etc.

    Reply
  • Good trick. Thanks.

    Posted by Legacy on 11/13/2002 12:00am

    Originally posted by: PixiGreg

    nice. Thanks.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds