【问题标题】:COM - How to create a method that returns a pointer to an interface?COM - 如何创建一个返回指向接口的指针的方法?
【发布时间】:2010-01-26 13:16:46
【问题描述】:

如何在 COM 中创建一个返回接口指针的方法,这需要在 IDL 文件中完成。

编辑:

我如何在一个类中实现它:

STDMETHODIMP CBlah::get_Something(IOtherBlah** retval){
return m_protectedvar->QueryInterface(retval);
}
STDMETHODIMP CBlah::put_Somthing(IOtherBlah* rhs){
m_protectedvar = rhs;
return S_OK;
}

上述方法不起作用。我收到以下错误:

cannot instantiate abstract class with[ Base=Blah ] due to following members:
'HRESULT IBlah::putref_Something(IOtherBlah*)' : is abstract

【问题讨论】:

    标签: com interface methods


    【解决方案1】:

    类似这样的:

     interface IYourInterface {
         HRESULT GetPointer( [out, retval]IInterface** );
     };
    

    调用者会这样称呼它:

     IInterface* pointer = 0;
     HRESULT hr = yourInterfacePointer->GetPointer( &pointer );
    

    【讨论】:

    • 尝试做一个get/set方法,一个用来设置对象,一个用来获取指向对象/接口的指针
    • 没问题,在另一个答案中使用用户 nobugz 的 sn-p。
    【解决方案2】:
    [ attributes here...]
    interface IBlah : IDispatch {
      [id(1), propget]    HRESULT Something([out,retval] IOtherBlah** retval);
      [id(1), propputref] HRESULT Something([in] IOtherBlah* rhs);
    };
    

    【讨论】:

    • @Phill - 为什么取消标记答案?你还需要什么?
    • 你必须写 putref_Something(),而不是 put_Somthing()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多