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

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

  • Although much publicity around computer security points to hackers and other outside attacks, insider threats can be particularly insidious and dangerous, whether caused by malice or employee negligence. In this report, you learn the eight most significant cybersecurity threats that could impact your organization (at any time), Forbes cited internal threats as No. 3, noting that internal attacks can be "the most devastating" due to the amount of damage privileged users can inflict and the type of data they can …

Most Popular Programming Stories

More for Developers

RSS Feeds

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