实现IUnknown接口获取错误c2259:无法实例化抽象类

时间:2011-07-25 19:16:48

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

我正在尝试创建一个为我的类实现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几十个构建才意识到我已经添加了所有的实现... lol

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

2 个答案:

答案 0 :(得分:0)

HRESULT __stdcall RotateWrapper::QueryInterface(const GUID riid, void **ppvObj)

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

答案 1 :(得分:-1)

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