这段代码构造的解释
我有这样的代码(它是在 DLL 文件中实现的接口函数的声明 - 它来自书籍 - PTR 3D 游戏引擎编程课程):
extern "C"
{
HRESULT CreateRenderDevice(HINSTANCE hDLL, ZFXRenderDevice **pInterface);
typedef HRESULT (*CREATERENDERDEVICE)
(HINSTANCE hDLL, ZFXRenderDevice **pInterface);
HRESULT ReleaseRenderDevice(ZFXRenderDevice **pInterface);
typedef HRESULT(*RELEASERENDERDEVICE)
(ZFXRenderDevice **pInterface);
}
并且像这样使用
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);
我明白它是从 DLL 中提取函数,但有人可以解释一下吗代码的一部分?它是什么结构(宏观?)以及它是如何工作的?
typedef HRESULT (*CREATERENDERDEVICE)
(HINSTANCE hDLL, ZFXRenderDevice **pInterface);
以及
typedef HRESULT(*RELEASERENDERDEVICE)
(ZFXRenderDevice **pInterface);
它的用法
_CreateRenderDevice = (CREATERENDERDEVICE)
GetProcAddress(m_hDLL,“CreateRenderDevice”);
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”);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是函数指针(指向函数的指针)的 typedef 声明。
在这个 typedef 声明之后。
声明一个函数指针,该函数返回
HRESULT
并采用两个参数HINSTANCE
&ZFXRenderDevice **
。将函数
GetProcAddress
的地址分配给函数指针CreateRenderDevice
is an typedef declaration for an function pointer(pointer poiting to a function).
After this typedef declaration.
Declares a function pointer to a function which returns
HRESULT
and takes two parametersHINSTANCE
&ZFXRenderDevice **
.Assigns the address of the function
GetProcAddress
to function pointerCreateRenderDevice
也许您应该看看此处。
这是对函数指针的简短介绍,应该可以回答您的问题。
Maybe you should take a look here.
It is a small introduction to function pointers and should answer your question.
基本上,声明
只能告诉编译器该函数是什么样的。为了能够调用该函数,您的代码需要能够知道该函数所在的位置。因此,要执行此操作,您的代码将首先使用 LoadLibrary() 加载 dll,以获取 dll 的 hInstance。然后使用 GetProcAddress 获取具有特定名称的导出函数的地址,并将其分配给上面声明的函数指针类型。这个函数指针足以调用指定的函数。
Basically the declaration
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.