2

我正在尝试创建一个实现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 long __stdcall Vmr9Presenter::QueryInterface(struct _GUID const &,void * *)" (?QueryInterface@Vmr9Presenter@@UAGJABU_GUID@@PAPAX@Z) Vmr9Presenter.obj VmrPresenter

这让我相信有些东西没有被拉进来。关于如何摆脱这个错误的任何建议?

4

2 回答 2

4

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

当你说:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9

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

这意味着您需要在对象的纯虚拟基类中实现所有方法。所以你需要实现IVMRImagePresenter9和IVMRSurfaceAllocator9上的所有方法。您还需要在基类上实现所有方法,包括 IUnknown。

IUnknown 有 3 个方法:AddRef、Release 和 QueryInterface。您报告的错误表明链接器无法找到名为 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;
    }
}
于 2009-09-12T23:44:48.750 回答
0

IVMRImagePresenter9、IVMRSurfaceAllocator9 中的任何一个是否已经实现了 IUnknown?也许你需要:

class Vmr9Presenter : IVMRImagePresenter9, IVMRSurfaceAllocator9, IUnknown

我猜您可能还需要根据IUnknown的文档实现 AddRef() 和 Release()。

于 2009-09-12T20:38:29.860 回答