这段代码构造的解释

发布于 2024-11-27 01:07:04 字数 1355 浏览 1 评论 0原文

我有这样的代码(它是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月竹挽风 2024-12-04 01:07:04
typedef HRESULT (*CREATERENDERDEVICE) (HINSTANCE hDLL, ZFXRenderDevice **pInterface); 

是函数指针(指向函数的指针)的 typedef 声明。

在这个 typedef 声明之后。

CREATERENDERDEVICE _CreateRenderDevice;

声明一个函数指针,该函数返回 HRESULT 并采用两个参数 HINSTANCE & ZFXRenderDevice **

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

将函数 GetProcAddress 的地址分配给函数指针 CreateRenderDevice

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

醉生梦死 2024-12-04 01:07:04

也许您应该看看此处
这是对函数指针的简短介绍,应该可以回答您的问题。

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

薄荷港 2024-12-04 01:07:04

基本上,声明

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

只能告诉编译器该函数是什么样的。为了能够调用该函数,您的代码需要能够知道该函数所在的位置。因此,要执行此操作,您的代码将首先使用 LoadLibrary() 加载 dll,以获取 dll 的 hInstance。然后使用 GetProcAddress 获取具有特定名称的导出函数的地址,并将其分配给上面声明的函数指针类型。这个函数指针足以调用指定的函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文