![]() Delphi™ | C++Builder™ Technical Articles |
|
Delphi™ | C++Builder™ Technical Articles CALLING CLASS METHODS OR STATIC FUNCTIONS — AN INTRODUCTION FOR APPLICATION DEVELOPERS COMPILING RESOURCE (.RES) FILES FROM RESOURCE COMPILER (.RC) TEXT FILE PRESCRIPTIONS, AND USING LoadFromResourceName PAGED, MULTIPLE WORKSPACE (PMW) APPLICATION DEVELOPMENT MODEL GOALS OF THE PAGED, MULTIPLE WORKSPACE (PMW) APPLICATION DEVELOPMENT MODEL HOW TO BUILD SUPERIOR PAGED, MULTIPLE WORKSPACE (PMW) APPLICATIONS ROOT CLASSES — OOP BASE CLASSES WE CAN WORK WITH VITAL FIXES FOR FUNDAMENTAL OWNERSHIP AND STREAMING PROBLEMS IN THE DELPHI™ IDE |
|
COMPILING RESOURCE (.RES) FILES FROM RESOURCE COMPILER (.RC) TEXT FILE PRESCRIPTIONS |
|
Resource (.res) files are an intermediate container for resources such as bitmaps or text, which may later be compiled into applications, components, or DLLs. This article documents how and when to compile resource (.res) files from .rc text file prescriptions. |
|
ADVANTAGES OF DIRECT RELIANCE ON THE RESOURCE COMPILER |
|
Visual GUIs are not always the "easy," most efficient, or most reliable way to get a job done. "Manual" compilation of resource (.res) files from .rc text file prescriptions overcomes the limitations of visual resource editors, and substantially automates your work:
The following techniques allow application developers to compile or re-compile resources with a single click. |
|
WHEN TO UTILIZE COMPILED .RES FILES |
|
Resource (.res) files should be routinely compiled into distributables for the following reasons:
|
|
THE RESOURCE COMPILER (.RC) FILE — A PRESCRIPTION FOR THE COMPILED RESOURCES |
|
Resource Compiler (.rc) files are built with generic text editors, and are simply text file prescriptions which declare on each line, the run time reference names and source files of resources to be compiled into a .res file. |
|
THE RESOURCE (.RES) FILE — INTERMEDIATE CONTAINER FOR RESOURCES |
|
Resource (.res) files are generated by the resource compiler, and ultimately are compiled into applications, components, or dynamic link libraries by your regular application compiler, which heeds linking instructions indicating the .res resources which thus are internalized into your project. |
|
ORGANIZATION GUIDELINE |
Two clicks generate the .res file (1/2) and copy it to the project directory (2/2). |
|
CONVENTIONS — UPPERCASE NAMES WITH UNDERSCORE CHARACTERS |
|
To avoid undocumented problems (or rules), we use uppercase alpha-numeric resource and file names with underscore characters. |
|
WRITING THE .RC FILE |
|
Each line of the .rc text file prescribes:
In the following example .rc prescription statement,
Because our source file is identified by the resource name, we can simply copy source file names into the .rc prescription. |
|
EXAMPLE .RC PRESCRIPTION STATEMENT |
|
QUICKLY WRITING ERROR FREE .RC FILE PRESCRIPTIONS |
|
To quickly build a prescription, copy a template block into enough lines for the .rc file you need to build. You require 1 line per resource. |
|
EXAMPLE REPLACEMENT TOKEN STATEMENTS (10 ONLY) |
|
Now open Windows Explorer to your source directory so that you can select file names to paste into your .rc prescription. Because our source files are identified by the intended run time resource names, we can quickly build each line of the .rc prescription file by,
|
|
EXAMPLE AISUPC.RC FILE CONTENT |
|
INVOKING THE RESOURCE COMPILER WITH A BATCH FILE RUN BY A SHORTCUT |
|
Here, we use an AISUPC.BAT batch file to run the Borland RC Compiler, BRCC32.EXE, with the switch -r, passing AISUPC.RC as the .rc file argument.
|
|
EXAMPLE AISUPC.BAT COMPILER FILE CONTENT |
|
ERRORS AND OUTPUT |
|
|
COPYING THE COMPILED .RES FILE TO YOUR PROJECT DIRECTORY WITH A BATCH FILE RUN BY A SHORTCUT |
|
Because the output .res file lands in the same directory, now we will want to copy the .res file to destination directories which may compile it into executables. This process too can be automated into a single click. A batch file invoked by another shortcut copies our .res output file to destination directories: |
|
AISUPC_COPY_RES_TO_DESTINATION.BAT |
|
REVISING YOUR RESOURCE FILE |
Now we can use our resource file. |
|
LINKING THE .RES FILE FROM APPROPRIATE DELPHI OR C++BUILDER UNITS |
|
Linking causes the .res file to be built into your application, component or DLL during regular project compiles. To link your .res file into a Delphi project, place a $R ("Resource") compiler directive and resource filename statement under the implementation keyword of the bottom-most unit referring to the resources. If UnitC uses UnitB which uses UnitA, and if all of these units might rely on any of the embedded resources, link to the .res file from UnitA. |
|
EXAMPLE RESOURCE COMPILER DIRECTIVE |
|
LINKING RESOURCES INTO A DELPHI DLL |
|
One way to bundle compiled resources with distributables is to compile your resource file into a DLL. To build compiled resources into a Delphi DLL, all you have to do is add the $R directive to the project skeleton generated by the IDE:
|
|
DELPHI EXAMPLE — A COMPLETE RESOURCE DLL |
|
LINKING A RESOURCE FILE INTO A C++BUILDER PROJECT OR DLL |
|
The equivalent C++ Builder™ DLL wizard generates all of the following code but the necessary #pragma resource statement: |
|
C++ EXAMPLE — A COMPLETE RESOURCE DLL |
|
USING LoadFromResourceName |
|
We must call LoadFromResourceName() to assign resources to Delphi and C++Builder objects. |
|
PACKAGED COMPONENTS — DYNAMICALLY LOADING IMAGES WITH LoadFromResourceName |
|
The following examples assign a MT_CLONE image resource to the Glyph property of MySpeedbutton: |
|
DELPHI EXAMPLE C++ EXAMPLE |
|
APPLICATIONS — DYNAMICALLY LOADING IMAGES FROM DLLs WITH LoadFromResourceName |
|
To load images from DLLs, we need to declare a THandle (Delphi) or HANDLE (C++): |
|
DELPHI EXAMPLE C++ EXAMPLE |
|
ADDING .RC FILES TO PROJECTS? |
|
The Delphi and C++Builder development environments allow developers to add RC files to projects. In our experience however, this feature does not necessarily link the .res file to the appropriate unit; nor is it useful to recompile resources every time the project is compiled. |
|
CAVEATS — BROKEN PRODUCTS |
|
Why are developers embedding resources into DLLs, instead of directly into their executables? C++ debug sessions show that there are problems casting HInstance to an unsigned integer. The compiler asks for an unsigned integer, but the value of the necessary HANDLE is incorrectly cast to unsigned int: |
|
LONG EXISTING C++Builder AND DELPHI BUG? |
|
RECOMMENDED REFERENCES |
|
Calvert provides excellent teaching of .res and .rc topics in his "Delphi Unleashed" series. |
|
• PRECEDING
|
NEXT •
|
|
|