Sign in to follow this  
Squirell

Dynamic Class Help

Recommended Posts

I'm trying to get dynamic classes working in my program. I have a static library that has a class (IRender). This has a function called CreateRenderDevice() which is supposed to load the D3DDriver DLL and return a pointer to the CD3DRenderDevice class (which is in the D3DDriver.dll). Anyway, when I create an instance of IRender in a .exe and call the CreateRenderDevice() function it does not return a pointer to the class. I have pinpointed the probelem to be with my GetProcAdress call. Here is the source code:
////////////////
//IRender.h
////////////////

#ifndef I_RENDER_H_INCLUDED
#define I_RENDER_H_INCLUDED

#include <iostream>
#include "CD3DRenderDevice.h"

using namespace std;

class IRender 
{
public:
	IRender();
	virtual ~IRender();

	virtual CD3DRenderDevice* CreateRenderDevice();
};

#endif

/////////////////
//IRender.cpp
/////////////////

#include "IRender.h"

IRender::IRender()
{

}

IRender::~IRender()
{

}

CD3DRenderDevice* IRender::CreateRenderDevice()
{
	PFNCREATEMYCLASS pfnCreateMyClass = NULL;
	CD3DRenderDevice* pMyClass = NULL;
	HMODULE	hDll = NULL;

	hDll = LoadLibrary ("D3DDriver.dll");

	if(!hDll)
	{
		cout << "Failed to load D3DDriver.dll\n";
		return	NULL;
	}

	cout << "Loaded D3DDriver library\n";

	pfnCreateMyClass = ( PFNCREATEMYCLASS) GetProcAddress (hDll, "CreateMyClass");

	if(pfnCreateMyClass)
	{
		//The code never enters this if block so I am assuming the GetProcAddress does not give me 
		//a valid pointer to CreateMyClass

		cout << "Obtained pointers to initalization functions\n";
		pMyClass = (pfnCreateMyClass)();
	}

	return pMyClass;
}

//////////////////////
//CD3DRenderDevice.h
//////////////////////

#ifndef C_D3D_RENDER_DEVICE_H_INCLUDED
#define C_D3D_RENDER_DEVICE_H_INCLUDED

#include <windows.h>

//I am defining _DLL in my project settings (However, it doesn't work if I undefine it either)

#ifdef _DLL // assume this is defined when we build the dll
#define _DYNLINK __declspec( dllexport)
#else
#define _DYNLINK __declspec( dllimport)
#endif

class CD3DRenderDevice
{
public:
	CD3DRenderDevice();
	~CD3DRenderDevice();
};

typedef void (CD3DRenderDevice::*PMYCLASSMETHOD)();

#ifndef _DLL
typedef CD3DRenderDevice* ( *PFNCREATEMYCLASS)();
#else
_DYNLINK CD3DRenderDevice* CreateMyClass() 
{ 
	return ( new CD3DRenderDevice());
}
#endif

#ifndef _DLL
typedef void ( *PFNDELETEMYCLASS)( CD3DRenderDevice*);
#else
_DYNLINK void DeleteMyClass (CD3DRenderDevice* pObj) 
{ 
	delete pObj;
}
#endif

#endif

////////////////////////
//CD3DRenderDevice.cpp
////////////////////////

#include "CD3DRenderDevice.h"


HINSTANCE		g_hinstThisDll;
static	BOOL			g_bCrippledOS	=	FALSE;
static	OSVERSIONINFO	g_osviVerInfo;

extern "C" int APIENTRY	DllMain	(	HINSTANCE	hInstance, 
									DWORD		dwReason, 
									LPVOID		lpReserved
								)
{
	BOOL	bRC	=	TRUE;

    if	(	DLL_PROCESS_ATTACH	==	dwReason)
		{
		    // Extension DLL one-time initialization
	        g_hinstThisDll	=	hInstance;

			g_osviVerInfo.dwOSVersionInfoSize	=	sizeof	(	OSVERSIONINFO);
			
			GetVersionEx(	&g_osviVerInfo);
			
			if (	VER_PLATFORM_WIN32_WINDOWS	==	g_osviVerInfo.dwPlatformId)
					g_bCrippledOS	=	TRUE;

		}

	return	(	TRUE);
}

CD3DRenderDevice::CD3DRenderDevice()
{

}

CD3DRenderDevice::~CD3DRenderDevice()
{

}

////////////////////////
//CD3DRenderDevice.def
////////////////////////
LIBRARY D3DDriver.dll
EXPORTS
	CreateMyClass               @2 PRIVATE          ; object creation
	DeleteMyClass               @3 PRIVATE          ; object destruction



Any help would be appreciated

Share this post


Link to post
Share on other sites
Make the functions that are called across the DLL boundary use C naming conventions by using extern C, otherwise the names will be mangled by the compiler. You may also need to specify the calling convention.
//////////////////////
//CD3DRenderDevice.h
//////////////////////

#ifndef C_D3D_RENDER_DEVICE_H_INCLUDED
#define C_D3D_RENDER_DEVICE_H_INCLUDED

#include <windows.h>

//I am defining _DLL in my project settings (However, it doesn't work if I undefine it either)

#ifdef _DLL // assume this is defined when we build the dll
#define _DYNLINK __declspec( dllexport)
#else
#define _DYNLINK __declspec( dllimport)
#endif

class CD3DRenderDevice
{
public:
CD3DRenderDevice();
~CD3DRenderDevice();
};

typedef void (CD3DRenderDevice::*PMYCLASSMETHOD)();

#ifndef _DLL
typedef CD3DRenderDevice* ( *PFNCREATEMYCLASS)();
#else
extern "C"
{

_DYNLINK CD3DRenderDevice* __stdcall CreateMyClass()
{
return ( new CD3DRenderDevice());
}
}
#endif

#ifndef _DLL
typedef void ( *PFNDELETEMYCLASS)( CD3DRenderDevice*);
#else
extern "C"
{

_DYNLINK void __stdcall DeleteMyClass (CD3DRenderDevice* pObj)
{
delete pObj;
}
}
#endif

#endif


Enigma

[Edited by - Enigma on December 1, 2004 6:21:44 AM]

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