An Easy Way to Make an MFC DLL

All developers, sooner or later, face the need/desire to write your own DLL. This operation is not always trivial, especially the first time. I have studied many examples and, after that, I chose the following solution. I believe that there is nothing more straightforward and simple.

Before I begin, it is appropriate to mention a few brief theoretical points. What is a a DLL? Or, perhaps, it might be better to ask what the difference is between an EXE and a DLL.

The two structures are almost identical; the only difference is that the DLL has an exports table (the name betrays its meaning; in fact, this table contains the names of all the functions that the DLL exports outwards (EXE or another DLL). Anything that is not present in this table will remain private at the DLL.

Usually, when you learn to use the DLL, projects take a new form; you start with one, and then you have two. You end with many different DLLs.

The DLL is a very versatile instrument, but it does not solve all problems; if you abuse DLLs, they can create problems. Before you start a new project, it is well worth studying its structure, so you don't drive yourself crazy when you compile it! In fact, the order of compilation is not random; it must be well studied. I do not think there are precise rules; I think that experience makes the difference.

My article gives a whole example with the DLL's sources and a minimal application that shows how to use it. I've created a solution that I called TestSolution; the solution is made by the application that I called TestEXE and by the DLL that I called TestDLL.


  1. Create the project of the application that will use the DLL (for convenience, I opted for a dialog based; but, of course, you could choose any other type: MDI, SDI, and so forth.
  2. Create the project DLL.
  3. Select MFC DLL.
  4. Select the MFC extension DLL option.

At this point, the solution is complete. Now, you will focus on the code....

Add to the TestDLL project a new file that includes what I'll call TestDLL.h.

TestDLL.h is perhaps the part where you should concentrate more. You must keep the following statement in your mind:

   #define TESTDLL_API __declspec(dllexport)
   #define TESTDLL_API __declspec(dllimport)

Also, in this case it is appropriate to give you some more information. Microsoft has introduced with the 16-bit compiler the keyword __export. This directive automatically generates the names of functions to be exported; these statements are in a .LIB file.

When it was introduced in the 32-bit compiler, the keyword was changed to __declspec(dllexport). This new statement, unlike the previous one, puts the list of functions to be exported directly in the object file, with the obvious advantage of not having to use the .DEF.

The EXE (or other DLL) that will use functions exported from a DLL should use the __declspec(dllimport) keyword. In substance, it will be enough to use the same .H file of the DLL; that exposes the functions to be exported and precedes all functions you want to import with the __declspec(dllimport) statement.

Of course, there's a more cunning method you can use; you can generate a macro (see TESTDLL_EXPORT) so, with a single .H file, you can export and import all the desired functions. It's a convenient, simple, and blank way!

It is absolutely necessary that you remember to add the following definition, TESTDLL_EXPORT, in the properties of the project DLL.

An Easy Way to Make an MFC DLL

All classes to be exported must be placed within the following statement. They must always be preceded by the following suffix: TESTDLL_API.

#ifdef __cplusplus
   extern "C" {
#endif    /* __cplusplus */


#ifdef __cplusplus

Now, you must put the statement of the file that includes the DLL in the include stdafx.h of the TestEXE project file.

#include "..\\TestDLL\\TestDLL.h"

Of course, especially in the presence of complex solutions composed of more that one DLL, it is necessary to indicate to the compiler the order in which you want all projects to be processed.


If you have followed all the steps in the TestEXE project, now you'll simply create an instance in the exported class to use all methods....

I hope this short article can be useful to you. Do not hesitate to contact me for further clarification.

class CTestEXEDlg : public CDialog
   CTestEXEDlg(CWnd* pParent = NULL);    // standard constructor



   CExportDLL m_exportDLL;

   // DDX/DDV support
   virtual void DoDataExchange(CDataExchange *pDX);

   virtual BOOL OnInitDialog();
   afx_msg void OnPaint();
   afx_msg void OnBnClickedPush();
   afx_msg void OnBnClickedPull();


  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • Employees must exchange sensitive emails with customers and partners. These emails might contain protected health information, protected financial information, or corporate information that should not be made public. Globalscape® Mail Express® allows you to encrypt the emails that it manages so that no one but the sender and recipient--not even the administrator--can view the contents of the email. "Secure the Transfer of Sensitive Emails" is the property of GlobalSCAPE

  • You may not realize the complexity you could be creating when developing mobile apps – many companies don't initially. You could be doubling your IT costs for development and delivery; not to mention, risking sales, productivity and brand satisfaction. Read this IBM-commissioned Forrester Study to understand the key cost drivers of mobile app delivery -- for both customer-facing and enterprise applications. Find out how you could lower costs and increase success with the right strategy and investment.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date