【问题标题】:Implementing IUnknown interface getting error c2259: cannot instantiate abstract class实现 IUnknown 接口出现错误 c2259:无法实例化抽象类
【发布时间】:2011-07-25 19:16:47
【问题描述】:

我正在尝试创建一个实现 IUnknown 接口以供我的类使用的接口,但我不断收到上述错误:c2259:无法实例化抽象类。我尝试从 IUnknown 实现所有 3 种方法,但我仍然收到错误。

您可以在下面看到我正在使用的代码。这个链接是我的主要resource

我的接口类:

interface __declspec(uuid("c78b266d-b2c0-4e9d-863b-e3f74a721d47"))
IClientWrapper : public IUnknown
{
    public:
        virtual STDMETHODIMP get_CurrentIsReadOnly(bool *pIsReadOnly) = 0;
        virtual STDMETHODIMP get_CachedIsReadOnly(bool *pIsReadOnly) = 0;
};

我的处理程序类:

#include "RotateHandler.h"

RotateHandler::RotateHandler()
{
}

RotateHandler::~RotateHandler()
{
}

STDMETHODIMP RotateHandler::CreateClientWrapper(IUIAutomationPatternInstance *pPatternInstance, IUnknown **pClientWrapper)
{
    *pClientWrapper = new RotateWrapper(pPatternInstance);  //here is error c2259
    if (*pClientWrapper == NULL)
        return E_INVALIDARG;
    return S_OK;
}

STDMETHODIMP RotateHandler::Dispatch(IUnknown *pTarget, UINT index, const struct UIAutomationParameter *pParams, UINT cParams)
{
    switch(index)
    {
        case Rotation_GetIsReadOnly:
            return ((ICustomProvider*)pTarget)->get_IsReadOnly((bool*)pParams[0].pData);
    }
    return E_INVALIDARG;
}

还有我的包装类:

#include "RotateWrapper.h"

RotateWrapper::RotateWrapper()
{
}

RotateWrapper::RotateWrapper(IUIAutomationPatternInstance *pInstance)
    : _pInstance(pInstance)
{
    _pInstance->AddRef();
}

RotateWrapper::~RotateWrapper()
{
    _pInstance->Release();
}

STDMETHODIMP RotateWrapper::get_CurrentIsReadOnly(bool *pIsReadOnly)
{
    return _pInstance->GetProperty(0, false, UIAutomationType_Bool, pIsReadOnly);
}

STDMETHODIMP RotateWrapper::get_CachedIsReadOnly(bool *pIsReadOnly)
{
    return _pInstance->GetProperty(0, true, UIAutomationType_Bool, pIsReadOnly);
}

HRESULT __stdcall RotateWrapper::QueryInterface(const GUID riid, void **ppvObj)//riid == IID_IUIAutomationRegistrar, ppvObj == interface pointer to registrar
{
    HRESULT res;
    return res;
}

ULONG __stdcall RotateWrapper::AddRef()
{
    InterlockedIncrement(&refCount);
    return refCount;
}

ULONG __stdcall RotateWrapper::Release()
{
    ULONG ulRefCount = InterlockedDecrement(&refCount);
    if (ulRefCount == 0)
    {
        delete this;
    }
    return ulRefCount;
}

我的类定义是这样的:

public class RotateWrapper : public IClientWrapper

我被困在这里太久了,需要继续这个项目。任何帮助表示赞赏。

编辑 1

我的 Wrapper.h 头文件

public class RotateWrapper : public IClientWrapper
{
    public:
        RotateWrapper();
        RotateWrapper(IUIAutomationPatternInstance *pInstance);
        ~RotateWrapper();

        //IUnknown Interface
        STDMETHODIMP get_CurrentIsReadOnly(bool *pIsReadOnly);
        STDMETHODIMP get_CachedIsReadOnly(bool *pIsReadOnly);
        HRESULT __stdcall QueryInterface(REFIID riid, void **ppvObj);
        ULONG __stdcall AddRef();
        ULONG __stdcall Release();
};

好吧,我的坏人......我猜 Visual Studios 2010 很糟糕,因为仍然有很多错误。它花了大约 2 几十个构建才意识到我已经添加了所有的实现......哈哈

感谢大家的帮助!!非常感谢。

【问题讨论】:

  • 请帮忙!我只需要一个关于如何更正确地实现 IUnknown 的简短示例......
  • 也许 DECLARE_IUNKNOWN 会让生活更轻松一些? (搜索那个)

标签: .net c++ visual-c++ com


【解决方案1】:
HRESULT __stdcall RotateWrapper::QueryInterface(const GUID riid, void **ppvObj)

这不是QueryInterface 的正确签名。提示:REFIIDconst GUID 不同。

【讨论】:

  • 可能不一样,但我认为它是 4 个重载之一。无论哪种方式我尝试我都会遇到同样的问题...... :(
  • @Chef,再看一下QueryInterface的定义。 QueryInterface 没有 4 个重载,只有一个,而且你写的不是它。为什么不像标题指定的那样用REFIID 编写你的实现?
  • 好吧,当您这么说时,我刚刚更改了它,但我仍然遇到同样的错误。任何其他想法也许......
  • @Chef 你的 RotateWrapper.h 中有什么?对于您的目的而言,这实际上比 RotateWrapper.cpp 中的内容更重要,因为 C++ 编译器仅在引发错误时才看到标头。
  • 抱歉,和老板开了个短会。我在 EDIT 1 下添加了我的头文件。感谢您的帮助。
【解决方案2】:

好吧,我的坏人……我猜 Visual Studios 2010 很糟糕,因为仍然有很多错误。它花了大约 2 几十个构建才意识到我已经添加了所有的实现......哈哈

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多