实现 IUnknown、未解析的外部符号

发布于 2024-08-04 18:39:08 字数 684 浏览 8 评论 0原文

我正在尝试创建一个实现 IUnknown 接口的类。我在头文件中有以下代码:

#pragma once

#include "stdafx.h"
#include "Unknwn.h"


class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9
{
public:
Vmr9Presenter(void);
HRESULT Initialize(void);
~Vmr9Presenter(void);
STDMETHODIMP QueryInterface(const IID &riid, void **ppvObject);
};  

我已经包含了相关的 uuid.lib 和其他几个代码。但是,当我尝试编译时,出现错误:

错误 2 错误 LNK2001:无法解析的外部符号“public: virtual 长 __stdcall Vmr9Presenter::QueryInterface(struct _GUID const &,void * *)" (?QueryInterface@Vmr9Presenter@@UAGJABU_GUID@@PAPAX@Z) Vmr9Presenter.obj VmrPresenter

这让我相信有些东西没有被拉进去。关于如何消除此错误有什么建议吗?

I am trying to create a class that implements the IUnknown interface. I have the following code in the header file:

#pragma once

#include "stdafx.h"
#include "Unknwn.h"


class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9
{
public:
Vmr9Presenter(void);
HRESULT Initialize(void);
~Vmr9Presenter(void);
STDMETHODIMP QueryInterface(const IID &riid, void **ppvObject);
};  

I have included the relevant uuid.lib and several others. However, when I attempt to compile, I get the error:

Error 2 error LNK2001: unresolved external symbol "public: virtual
long __stdcall Vmr9Presenter::QueryInterface(struct _GUID const &,void
* *)" (?QueryInterface@Vmr9Presenter@@UAGJABU_GUID@@PAPAX@Z) Vmr9Presenter.obj VmrPresenter

This leads me to believe that something isn't getting pulled in. Any suggestions on how to get rid of this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夜无邪 2024-08-11 18:39:08

所有 I* 接口都只是接口定义。接口是 C++ 术语中的纯虚拟基类。

当您说:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9

您是在说“Vmr9Presenter 类实现了这些接口”。您还说“Vmr9Presenter 类派生自两个名为 IVMRImagePresenter9 和 IVMRSurfaceAllocator9 的纯虚拟基类。按照惯例,所有接口均派生自名为 IUnknown 的纯虚拟基类。

这意味着您需要实现纯虚拟基类中的所有方法因此,您需要实现 IVMRImagePresenter9 和 IVMRSurfaceAllocator9 上的所有方法,您还需要实现其基类上的所有方法,包括 IUnknown

有 3 个方法:AddRef、Release 和 IUnknown。您报告的错误表明链接器无法找到名为 Vmr9Presenter::QueryInterface 的函数。

您需要将这样的函数添加到您的类中,通常

QI 实现应该如下所示:

HRESULT IVmr9Presenter::QueryInterface(REFIID iid, PVOID *pvInterface)
{
    if (pvInterface == NULL)
    {
        return E_POINTER;
    }
    *pvInterface = NULL;
    if (iid == IID_IUnknown)
    {
         *pvInterface = static_cast<PVOID>(static_cast<IUnknown *>(this));
         return S_OK;
    }
    if (iid == IID_IVMRSurfaceAllocator9)
    {
         *pvInterface = static_cast<PVOID>(static_cast<IVMRSurfaceAllocator9*>(this));
         return S_OK;
    }
         :
    else
    {
        return E_NOINTERFACE;
    }
}

All the I* interfaces are just that - interface definitions. An interface is a pure virtual base class in C++ terms.

When you say:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9

you're saying "the Vmr9Presenter class implements these interfaces". You're also saying "The Vmr9Presenter class derives from two pure virtual base classes named IVMRImagePresenter9 and IVMRSurfaceAllocator9. By convention all interfaces derive from a pure virtual base class called IUnknown.

This means that you need to implement all the methods in the pure virtual base classes in your object. So you need to implement all the methods on IVMRImagePresenter9 and IVMRSurfaceAllocator9. You ALSO need to implement all the methods on their base classes, including IUnknown.

IUnknown has 3 methods: AddRef, Release and QueryInterface. The error you're reporting says that the linker was unable to find a function named Vmr9Presenter::QueryInterface.

You need to add such a function to your class, once you do that it should work.

Typically a QI implementation looks like:

HRESULT IVmr9Presenter::QueryInterface(REFIID iid, PVOID *pvInterface)
{
    if (pvInterface == NULL)
    {
        return E_POINTER;
    }
    *pvInterface = NULL;
    if (iid == IID_IUnknown)
    {
         *pvInterface = static_cast<PVOID>(static_cast<IUnknown *>(this));
         return S_OK;
    }
    if (iid == IID_IVMRSurfaceAllocator9)
    {
         *pvInterface = static_cast<PVOID>(static_cast<IVMRSurfaceAllocator9*>(this));
         return S_OK;
    }
         :
    else
    {
        return E_NOINTERFACE;
    }
}
花之痕靓丽 2024-08-11 18:39:08

IVMRImagePresenter9、IVMRurfaceAllocator9 是否已实现 IUnknown?也许您需要:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9, IUnknown

我猜您可能还需要根据 IUnknown 的文档

Do either of IVMRImagePresenter9, IVMRSurfaceAllocator9 already implement IUnknown? Maybe you need:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9, IUnknown

I would guess you may also need to implement AddRef() and Release() too according to the docs for IUnknown.

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