Methods for writing DLLs in C + +

Source: Internet
Author: User

Http://files.cnblogs.com/files/profession/DllTest.zip

In writing C + +program, it is often necessary to write a class as a DLL for the client program to invoke. Such a DLL can export the entire class, or you can export a method of this class. First, it is very simple to export the entire class method, only need to add _declspec (dllexport) between the class and class names in the header file of the classes, and add _declspec ( dllimport). To enable client programs and DLL programs to share a header file for that class, you typically use macros and precompiled directives in the header file of the class to handle it. The following DLLTest.h: Copy code #ifdef DLL_TEST_API#else#defineDll_test_api _declspec (dllimport)#endifClass dll_test_api cdlltest{public:cdlltest (); ~cdlltest (); intADD (intAintb);}; Copy the code DLLTest.cpp as follows:#defineDll_test_api _declspec (dllexport)#include "DLLTest.h" ....... ............................ This way, DLL_TEST_API is defined as _declspec (dllexport) when the DLL is compiled, and it is defined as _declspec (dllimport) when the client program compiles. Export one or several methods of this class. At this point, you need to put _declspec (dllexport) in front of the member function name, such as DLLTest.h: Copy code #ifdef DLL_TEST_API#else#defineDll_test_api _declspec (dllimport)#endifClass cdlltest{public:cdlltest (); ~cdlltest (); intDll_test_api Add (intAintb);}; Copy code However, if this is the case, when the client program # # # # of the header file, after defining an object of the class Dlltest (statically linked DLL), the client program cannot be linked through, it will prompt the constructor and destructor can not be resolved, at this time, You need to add a DLL_TEST_API macro before the constructor and destructor. Of course, there is a problem here is that the function of the class after the export, the name will change, we can precede the function name with the extern "C", such asextern"C" Dll_test_apiintADD (intAintb); But this only solves the C and C + +when calling the name change problem, the reliable method is to add a module definition file Def, in which the name of the exported function is defined, we will see the sample later. After the DLL is written, only the client program is left to call the DLL, statically call the DLL and invoke the DLL in a dynamic manner. A static way to call the DLL this method is simple, copy the DLLTest.h header file and the Dlltest.lib,dlltest.dll file to the client program's current directory, in the client program # # #<dlltest.h>, and then through#pragmaComment (lib, "DLLTest.lib") introduces the Lib library, or adds 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 call DLL dynamic call this DLL, you need to modify this class. First, add a global function to the DLLTest.cpp file, which returns an instance of the class, so that the client program calls the global function and then gets an instance of that class to invoke the class's instance method. extern"C" _declspec (dllexport) cdlltest*getinstance () {return NewCdlltest;} Note:extern"C" only solves C and C + +compiler compatibility issues, if necessary and other compilers compatible, the reliable way is to add a. def file, the contents of the file are as follows: LIBRARY "Dlltest" Exportsgetinstance=getinstance The name of the function that specifies the DLL is still unchanged after the export. This allows the client program to obtain an instance of class from this function. As follows: First you need to define a function pointer type: Copy code typedef cdlltestbase* (*pfgetinst) ();//Note: The Cdlltestbase class is described later. Hmoudle Hmod=LoadLibrary (_t ("DLLTest.DLL"));if(hmod) {pfgetinst pfgetinstance=(Pfgetinst) GetProcAddress ("getinstance"); if(p) {//To point to a derived class object by a base-class pointerCdlltestbase * Pinst =pfgetinstance (); if(NULL! =pinst) {Pinst->add (1,2); }if(NULL! =pinst) { //Releasing ObjectsDelete pinst; }}} Copy the code of course, this is still required to include this DLL's header file DLLTestBase.h, if the previously written header file DLLTest.h directly copied to the client program's current directory, and include in the words, when compiling the connection, it is not through , we need to modify this header file first by adding a. h File DLLTestBase.h, in this file we will need to call in the client program functions are named pure virtual function, and then let the Cdlltest class inherit from the Cdlltestbase class, DLLTestBase.h as follows: Copy code class cdlltestbase{public:virtual~cdlltestbase () {};//virtual destructor, and is an inline functionVirtualintADD (intAintb) =0;} Copy code DLLTest.h modified as follows: Copy code # include "DLLTestBase.h" Class cdlltest: Publiccdlltestbase{public:cdlltest (); ~cdlltest (); intADD (intAintb);}; Copy Code 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. This way, simply copy the DLLTestBase.h to the client program's current directory, and then include "DLLTestBase.h" in the client program, you can call the DLL inside the client program as described above.

Methods for writing DLLs in C + +

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.