Click here to Skip to main content
Click here to Skip to main content

Accessing class object present in exe from explicitly loaded DLL using Inheritance and virtual function

, 19 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Accessing class object present in exe from explicitly loaded DLL using Inheritance and virtual function.

Introduction 

This article will teach you How to access class object present in exe from explicitly loaded dll using Inheritance and virtual function . Please consult your seniors before using this method.I am not sure about side effects of this method .This article is usefull when you dont want to  expose your code to user and want use some class function from user exe in your DLL   

Background 

Should know how to use and create DLLs ,How to load DLL explicitly 

Using the code  

Here I am creating abstract class for interface between executable and DLL. User will derive from this class and override the function which he thinks will be used in DLL  

class A
{
public:

virtual void who()=0
{
}
}; 

The following code is used to derive user class from abstract class. You can create .h file for class A or just copy paste class declaration in user code:

//user application code
#include <windows.h>
#include<iostream>
#include<string.h>

using namespace std;
class A //abstract class for interface
{
  public: virtual void who()=0 { }
};

class B:public A
//user will derive class from class A which is provided by me for interface
{
  public: void who() { cout<<endl<<"Derived class";  }
  void what() { cout<<endl<<"nothing";  }
};

Now user need to pass this object pointer to DLL, so we need to create following exported function in DLL which will use base class pointer to take derived class pointer  

//DLL code 
extern "C" __declspec(dllexport) void setClassPtr(A *tempPtr) 

Following code will load DLL and get function address of setClassPtr() function. User will create object of derived class B and pass it to pointer of class A *tempPtreA and then tempPtreA will be passed to the function mySetPtr().

//user application code 
typedef void (WINAPIV* SETPTR )( A*);

int main()
{
    wchar_t dllname[100]=L"DLLproject.dll";
    HMODULE DLL_Handle;
    DLL_Handle = LoadLibrary(dllname);
    
    SETPTR mySetPtr = (SETPTR)GetProcAddress(DLL_Handle,"setClassPtr");
    
    A *tempPtreA=new B();
    mySetPtr(tempPtreA);
}

Now function setClassPtr() from DLL will get called. In the function first I am copying tempPtr to global pointer of class A Aptr. This is just for demonstration purpose that you can use this pointer anywhere. Now using Aptr I am calling who(). If you see output derived class function will get called since who is virtual function. Here DLL doesn't know the definition of derived class, still we are able access its function using base class pointer. You can use similar kind of abstract class for defining interface between DLL and ex 16 A *Aptr;

extern "C" __declspec(dllexport) void setClassPtr(A *tempPtr)
{
    Aptr=tempPtr;

    Aptr->who();
} 
output ::Derived class 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amit Deshmukh 1010
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
QuestionGood idea... Pinmemberimagiro15-May-13 0:44 
AnswerRe: Good idea... Pinmemberamitdeshmukh101019-May-13 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 19 May 2013
Article Copyright 2013 by Amit Deshmukh 1010
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid