Sign in to follow this  
vivendi

[C++] From EXE - Static Lib - DLL (3D Game Engine Programming)

Recommended Posts

I want to get a bit more familiar with static lib / dll programming. I good example i found was in the book "3D Game Engine Programming" (by Stefan Zerbst). In his code examples you'll see that he uses a static lib which communicates with the actual engine, a DLL file. What i don't get is how this works. The static lib solution only has a handful of functions, like: CreateDevice and Release. The DLL on the other hand, has alot of classes/methods, yet he only exports 2 functions from that DLL (using a .DEF file). The CreateRenderDevice and ReleaseRenderDevice. I really don't get it how he is still able to communicate with the classes/methods in that DLL file. So my question basically is, how can he have access to all the class files in the DLL while he only exports two of them in the DEF file... I don't see the conneciton.

Share this post


Link to post
Share on other sites
The CreateRenderDevice is in the DLL, and the code in the sourcefile looks like this:


HRESULT CreateRenderDevice(HINSTANCE hDLL, ZFXRenderDevice **pDevice) {
if(!*pDevice) {
*pDevice = new ZFXD3D(hDLL);
return ZFX_OK;
}
return ZFX_FAIL;
}


*edit*
I'm also uploading one sample code of the book. For those who are interested in a more detailed example.

Share this post


Link to post
Share on other sites
The render device is full of virtual functions, which are a bit like function pointers. The functions are implemented in the DLL, and exposed to the program through the interface returned by CreateRenderDevice().

Quake II does something similar, only in a C way; it returns a struct full of function pointers - that's essentially the same thing, just a function pointer to access a function in the DLL.

Share this post


Link to post
Share on other sites
Ok i see. So he's only taking the functions out of the classes, and not giving instances of the entire class right?

Like in a DLL i could have:

include "interface.h"

class
{
public:
void GetSomething ( );
};


and in the headerfile of my lib i just simply do:
//interface.h
virtual void GetSomething ( ) = 0;

Is that basically how it works?

[Edited by - Evil Steve on February 19, 2010 2:39:18 PM]

Share this post


Link to post
Share on other sites
Not quite.. do you know how virtual functions work?

CreateRenderDevice is returning (via pointer to pointer) a new ZFXD3D instance, which implements the ZFXRenderDevice interface.

The compiler can then call virtual member functions via this pointer directly. You don't need to explicitly export these member functions, only the instance creation/deletion functions.

More info, maybe helpful.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this