Original Post
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: Any help would be appreciated
////////////////
//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