VC++, MFC - Regular Dlls

Limitation of CBitmap class is that it can load a bitmap only using the ID of the bitmap and not the filename.

Suppose we intend to load a bitmap image using its filename. To do so we can write a function in the form of a DLL so that it can be linked with any other application. DLLs also save memory and hard disk space.

We have used 'MFC AppWizard (dll)' to develop the project. In Step 1 we selected 'Regular DLL using shared MFC DLL'.  This helps us in creating a regular DLL from which we will be able to export functions and variables only. From this DLL a load( ) function has been exported. This function loads a bitmap image using ::LoadImage( ) function that accepts the bitmap filename and not the ID. The load( ) function also displays the bitmap at position (0, 0) in the window of the caller program.

The exported load( ) function accepts two parameters: full path of the bitmap filename and the device context on which to draw. It returns nothing hence the return type is void.
The use of dllexport attribute with the __declspec keyword will tell the linker that the function is available for use by other programs.
We have also specified extern "C" to prevent the C++ compiler from mangling the name of the function thinking of it as a C++ function. In C++ function overloading forces the compiler to mangle the names of the functions. Since function overloading is not possible in C, the programs written in C do not understand the mangled names. On omitting extern "C" the compiler would mangle the name and so programs written in C or any other languages that do not understand the mangled name will fail to use the DLL.

In addition to the DLL it is necessary to create a header file with the following declaration in it:

extern "C" __declspec ( dllimport ) void load ( CString str, CDC *p ) ;

The dllimport attribute with __declspec keyword will tell the linker that the display( ) function is available for use by other programs. This header file,
the DLL and the Lib file would be required by a user keen to use this DLL.


This program is a client program that uses an exported load( ) function from a regular DLL. The creation of this regular DLL has been discussed in the first article of the DLL section of this site.
To make the client program work you should download the program in the first article.

The client is a Doc/View application developed using AppWizard. The OnDraw( ) handler of the view class calls the load( ) function in the DLL. To make this call successful we have to carry out four steps:

(a) Include the header file supplied by the DLL
(b) Copy the file 'loaddll.dll' generated in the first article into the Windows directory or Windows System directory or the current directory of the client.
(c) Copy the file with 'loaddll.lib' in the current directory.
(d) In the 'Project | Settings | Link | Object libraries' edit box enter 'loaddll.lib'.

The '.lib' file provides all the necessary information about the exported function in the DLL to the client. While calling the exported load( ) function we have passed the bitmap filename with the full path and the device context of the window in which to be drawn. On execution we get the bitmap image in the window's client area of the client application.