DLL Exporting problem

Started by
8 comments, last by iMalc 18 years, 8 months ago
I'm attempting to export a pointer to a new object from a DLL (Factory pattern), and I'm having a bit of a problem. Looking through the debugger it shows that the pointer is indeed not NULL, but the virtual table does not seem to appear (the debugger says: __vfptr CXX0030: Error: expression cannot be evaluated) and crashes when one of the virtual methods are called. Here's the calling code:

bool VideoTask::start(void)
{
	// we need to load the video library from the DLL
	if(!libraryManager()->load( &m_pDevice, "glDevice.dll" ))
		return false;

	m_pDevice->Test();
	return true;
}


And the library loading function:

		template<class Tp> bool load(Tp** pFactory,string szFilepath)
		{
			// we need a path to work with anything right?
			if(szFilepath.empty())
				return false;

			// load the library and return the pointer (through OS specific functions)
			typedef Tp* (*libFunc_t)();
			void* hLib = Platform::LoadLibrary( szFilepath.c_str() );

			if(!hLib)
			{
				Platform::MessageBox("Error","Failed loading DLL; please check your installation.");
				return false;
			}
			
			libFunc_t pfnExport = reinterpret_cast<libFunc_t> (Platform::GetProcAddress(hLib, "ExportAPI"));
			if(!pfnExport)
			{
				Platform::MessageBox("Error","Failed getting function address; please check your installation.");
				Platform::FreeLibrary(hLib);
				return false;
			}
			
			(*pFactory) = pfnExport();
			m_List.push_back( hLib );
			return true;
		}


The Platform:: calls are wrappers to the Win32 API, I've attempted the code with and without the wrapper calls and the same results happen. So I've decided to leave the wrapper calls in. The code dies when m_pDevice->Test() is called. Thanks for the help ahead of time!
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Advertisement
Looks okay to me. I've just been writing virtually the same thing a couple of days ago. In fact, today I exported some functions within the exe, and produced a lib file, for them to be imported back into the dll! (I didn't even realise that was possible until yesterday)
Try posting the definition of the function in the DLL, or anything else that may help. Or maybe you can make a bare bones (yet complete) example to show the problem? Of course you may even solve it just by doing that if you're lucky.[smile]
Make sure your calling conventions match btw.

Also be careful when destroying these objects as they're allocated from within the DLL, so you probably need to deallocate them from the DLL. I do this by making the destructor 'protected', and providing a virtual 'release' function.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
I can't believe I forgot to post that, heh, here's the exporting function from the DLL I'm attempting to load up.
#define GLAPI_EXPORT extern "C" __declspec(dllexport)GLAPI_EXPORT IVideoDevice* ExportAPI(void){	return new GLDevice();	//pDevice = new GLDevice();}
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
What's up with this line

(*pFactory) = pfnExport();

??

why the () at the end, arent' you just copying pointers??

Cheers
Chris

PS Set m_pFactory to NULL before you pass it and chekc its non null when you get it back.

Cheers
Chris
CheersChris
Because pfnExport is a pointer to the function inside the DLL. I need to call the function in order to return a pointer of IVideoDevice.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Ahh so your not returning a ponter to a function just a pointer to a class, gotcha!

Can you step into that call then and verify its being called properly. And make sure both your projects are linking with the same std runtime library.

Cheers
Chris
CheersChris
Quote:Original post by bkt
Because pfnExport is a pointer to the function inside the DLL. I need to call the function in order to return a pointer of IVideoDevice.
Yeah I nearly made the same mistake myself, but then guessed what you were doing.
Well I'm stumped. Everything looks okay.
Is it just the vritual table that has problems, or do data members that are initialised in the constructor not appear to have the correct values either?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Well I haven't checked with variables, but I need the virtual tables to work; there's no question there. I'm going to test a few more things in the debugger, but its not even working after I first export it either.

update: it works right after being exported, but it does not work from the calling function. Therefore my wrapper functions are working properly, and I don't have to change them. It has to be something with the pointer itself. It shouldn't have anything to do with hLib falling out of scope right? That's a pointer.
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Well a friend of mine figured it out. It seems the data in hLib was falling out of scope (not the pointer), so here was the one line fix:
m_List.push_back( &hLib );
-John "bKT" Bellone [homepage] [[email=j.bellone@flipsidesoftware.com]email[/email]]
Well done! And thanks for posting the solution so that others may learn from it.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement