Talking about DLL export class in Windows

Source: Internet
Author: User
Tags export class

General DLL Export class method, a simple example:

DllExample.h:

1 #pragmaOnce2 3 #ifdef dll_exports4 #defineDll_api __declspec (dllexport)5 #else6 #defineDll_api __declspec (dllimport)7 #endif8 9 classDll_api ExportclassTen { One pirvate: A     intx; -  Public: -     voidfoo (); the};

DllExample.cpp:

1 #define Dll_exports2"dllExample.h"34void  Exportclass::foo ()5{6     //dosomething ... 7     return ; 8 }

The external code only needs to include the header file, it will automatically import the definition of Exportclass.
Compile-time connection to the DLL corresponding to the LIB, the runtime provides DLL files, can run normally.

However, there is a limit to this simple DLL export if we export a class that contains a non-C + + base type:

DllExample.h:

1 #pragmaOnce2 3 #ifdef dll_exports4 #defineDll_api __declspec (dllexport)5 #else6 #defineDll_api __declspec (dllimport)7 #endif8 9 classDll_api ExportclassTen { One pirvate: ASTD::stringX//the string type export here is not secure -  Public: -     voidfoo (); the};

We know that for STL, Microsoft has different implementations for each version of VS, VS2008 (VC90), VS2010 (VC100), VS2013 (VC120).
Due to the different implementations of STL, we can not see the direct transfer of std::string in different versions, otherwise unpredictable errors may occur during the run time.
In fact, the std::string x variable in our exportclass is not expected to be used externally, that is, there is no need to export, in fact, it is not recommended to let the DLL out of any definition of non-C + + base type.
But because Exportclass needs to be exported outward (because of the need to use the Foo () function), how should we deal with such contradictions?

For such a problem, we need to use the abstract class of C + + (which is actually the interface concept in Java) to solve:
We need to:
1. Declare a base class that has only pure virtual functions and C + + base types, and all definitions that need to be exported externally are included in the class.
2. Declare another class, inheriting the base class.
3. Implement a getinstance function that returns a pointer to a base class function, that is, a factory method that returns an instance of a derived class.
4. In external code, the class is accessed through a polymorphic mechanism.

DllExample.h:

1 #pragmaOnce2 3 #ifdef dll_exports4 #defineDll_api __declspec (dllexport)5 #else6 #defineDll_api __declspec (dllimport)7 #endif8 9 classDll_api ExportinterfaceTen { One  Public: A     Virtual voidFoo () =0; - }; -  the extern "C"Dll_api exportinterface*getinstance (); -  -#ifdef Dll_exports//we do not need to export the definition of the class outward, nor do we need to include the definition of this class when the external code is compiled.  - classExportclass: PublicExportinterface + { - pirvate: +STD::stringX//because external code is not visible to this, the std::string here are safe.  A  Public: at     voidFoo ();//function body implemented in DllExample.cpp - }; - #endif

DllExample.cpp:

  #define  dll_exports #include   " dllexample.h  "  extern   c   " Dll_api exportinterface* getinstance () {exportinterface  * pinstance = new   Exportclass ();  return   pinstance;}  void   Exportclass::foo () { // do something ...  return  ;}  

Talking about DLL export classes in Windows

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.