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

Started by
4 comments, last by mattd 14 years, 2 months ago
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.
Advertisement
Take a look at the return type of CreateRenderDevice. Is it, mayhap, a class with a whole bunch of virtual functions?
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.
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.
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]
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.

This topic is closed to new replies.

Advertisement