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.

So...

  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:

#ifdef TESTDLL_EXPORT
   #define TESTDLL_API __declspec(dllexport)
#else
   #define TESTDLL_API __declspec(dllimport)
#endif

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 */


class TESTDLL_API CExportDLL
{
};

#ifdef __cplusplus
   }
#endif

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.

[GL6.jpg]

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
{
public:
   CTestEXEDlg(CWnd* pParent = NULL);    // standard constructor

   enum { IDD = IDD_TESTEXE_DIALOG };

protected:

   CExportDLL m_exportDLL;

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

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


Comments

  • 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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Mobile devices, social business apps, and business analytics are converging with the Cloud to create the most substantial changes in technology since the Internet revolution. Businesses have to change the way they think and operate, and with rising budgets for technology, they need someone to provide the services that will keep them competitive in this environment. Learn more about the important technology trends you need to stay on top of to ensure your business doesn't get left behind.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds