0

我想将 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;
}
4

3 回答 3

2

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

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

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

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

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

一个标准的技巧是 (1),然后将对象 this 指针作为参数显式传递,从而允许您调用非静态成员。

于 2009-01-08T10:04:02.887 回答
1

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

于 2009-01-08T19:43:10.737 回答
0

正如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;
}
于 2009-01-16T12:35:06.967 回答