Archived

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

allsorts46

DLLs

Recommended Posts

Hi, I have a question about DLLs... I am using MS Visual C++ 6. I want to create a setup used in many games, where my main executable can load a specified renderer in a DLL file. For example I would have OpenGL.dll and Direct3D.dll, each containing a certain set of exported functions which are the interface to the renderer, which would be the same regardless of the library in use, so that after loading a library, the application can call its functions in the library without having to know what happens afterwards. I can create the DLLs ok, and I have a class in each of them called IIR_Renderer which contains everything I need. I can make the DLL export this class by copying example code I have (you might be able to tell I''m very new to this sort of stuff), but how can I get my main executable to load a library on request, and then how do I access the class from the one I have loaded? All I have to go on is something I remember seeing, which is LoadLibrary. I played around with that for a while, but not managing to make it do anything useful I gave up. Thanks for any help you can offer, Ashley Warning in life.cpp (8): const Happiness is declared but never used.

Share this post


Link to post
Share on other sites
LoadLibrary is how you load DLL modules on-demand. Pass it a filename, and it returns an HINSTANCE to the DLL. You can then use GetProcAddress to retrieve function pointers to the functions you exported in your DLL. The only place where you might run into trouble is remember to extern "C" {...} your function exports. This tells the compiler not to mangle the name. I would rather search for a function by "MyFunction" rather than "@@MyFunction@@3Fv@@$dfdgf" or whatever it would look like

The same goes for exporting from a class. They're still functions. I'm not sure if there are any special rules, however, I've never exported functions from a class. Can you even export member functions? Yeesh, I'm out of touch

[edited by - Zipster on December 25, 2002 5:58:44 PM]

Share this post


Link to post
Share on other sites
It''s easier to export a factory function under export "C" that returns a pointer to a new''ed class. Then have a destroy() function that deletes the class (since it was new''ed in the DLL''s memory space).
That''s what I''ve been doing.

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites
Zipster: I eventually managed to do what you were saying, but still I can only export functions, and it''s certainly not a simple way. What I want to do is not export a member function of a class, but the class in it''s entirity so that I can use the class in my executable just as if it were decalred there rather than in the DLL. After posting I have spent another 10 or so hours working on it and searching the internet, but I cant seem to find any way of doing it.

I did in the end manage to get a class exported from a DLL and used in my program, and I thought I had done it - then I realised my program was now loading the DLL implicitly at startup, rather than it being loaded explicitly by me in the program with LoadLibrary, so it was no good after all.

risingdragon3: Um, sorry but I have not a clue what you mean

Thanks for your replies anyway. Hopefully now the thread will go back up to the top anyway it might attract some attention.

Warning in life.cpp (8): const Happiness is declared but never used.

Share this post


Link to post
Share on other sites
One way to do it is using interfaces. What you do is define an interface like this(say it's for your graphics render)


    
struct iRenderer
{
virtual void Draw( void ) = 0;
virtual void Setup( void ) = 0;
etc...
};


you put that in a file called say iRenderer.h

You then actaully implement it like this


class CRenderer : public iRenderer
{
private:
members vars
public:
virtual void Draw( void );
virtual void Setup( void );
etc...
};


Now you create whats called a class factory. it's just a simple c function

iRenderer* create_CRenderer( void )
{
return new (iRenderer*)CRenderer;
}

now in your main program create a function that takes a class name so in the example CRenderer puts create_ on the front of it then attempts to load it from your dll (opengl.dll or dx3d.dll) and then return the interface. You then use the interface to access the renderer. You don't directely return the new class cause your two renderers will use different classes and this interface allows you to access both in exactly the same way

Hope that helps

[edited by - Monder on December 26, 2002 5:35:16 PM]

Share this post


Link to post
Share on other sites
quote:
What I want to do is not export a member function of a class, but the class in it''s entirity so that I can use the class in my executable just as if it were decalred there rather than in the DLL.

In that case, you don''t want to export an entire class. Instead, simply put the class declaration into a custom header file and include it where you need it. Then you can get the funtion pointers using an interface. Monder has the gist of it.

Share this post


Link to post
Share on other sites
I do it the way Monder does it. It helps to also have a function to return the name of the renderer. That way you can find all .dlls in the directory, and try to get the GetName() function from GetProcAddress() If it succeeds, you can add the text returned to a listbox, and then display that listbox to the user.

E.g.:

  
const char* GetRenderName()
{
return "Microsoft DirectX 8.0 D3D Render Device";
}

or

const char* GetRenderName()
{
return "Standard OpenGL Render Device";
}


2p

Share this post


Link to post
Share on other sites