C + + Builder creates and invokes resources in the DLL

Source: Internet
Author: User

Some icons, pictures, cursors, sounds, etc. are often used in program development, and we call them resources (Resource). When multiple windows are used for the same resource, these common resources can be placed in a DLL file, so that the application executes faster because the location resource spends less time than locating the file on disk. Multiple resources are placed in one file, reducing the number of files, such as icons, pictures, cursors, sounds, and so on, to reduce the size of your application. Do not be afraid of users in the process of careless and damage to the resource file so that the program does not work properly. Of course, can not be made into DLLs, and the application put together, use the time and then LoadFromFile, but so easy to lose, once lost will affect the operation of the program.

The following describes how to load a resource into a DLL file and call it in the application.

First, create a resource file

First the resources you will use are placed under the same folder, create a new Notepad, and change the suffix name to. RC (This is the case with rc.rc file name). Note: Create a resource file name that is not the same as the project name, because BCB creates a project with the same resource files as the project name, and it is a good idea to save the resource file to the same folder as the project file. Open the created rc.rc file and write the resource information in the following format:

Resource Identifier keyword resource file name

Format Description:

① Resource Identifier: a specific label that is defined by itself when a resource is called in a program.

② Keyword: Identifies the resource file type, for example:

Wave: The resource file is a sound file;

Rcdata:jpeg file;

Avi:avi animation;

Icon: Icons file;

BITMAP: Bitmap file;

Cursor: Cursors file;

Rmi:rmi music files;

Midi:midi Music files

Exefile: Executable file

Jpeg:jpg Pictures

Exefile:exe executable file

③ resource file Name: Added resource file name (with extension, you can make a full file name with the pathname);

④ Example: If there are three graphics: "Open.bmp", "Save.bmp", "Ico.ico", the prize in the Rc.rc file to write the following information:

OPEN BITMAP open.bmp

SAVE BITMAP save.bmp

ICO ICON Ico.ico

Resource filenames can be unquoted or quoted. Save Rc.rc.

Ii. Creating a DLL project

1. Create a new DLL Wizard in C + + builder and select Use VCL on the popup dialog box, such as:

Click "OK" to complete the creation and save to the folder where the above resource files are located.

2, click "Project"/"Add to Project" (or SHIFT+F11) on the menu, select the Rc.rc file which is written above.

3. Add the following two functions in Unit1.cpp:


Loading BMP bitmaps

extern "C" __declspec (dllexport) hbitmap __stdcall Getbitmap (ansistring rcname);

Hbitmap __stdcall Getbitmap (ansistring rcname)

{return:: LoadBitmap (HInstance, Rcname.c_str ());}

Load ico icon

extern "C" __declspec (dllexport) hicon __stdcall GetIcon (ansistring);

Hicon __stdcall GetIcon (ansistring rcname)

{return:: LoadIcon (HInstance, Rcname.c_str ());



Where Rcname is the resource identifier.

4, Save the project, compile (ALT+F9), and generate Project1 (CTRL+F9). Now we get the Project1.dll.

Iii. calling resources in a DLL

Re-create a new project (application), add two bitbtn on Form1, and set its Caption property to "open" and "save", and write the following code under FORM1 onshow function:


void __fastcall tform1::formshow (tobject *sender)


Hbitmap __stdcall (*getbitmap) (ansistring); Defining function prototypes

Hicon __stdcall (*geticon) (ansistring); Defining function prototypes


HDL =:: LoadLibrary ("Project1.dll"); Load DLL

if (Hdl! = NULL)


Getbitmap= (hbitmap __stdcall (*) (ansistring)):: GetProcAddress (HDL, "Getbitmap");

Fetch function Entry Address

if (getbitmap!= NULL)

{Bitbtn1->glyph->handle=getbitmap ("OPEN");

Bitbtn2->glyph->handle=getbitmap ("SAVE");


Geticon= (Hicon __stdcall (*) (ansistring)):: GetProcAddress (HDL, "GetIcon");

Get function Entry Address

if (geticon!= NULL)

{icon->handle= GetIcon ("ICO");} Change the icon for a form

:: FreeLibrary (HDL);


else {MessageBox (Handle, "Cannot load resource project.dll!", "error", 48);}



Then run, the form's icon becomes an icon in the resource, and two bitbtn also loads the graphic.

To this has completed the invocation function of the resource in the DLL.

Iv. Other Resources

The above describes only the bitmap and icon access, respectively, to LoadIcon and LoadBitmap, for other resources to access the following:


Mage1->picture->bitmap->handle=loadbitmap (HINSTANCE, ' resource identifier ');

or Image1->picture->bitmap->loadfromresourcename (HINSTANCE, ' resource identifier ');


Screen->cursors[1]=loadcursor (HINSTANCE, ' resource identifier ');

Screen->cursors[2]=loadcursor (HINSTANCE, ' resource Identifier 2 ');

form1->cursor=1; For Form1 only

image1->cursor=2;//only for Image1

screen->cursor=1;//the entire application is changed

There are two sets of mouse defined here, the use of the words need to define the RC file two times the mouse resource files.


Application->icon->handle = LoadIcon (hinstance, ' resource identifier ');

AVI file

Add a Tanimate control (on the Win32 Control Panel) to the project and join it where needed:

Animate1->resname= "Myavi"; Resource identification

Animate1->active = True;

A little result in practice: Not all AVI resources can be played with tanimate components, and the program should be tested when programmed. If you encounter an AVI resource that cannot be played with the Tanimate component, you can detach it from the resource file and play it using the appropriate playback components such as Tmediaplayer. Remove the detached temporary files after use.

Wave file

#Include "MMSystem.h"

Char *wav_handle;

Hrsrc h = findresource (hinstance, "Mywav", "WAV");

Hglobal H1 = LoadResource (hinstance, h);

Wav_handle = (char *) Lockresource (H1);

PlaySound (Wav_handle,null, Snd_memory | Snd_async);

FreeResource (H1);

PlaySound (Wav_handle,null, Snd_memory | snd_async| Snd_loop);

Repeat play

JPEG pictures

#include "jpeg.hpp"

Tjpegimage *fjpg=new tjpegimage ();

Tresourcestream *fstream=new tresourcestream ((int) hinstance, "Myjpg", "JPEG");

Fjpg->loadfromstream (FStream);

Image2->picture->bitmap->assign (fjpg);

EXE file

First, separate, then execute.

Setcurrentdir (path); Set Current working directory

Tresourcestream &res = *new tresourcestream ((int) hinstance, ansistring ("process"), "Exefile");

String file=path+ "//process.exe";

Res. SaveToFile (file);

Delete &res;

Startupinfo si;

Process_information Pi;

ZeroMemory (&si,sizeof (SI));

Si.cb=sizeof (SI);

ZeroMemory (&pi,sizeof (pi));


Note that the Create_no_window is not shown here, and different parameters are set according to the requirements, please refer to MSDN.

Other resources

You can isolate the source files in the resource file, create a temporary physical file that exists under the application path, and then use the file in the appropriate type of component or method. Do not forget to delete the temporary file when the program exits.

For example:

String tmpdirectory;

Tresourcestream Myres;

Tmpdirectory = Extractfilepath (paramstr (0));

if (FileExists (tmpdirectory + "//music1.rmi"))

Myres = Tresourcestream->create (hinstance, "Music1", "RMI");

Myres->savetofile (tmpdirectory + "Music1.rmi");//separated from the resource file


Delete the program when it exits:

if (fileexists (tempdirectory + '//music1. RMI '))

DeleteFile (tempdirectory + '//music1. RMI ');

C + + Builder creates and invokes resources in the DLL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.