【问题标题】:How to pass a COM method as a function argument? And Microsoft Compiler error C3867如何将 COM 方法作为函数参数传递?和 Microsoft 编译器错误 C3867
【发布时间】:2009-01-08 09:48:32
【问题描述】:

我想将 COM 方法作为函数参数传递,但出现此错误(Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86):

错误 C3867:“IDispatch::GetTypeInfoCount”:函数调用缺少参数列表;使用 '&IDispatch::GetTypeInfoCount' 创建指向成员的指针

我错过了什么?

非常感谢。

#include <atlbase.h>

void update( HRESULT(*com_uint_getter)(UINT*), UINT& u )
{
   UINT tmp;
   if ( S_OK == com_uint_getter( &tmp ) ) {
      u = tmp;
   }
}

// only for compile purpose, it will not work at runtime
int main(int, char*[])
{
   // I choose IDispatch::GetTypeInfoCount just for the sake of exemplification
   CComPtr< IDispatch > ptr;
   UINT u;
   update( ptr->GetTypeInfoCount, u );
   return 0;
}

【问题讨论】:

    标签: c++ com function-pointers


    【解决方案1】:

    看起来像一个直接的 c++ 问题。

    您的方法需要一个指向函数的指针。

    你有一个成员函数 - (与函数不同)。

    通常您需要:
    1.将要传递的函数更改为静态。
    2. 将预期的指针类型更改为成员函数指针。

    处理成员函数指针的语法不是最好的...

    标准技巧是 (1) 然后显式传递对象 this 指针 作为参数,您可以随后调用非静态成员。

    【讨论】:

    【解决方案2】:

    Boost.Function 在这里也是一个合理的选择(请注意,我没有测试我在下面写的内容,因此可能需要进行一些修改 - 特别是,我不确定您是否必须调用某种 get () 方法在您的 CComPtr 对象上):

    #include <atlbase.h>
    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    
    void update( boost::function<HRESULT (UINT*)> com_uint_getter, UINT& u )
    {
       UINT tmp;
       if ( S_OK == com_uint_getter( &tmp ) ) {
          u = tmp;
       }
    }
    
    // only for compile purpose, it will not work at runtime
    int main(int, char*[])
    {
       // I choose IDispatch::GetTypeInfoCount just for the sake of exemplification
       CComPtr< IDispatch > ptr;
       UINT u;
       update( boost::bind(&IDispatch::GetTypeInfoCount, ptr), u );
       return 0;
    }
    

    这与 morechilli 提到的所有指向成员的东西相同,但它隐藏了使用它的一些更混乱的语法。

    【讨论】:

    • 我在“更新”调用中收到此错误。任何的想法?谢谢你。 1>c:\devel\libs\boost\include\boost-1_35\boost\function\function_template.hpp(137) : 错误 C2440: 'return' : 无法从 'long (__stdcall &)' 转换为 'HRESULT' 1 > 没有可以进行这种转换的上下文
    【解决方案3】:

    正如morechilli 指出的,这是一个 C++ 问题。 感谢我的同事 Daniele,这就是解决方案:

    #include <atlbase.h>
    
    template < typename interface_t >
    void update( interface_t* p, HRESULT (__stdcall interface_t::*com_uint_getter)(UINT*), UINT& u )
    {
       UINT tmp;
       if ( S_OK == (p->*com_uint_getter)( &tmp ) ) {
          u = tmp;
       }
    }
    
    // only for compile purpose, it will not work at runtime
    int main(int, char*[])
    {
       // I choose IDispatch::GetTypeInfoCount just for the sake of exemplification
       CComPtr< IDispatch > ptr;
       UINT u;
       update( ptr.p, &IDispatch::GetTypeInfoCount, u );
       return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 2011-10-14
      • 2021-11-09
      相关资源
      最近更新 更多