Jump to content
  • Advertisement
Sign in to follow this  
Squirell

Dynamic Class Help

This topic is 5008 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!