我正在尝试将一个 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
的方法:QueryInterface
、AddRef
和 Release
。如果不这样做意味着您的类仍然具有纯虚方法,并且编译器禁止您实例化此类是正确的。
关于.net - 从 IUnknown 实现接口(interface)类时出现错误 c2259,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794829/