2009-01-08 16 views
0

我想传递一个COM方法作为函数的参数,但我得到这个错误(微软(R)32位C/C++优化编译器版本为15.00.30729.01的80x86):如何将COM方法作为函数参数传递?而微软编译器错误C3867

错误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; 
} 

回答

0

由于morechilli指出,这是一个C++的问题。 这是解决方案,这要归功于我的同事丹尼尔:

#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; 
} 
2

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

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

你有一个成员函数 - (它是从一个函数不同)。

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

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

标准的伎俩将是(1),然后传递对象this指针明确 作为参数让你再调用非静态成员。

+0

谢谢,我会研究http://www.parashift.com/c++-faq-lite/pointers-to-members.html – 2009-01-08 10:15:11

1

来自Boost.Function也是一个合理的选择在这里(请注意,我没有考什么我下面写的,所以它可能需要一些修改 - 尤其是,我不知道,如果你要调用某种让您但是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提到的指针到成员的东西,但它隐藏了一些使用它的混乱语法。

+0

我在``update''调用中出现这个错误。任何想法?谢谢。 1> C:\ devel的\库\升压\包括\升压1_35 \升压\功能\ function_template.hpp(137):错误C2440: '回归':不能从转换 '长(__stdcall&)' 到 'HRESULT' 1>没有可能进行此转换的上下文 – 2009-01-16 10:30:29

相关问题