Jump to content
  • Advertisement
Sign in to follow this  
bkt

DLL Exporting problem

This topic is 4870 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 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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 );

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!