.net - 从 IUnknown 实现接口(interface)类时出现错误 c2259

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

我正在尝试将一个 C++ 项目添加到我的 C# 代码中,并且我正在尝试实现 IUnknown 接口(interface)。我一直收到有关 c2259 的错误:无法实例化抽象类。

我尝试过一些操作,例如将类设为 ref 类并更改实现,但似乎没有任何效果。下面是我正在使用的一些代码。

我的接口(interface)类:

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);
}

感谢任何帮助。

我的类定义是这样的:

public class RotateWrapper : public IClientWrapper

最佳答案

您需要实现继承自 IUnknown 的方法:QueryInterfaceAddRefRelease。如果不这样做意味着您的类仍然具有纯虚方法,并且编译器禁止您实例化此类是正确的。

关于.net - 从 IUnknown 实现接口(interface)类时出现错误 c2259,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794829/

相关文章:

c# - 大文件传输中TcpClient VS Socket

c++ - 非命名空间范围内的显式特化在 GCC 中无法编译

C++ 多态性和默认参数

visual-studio - 如何用 Visual C++ 编写一个简单的 GUI 应用程序

C# 手动停止异步 for 语句(打字机效果)

c# - 我应该等到 VS 2010 发布后再购买 Resharper 吗?

c# - 从 JavaScript 编辑选择列表时如何通过事件验证

c++ - CMake问题:如何使用vcpkg自动安装依赖项?

c++ - "1 unresolved externals"C++

c - 是否有可以处理 C 内联结构的 MSVC 版本?