Sign in to follow this  
P3D-BrandonR

Dynamic object and Dynamic DLL ...

Recommended Posts

Hello all, I have a question dealing with this situation I'm trying to pull off. What I'm wanting to do is have a class interface that sits between my exe, and dll. I call the DLL from the EXE dynamically and the DLL creates a new instance of the class and passes it back. The EXE side uses this new class variable throughout the EXE, just simply referencing it's calls which is really happening on the DLL side of things once called. At the apps end, the EXE can simply delete the variable it has referenced as the class from memory. I have some code here at the bottom just simply showing the basic idea of what I'm doing, it seems to create and reference the variable somewhat correctly on the EXE side, although it seems the functions on the DLL side are not getting called at all. Also, upon deleting the variable I have on my EXE side, created from the DLL, it of course breaks with an error (HEAP memory error). You can sorta see below how my layout is working. I have a class on the EXE side that is basically manager for the DLL, this is where its calling the CreateInterface call to the DLL side and passing back the reference to the object that called it. The other class is has created a reference to this DLL manager class, and also has a MyInterface variable that is keeps and calls the DLL Manager class to do its thing to the DLL and pass back the reference to the new object. Any help or suggestions I would greatly appreciate. I have other implementations, though not as neat and clean as this one, that do work and I can use. I just wanted to make a neater layout than the previous ones I had been using and exploring. I know I can simply make the DLL Static and just do as I please, but the whole concept here is to keep the DLL dynamic and allow for a simple Interface Class to be the connector between the two. Here is the current code I have as a Test:
///////////////////////////
// Interface Class
// Shared between EXE and DLL
///////////////////////////

class MyInterface
{
public:
	
	//Base Creation and destroy functions
	virtual HRESULT	Initialize()=0;
	virtual HRESULT	Destroy()=0;

};


///////////////////////////
// My Class
// DLL Side Implementation
// of Interface Class
///////////////////////////

#include "myinterface.h"

class MyClass : public MyInterface
{
public:
	MyClass(){};
	~MyClass(){};
	
	HRESULT		Initialize() 	{ return S_OK;};
	HRESULT		Destroy()	{ return S_OK;};

};


///////////////////////////
// DLL main CPP
//////////////////////////

#include "myclass.h"

#define DLLEXPORT		extern "C" _declspec(dllexport)

DLLEXPORT void* CreateMyClass()
{
     return static_cast< void* > (new MyClass());
}


///////////////////////////
// EXE main CPP
//////////////////////////

#include "myinterface.h"

MyInterface* SomeClass::DoCreate()
{

    //Attempt create MyInterface across to DLL.
    typedef void* (*LPFNDLLCALL)();
    LPFNDLLCALL		lpfnDllCall;

    lpfnDllCall = (LPFNDLLCALL)GetProcAddress(_hModDLL,
                                           "CreateMyClass");
   if (!lpfnDllCall)
   {
      return NULL;
   }

    return (MyInterface*)lpfnDllCall();

}

void SomeOtherClass::SomeOtherFunction()
{
   SomeClass* _someClass = new SomeClass();

   MyInterface* _myInterface = _someClass->DoCreate();

   //code continues ...

   delete _myInterface;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this