Archived

This topic is now archived and is closed to further replies.

wannabe H4x0r

HELP how do i export a class from a DLL????

Recommended Posts

ok, i''ve got this program that needs to explicitly link to an external graphics engine in another DLL (for flexibility of graphics engines). so i''ve got a graphics class in the DLL inherited from a standard graphics class. the main program has a pointer to the parent class. the only function exported by the DLL is one that returns a pointer to the inherited class so that the main program can use the pointer as an interface to the DLL''s functions. but when i try to actually run the function and return a pointer to the main program, i get a runtime error! as in blablabla tried to access memory at blablabla memory could not be ''written'' my code essentially goes as follows: //GFX is the parent class, DXGFX is the inherited class //exported from DLL GFX* createGFX() { return new DXGFX; } //in the parent program GFX *gfxSystem; typedef GFX* (*PFNCREATEGFX)(); PFNCREATEGFX pCreateGFX; HMODULE hGFX = LoadLibrary("DXDLL.dll"); pCreateGFX = (PFNCREATEGFX)GetProcAddress(hGFX, "createGFX"); gfxSystem=(pCreateGFX)(); //error seems to occur on this line

Share this post


Link to post
Share on other sites
Oh, I really don''t think this should help (), but you could try using Win32 allocations instead of new... It seems that the mmeory allocated by your DLL isn''t accesable (for writin) from your app...

Share this post


Link to post
Share on other sites
In MyDLL.dll, MyClass.h:


  
class __declspec(dllexport) CMyClass
{

public:

void DoSomething();

CMyClass();
~CMyClass();

};


In the parent program, MyProgram.cpp


  
#pragma comment(lib, "MyDLL.lib") //The dll''s .lib file

#include "MyClass.h" //MyClass.h from the dll project


class CMyClass;

CMyClass* XMyClass = new CMyClass();

XMyClass->DoSomething();


And its that simple. That code right there should compile btw.

Join the World Wide Revolution:

Share this post


Link to post
Share on other sites
i did some research, found this on codeguru:

http://codeguru.earthweb.com/dll/expclass.shtml

seemed to have the EXACT same approach i had except his classes were not inherited. anybody help? i''m pretty much stuck.

Share this post


Link to post
Share on other sites
thanx neuro, but i dunno if i can do somethin like that because i need explicit linking. and you can just specify dllexport for a whole class like that?? from what i hear all COM interfaces work by returning pointers to interface classes so i dont know why the current approach isnt working

Share this post


Link to post
Share on other sites
The approach I showcased above is the method I''m using for my engine, I''m not sure if it really fits your situation at all but it answers the question in the subject field

Join the World Wide Revolution:

Share this post


Link to post
Share on other sites
Basically you can do this:

In a common header file:

  
class MyInterface
{
public:
virtual void Func1() = 0;
virtual void Func2() = 0;
// ...

};


In the DLL:

  
class MyImplementation : public MyInterface
{
public:
void Func1()
{
// do something

}

void Func2()
{
// do something else

}
};

extern "C" __declspec(dllexport) MyInterface *GetMyInterface()
{
return new MyImplementation();
}


Finally, in the main executable:

  
int main( int, char ** )
{
// call LoadLibrary() to load the DLL

// call GetProcAddress( "GetMyInterface" ) and cast the return

// value to the appropriate type

MyInterface *interface = GetMyInterface();
interface->Func1();
interface->Func2();
}


And that should be it. If you use new in the DLL you have to make sure you use delete in the DLL as well (which means providing a Free() method in your class, for example which just calls delete this).

P.S. There''s probably a few syntax errors in there, but you get the idea...

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
damn because that''s EXACTLY what i had been doing! see the reason i am so perplexed is that this SHOULD be working according to myself and evidently everyone else too!!??

Share this post


Link to post
Share on other sites
what''s the address where access violation occurs?

are you doing any kind of error checking?

are you unmangling function names with extern "C" or def files?

are you using multithreaded dll version of c runtime?

Share this post


Link to post
Share on other sites
in response to above questions:

instruction at 0x77fcb8d2 ref. mem at 0x00000038 the memory could not be written

error checking, i''m not sure what you mean, i''m building in Release mode in msvc6, i have no breakpoints or anything of that sort

i am exporting my functions using a DEF file as follows:

LIBRARY DXDLL
DESCRIPTION "DirectX DLL"

EXPORTS

createGFX

multithreated c runtime, even if i was using it i wouldn''t know. sticking here with mostly default settings haven''t played around with compiler options much

Share this post


Link to post
Share on other sites
holy shit i just realized i dont think my main exec is allowed to use the delete operator on the pointer... ha, my shutdown function in the main exec tried to delete the pointer and i got rid of the delete and it worked! im a dumbass, but thanx everybody. appreciate it

Share this post


Link to post
Share on other sites