代码之家  ›  专栏  ›  技术社区  ›  Alessandro Jacopson

如何将COM方法作为函数参数传递?和Microsoft编译器错误C3867

  •  0
  • Alessandro Jacopson  · 技术社区  · 17 年前

    我想将COM方法作为函数参数传递,但出现了以下错误(适用于80x86的Microsoft(R)32位C/C++优化编译器版本15.00.30729.01):

    错误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;
    }
    
    3 回复  |  直到 17 年前
        1
  •  2
  •   morechilli    17 年前

    看起来像是一个直的C++问题。

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

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

    通常,您需要:
    1.将要传递的函数更改为静态。

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

        2
  •  1
  •   Joel    17 年前

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

    #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提到的所有指向成员的指针相同,但它隐藏了使用它时更混乱的语法。

        3
  •  0
  •   Community Mohan Dere    8 年前

    morechilli 这是解决方案,感谢我的同事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;
    }
    
    推荐文章