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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Flash technology is becoming more prominent in the storage industry. Offering superior speed and reliability when compared to traditional hard disk drives – flash storage is a flexible and increasingly cost-effective technology that can be used to optimize enterprise storage environments. This ebook explores the many uses and benefits of flash storage technology in the enterprise. Check it out to discover and learn all you need to: Optimize storage performance Leverage server flash as storage cache …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds