2011-07-22 112 views
0

我想添加一个C++项目到我的c#代码,我试图实现IUnknown接口。我不断收到有关c2259的错误:无法实例化抽象类。错误c2259从IUnknown实现接口类

我试着玩一些东西,比如让这个类变成一个类并改变实现,但似乎没有任何工作。以下是我使用的一些代码。

我的接口类:

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 

回答

2

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

+0

我看,我没有实现QueryInterface。这可能是问题(我希望...大声笑) –

+0

感谢您的帮助Rob! –