The steps and implementation method of writing DLL dynamic link library in C + + _c language

Source: Internet
Author: User
Tags instance method

This article illustrates the steps and implementation methods of C + + writing DLL dynamic link library. Share to everyone for your reference, specific as follows:

When writing C + + programs, you often need to write a class as a DLL for the client to invoke. Such a DLL can export an entire class, or it can export a method of this class.

First, export the entire class

The simple approach is simply to add _declspec (dllexport) between class and class names in the header file of the classes, while adding _declspec (dllimport) between the class and class names in another header file that is provided to the class used by the client caller. In order for a client program and DLL program to use a header file for the class, it is usually handled using macros and precompiled directives in the header file of the class. The following DLLTest.h:

#ifdef DLL_TEST_API
#else
#define DLL_TEST_API _declspec (dllimport)
#endif
Class Dll_test_api Cdlltest
{public
   :
     cdlltest ();
     ~cdlltest ();
     int Add (int a, int b);


DLLTest.cpp is as follows:

#define DLL_TEST_API  _declspec (dllexport)
#include "DLLTest.h"

In this way, DLL_TEST_API is defined as _declspec (dllexport) at the time the DLL is compiled, and it is defined as _declspec (dllimport) when the client program compiles.

To export one or some of the methods of this class

At this point, you need to place _declspec (dllexport) in front of the member function name, such as DLLTest.h:

#ifdef DLL_TEST_API
#else
#define DLL_TEST_API _declspec (dllimport)
#endif
Class cdlltest
{ Public
   :
      Cdlltest ();     ~cdlltest ();
      int DLL_TEST_API Add (int a, int b);

However, if this is just the case, when the client program #include the header file, after defining an object of the class (statically linking the DLL), the client program cannot link through, prompting the constructors and destructors not to parse, at which point the dlltest You need to add a DLL_TEST_API macro to the constructor and destructor before.

There's a problem here, of course, when the function of a class is exported, the name changes, we can add extern "C" to the function name, such as extern "C" Dll_test_api int Add (int a, int b), but this only solves the problem of name change in C and C + + calls , the reliable approach is to add a module definition file Def, where the name of the exported function is defined, and we'll see the sample later.

When the DLL is written, only the client program will be left to invoke the DLL, statically calling the DLL and dynamically calling the DLL.

A static method of calling a DLL

This method is simple, copy the DLLTest.h header and Dlltest.lib,dlltest.dll files to the current directory of the client program, #include<dlltest.h> in the client program, and then through #pragma Comment (lib, "DLLTest.lib") approach the Lib library, or increase the introduction of the Lib file in the client program's engineering properties.

You can then use the DLL in the client program as if you were using a local class, such as:

Cdlltest dlltest;
Dlltest.add (1,2);

Two, dynamic way to call a DLL

Dynamic invocation of this DLL, you need to modify this class.

First, add a global function in the DLLTest.cpp file that returns an instance of this class, so that when the client program calls the global function, it gets an instance of the class and can call the instance method of that class.

extern "C" _declspec (dllexport) cdlltest* getinstance ()
{return
   new Cdlltest;
}

Note: extern "C" only solves the compatibility problem between C and C + + compilers, and if you need to be compatible with other compilers, a reliable solution is to add a. def file with the following contents:

The LIBRARY "dlltest"
exports
getinstance = getinstance

This specifies that the name of the DLL's function export remains unchanged.

This allows the client program to obtain an instance of class by using this function. As follows:

First you need to define a function pointer type:

typedef cdlltestbase* (*pfgetinst) ();
Note: The Cdlltestbase class is described later.
Hmoudle hmod = LoadLibrary (_t ("DLLTest.DLL"));
if (hmod)
{
  Pfgetinst pfgetinstance = (pfgetinst) GetProcAddress ("getinstance");
  The IF (p)
  {
   //) points to the derived class object through the base class pointer
    cdlltestbase * pinst = Pfgetinstance ();
   if (NULL!= pinst)
   {
    pinst->add (1,2);
   }   if (NULL!= pinst)
   {
    //free object
    delete pinst
   }
}  

Of course, there is still a need to include the header file DLLTestBase.h for this DLL, If you copy the previously written header file DLLTest.h directly to the current directory of the client program and include it in, you cannot pass it when compiling the connection, we need to modify the header file to add a. h first. File DLLTestBase.h, in which we will name the functions that need to be called in the client program as pure virtual functions, then let the Cdlltest class inherit from the Cdlltestbase class, DLLTestBase.h as follows:

Class cdlltestbase
{public
  :
    virtual ~cdlltestbase () {};//virtual destructor, and for inline function
     virtual int Add (int a, int b) = 0;
}

DLLTest.h modified as follows:

#include "DLLTestBase.h"
Class cdlltest:public cdlltestbase
{public
  :
    cdlltest ();
    ~cdlltest ();
    int Add (int a, int b);


Note: The dlltestbase here needs to provide a virtual destructor so that the derived class object can be freed from the base class pointer in the client program.

In this way, simply copy the DLLTestBase.h to the current directory of the client program, and then #include "DLLTestBase.h" in the client program, as described above, to invoke the method inside the DLL in the client program.

I hope this article will help you with VC + + program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.