-1

I have code like this (it's declaration of interface functions which are implemented in DLL file - it's from book - Course PTR 3D Game Engine Programming):

extern "C"
{
    HRESULT CreateRenderDevice(HINSTANCE hDLL, ZFXRenderDevice **pInterface); 
    typedef HRESULT (*CREATERENDERDEVICE) 
                        (HINSTANCE hDLL, ZFXRenderDevice **pInterface);
    HRESULT ReleaseRenderDevice(ZFXRenderDevice **pInterface); 
    typedef HRESULT(*RELEASERENDERDEVICE) 
                        (ZFXRenderDevice **pInterface);
}

and it's used liked this

CREATERENDERDEVICE _CreateRenderDevice = 0;
HRESULT hr; // pointer to DLL function ‘CreateRenderDevice’
_CreateRenderDevice = (CREATERENDERDEVICE) 
                        GetProcAddress(m_hDLL,“CreateRenderDevice”);

if ( !_CreateRenderDevice ) 
    return E_FAIL; // call DLL function to create the device

hr = _CreateRenderDevice(m_hDLL, &m_pDevice);

I understand taht it's extracting function from DLL, but can someone explain me this part of the code? What construction is it (macro?) and how dos it works?

typedef HRESULT (*CREATERENDERDEVICE)
                        (HINSTANCE hDLL, ZFXRenderDevice **pInterface);

and also

typedef HRESULT(*RELEASERENDERDEVICE)
                        (ZFXRenderDevice **pInterface);

and usage of it

_CreateRenderDevice = (CREATERENDERDEVICE)
                                  GetProcAddress(m_hDLL,“CreateRenderDevice”);
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
user867308
  • 21
  • 1
  • @Gens: What is poor in the Q? OP does not understand a piece of code and asking for help in understanding it better. Probably, it is worded poorly, but that is not enough to just brand it as poor question since not all posters are native english speakers here. – Alok Save Jul 28 '11 at 11:56

3 Answers3

3
typedef HRESULT (*CREATERENDERDEVICE) (HINSTANCE hDLL, ZFXRenderDevice **pInterface); 

is an typedef declaration for an function pointer(pointer poiting to a function).

After this typedef declaration.

CREATERENDERDEVICE _CreateRenderDevice;

Declares a function pointer to a function which returns HRESULT and takes two parameters HINSTANCE & ZFXRenderDevice **.

_CreateRenderDevice = (CREATERENDERDEVICE) GetProcAddress(m_hDLL,“CreateRenderDevice”);

Assigns the address of the function GetProcAddress to function pointer CreateRenderDevice

Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

Maybe you should take a look here.
It is a small introduction to function pointers and should answer your question.

Community
  • 1
  • 1
mkaes
  • 13,781
  • 10
  • 52
  • 72
  • While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard Jul 28 '11 at 14:33
0

Basically the declaration

typedef HRESULT (*CREATERENDERDEVICE) (HINSTANCE hDLL, ZFXRenderDevice **pInterface);

is only able to tell the compiler what the function looks like. To be able to call the function your code needs to be able to tell where this function is located. So to do that your code would first load the dll using LoadLibrary() to get the hInstance of the dll. Then you use GetProcAddress to get the address of the exported function with a certain name and this you assign to the above declared function pointer type. This function pointer then is enough to call the specified function.

Fed44
  • 194
  • 3