Explicitly Linking to Classes in DLL's

33426 단어
Posted by
V. Rama Krishna on
January 18th, 2000
 
Sometimes Explicit Linking to DLL's is advantageous over implicit linking. For example, if at runtime the DLL is not found the application can display an error message and still continue. Explicit linking is also useful if you want users to provide a plugin for your application, in which case you could explicitly load the dll and call some predefined set of functions in it.
Explicit linking to global(non-member) C/C++ is quite easy. For example, suppose you wan't to call to a function ExportedFn in a dll. You can simply export the function like this (or through the def file):-

  
  
  
  
  1. extern "C" _declspec(dllexport)
  2. void ExportedFn(int Param1, char* param2);

The extern "C"linkage specification is required because otherwise C++ compiler generates a decorated name for the function and the function would not be exported with the name "ExportedFn"instead it would be exported something like "??ExportedFn@QAEX". If this function resides in a DLL called DLL1.dll a client exe can call this function simply like this :-

  
  
  
  
  1. HMODULE hMod = LoadLibrary("Dll1.dll");
  2. typedef void (*PExportedFn)(int, char*);
  3. PExportedFn pfnEF = (PExportedFn)GetProcAdress("ExportedFn");
  4. pfnEF(1, "SomeString");

But what if you want to export a bunch of member functions of a C++ class and link to them explicitly. There are two problems. The first problem is that C++ member function names are decorated names (Specifying extern "C"does not help). The second problem is that C++ language specifications do not allow pointer to member functions to be converted into other types (later on I will present a simple way to do that). These two problems do restrict exporting of C++ classes in DLL's. In this article I will show some ways by which we could overcome these restrictions and explicitly load classes from dlls.
I am going to present two methods in this article and I will cover another method (delegation) in an other article.
The three methods which I am going to cover in this article are :-
  • Using virtual functions table or vtable. This is the way COM operates.
  • Using direct calls through GetProcAddress.

  • I will be taking the following example class for the purpose if this article.
    
      
      
      
      
    1. class A
    2. {
    3. private:
    4. int m_nNum;
    5. public:
    6. A();
    7. A(int n);
    8. virtual ~A();
    9. void SetNum(int n);
    10. int GetNum();
    11. };

    I have provided two different implementations of the class in two different DLLs. A client exe allows the user to enter the name of the dll from which the class should be loaded and accordingly output the results. You can download the sample project from this link The sample contains one workspace known as ExpClass.dsw, which has three projects one each for the two dlls and one for the client exe. The code demonstrates both the methods.

    Exporting Class Using VTable


    This method is the basis of COM. When we declare member function(s) of a class as virtual, compiler creates a table of all the virtual functions in the order in which they appear in the declaration. When an object of that class is created the first four bytes of the object point to that table. If we change the declaration of the class A to be :-
    
      
      
      
      
    1. class A
    2. {
    3. private:
    4. int m_nNum;
    5. public:
    6. A();
    7. A(int n);
    8. virtual ~A();
    9. virtual void SetNum(int n);
    10. virtual int GetNum();
    11. };

    Now a table is generated by the compiler which has the three virtual functions - the destructor, SetNum and GetNum. (You can actually observe that a virtual function table is created if you list the assembly with the source code. You can change the project options for that).
    Now the object needs to be created in the dll. Since we are going to link only explicitly we need some global exported functions that create an object of the class through operator new. Since there are two constructors we can create two functions : CreateObjectofA() and CreateObjectofA1(int) and export them. Finally the exe can use the object as
    
      
      
      
      
    1. typedef A* (*PFNCreateA1)();
    2. PFNCreateA1 pfnCreateA1 =
    3. (PFNCreateA1)GetProcAddress(hMod, TEXT("CreateObjectofA1"));
    4. A* a = (pfnCreateA1)();
    5. a->SetNum(1);
    6. _tprintf(TEXT("Value of m_nNum in a is %d
      "
      ),a->GetNum());
    7. delete a;

    The important thing to note is that CreateObjectofA creates the the class using operator new this allows the client exe to safely call opeartor delete.
    
      
      
      
      
    1. extern "C" __declspec(dllexport) A* CreateObjectofA1()
    2. {
    3. return new A();
    4. }

    This method is very useful if you want users to make plugins for your applications. The drawback of this method is that the memory for the class must always be allocated in the dll. If the client wants to create an object by allocating memory in a different way it can't do so.
    The next method involves obtaining the functions directly through GetProcAddress and calling the functions. The trick is to convert the FARPROC returned by GetProcAddress into C++ pointer to member functions. Fortuantely through C++ facility of templates and union this could be don very easily. All that needs to be done is to define a function like this
    
      
      
      
      
    1. template
    2. Dest force_cast(Src src)
    3. {
    4. union
    5. {
    6. Dest d;
    7. Src s;
    8. } convertor;
    9. convertor.s = Src;
    10. return convertor.d;
    11. }

    The above function lets us cast variables of any different types and is more powerful then reinterpret_cast. For example, if we define a pointer type
    
      
      
      
      
    1. typedef void (A::*PSetNum)(int);

    We can convert a pointer fp which is of type FARPROC to PSetNum simple by using.
    
      
      
      
      
    1. FARPROC fp;
    2. .
    3. .
    4. PSetNum psn = force_cast<PSetNum>(fp);

    The above operation is not possible through reinterpret_cast or C-Style cast.
    Having found a way to convert FARPROC to a pointer to member, let's see ways to export C++ class member functions through friendly names. This can be done through .def files.
    The first step is to find the decorated names of each of the functions that need to be exported, this can be done through either through map file or by generating assembly listing. Then the functions can be exported by friendly names through the following .def file syntax :-
    
      
      
      
      
    1. EXPORTS
    2. ConstructorOfA1 = ??0A@@QAE@XZ PRIVATE
    3. ConstructorOfA2 = ??0A@@QAE@H@Z PRIVATE
    4. SetNumOfA = ?SetNum@A@@UAEXH@Z PRIVATE
    5. GetNumOfA = ?GetNum@A@@UAEHXZ PRIVATE
    6. DestructorOfA = ??1A@@UAE@XZ PRIVATE

    Now the functions are exported through much more friendly names. Now here is the way by which these are called
    
      
      
      
      
    1. typedef void (A::*PfnConstructorOfA1)();
    2. typedef void (A::*PfnConstructorOfA2)(int);
    3. typedef void (A::*PfnDestructorOfA)();
    4. typedef void (A::*PfnSetNumOfA)(int);
    5. typedef int (A::*PfnGetNumOfA)();
    6. A* a1 = (A*)_alloca(sizeof(A));
    7. PfnConstructorOfA1 pfnConsA =
    8. force_cast<PfnConstructorOfA1>(GetProcAddress(hMod,
    9. TEXT("ConstructorOfA1")));
    10. (a1->*pfnConsA)();
    11. PfnSetNumOfA pfnSetNumA =
    12. force_cast<PfnSetNumOfA>(GetProcAddress(hMod,
    13. TEXT("SetNumOfA")));
    14. (a1->*pfnSetNumA)(1);
    15. PfnGetNumOfA pfnGetNumA =
    16. force_cast<PfnGetNumOfA>(GetProcAddress(hMod,
    17. TEXT("GetNumOfA")));
    18. _tprintf(TEXT("Value of m_nNum in a is %d
      "
      ),(a1->*pfnGetNumA)());
    19. PfnDestructorOfA pfnDestA =
    20. force_cast<PfnDestructorOfA>(GetProcAddress(hMod,
    21. TEXT("DestructorOfA")));
    22. (a1->*pfnDestA)();

    An interesting things to note here is that constructors and destructors are both being called explicitly. This is perhaps only way by which class constructors could be invoked explicitly. The other point to observe is that the object has been allocated memory over the stack through function alloca (if want to allocate memory on heap you need to use malloc), this is because allocating memory through new or by just decalaring an object of type A the constructor of A is automatically called. We don't want't this because the constructor of A is implemented in the Dll and we have not implicitly linked to the dll. You need not explicitly call the constructor and destructor instead you could implement the constructor in you exe file as :-
    
      
      
      
      
    1. A::A()
    2. {
    3. static PfnConstructorOfA1 pfnConsA1 =
    4. force_cast<PfnConstructorOfA1>
    5. (GetProcAddress(ClassLoader<A>::s_hMod,
    6. TEXT("ConstructorOfA1")));
    7. (this->*pfnConsA1)();
    8. }
    9. A::~A()
    10. {
    11. static PfnDestructorOfA pfnDestA =
    12. force_cast<PfnDestructorOfA>
    13. (GetProcAddress(ClassLoader<A>::s_hMod,
    14. TEXT("ConstructorOfA1")));
    15. (this->*pfnDestA)();
    16. }

    The above two implementations just delegate to the actual function in the dll at the same time allow you two declare and use an object of A in normal fashion. I will cover a better form of delegation in the next article. An important point to mention here is that you need not implement the functions SetNum, GetNum etc. if you only call them through pointers. The sample attached explains all the methods.

    Downloads

    Download demo project - 8.8 Kb

    좋은 웹페이지 즐겨찾기