如何指向一个成员函数传递给C函数? [英] How to pass a pointer to a member function to a C function?

查看:138
本文介绍了如何指向一个成员函数传递给C函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1000663/using-a-c-class-member-function-as-a-c-callback-function\">Using一个C ++类的成员函数作为一个C回调函数

我正在写在使用C库(WinPcap的)一个面向对象的库。我需要通过当网络数据包到达一个函数指针调用的回调函数。我想一个成员函数指针传递给WinPcap的,让我的设计面向对象并允许不同的对象接收不同的数据包。但是成员函数,据我了解有一个不同的调用约定,因而不能传递到C函数。有没有办法解决这个问题。我用的boost ::绑定的实验(这我几乎设法用其他比试验和错误)不能取得丰硕成果。

I'm writing an object-oriented library using a C library (winpcap). I need to pass the callback function that is called when a network packet arrives as a function pointer. I would like to pass a member function pointer to winpcap, to keep my design object oriented and to allow for different objects to receive different packets. However member functions as far as I understand have a different calling convention, and thus cannot be passed to a C function. Is there a way to fix this. My experiments with boost::bind (which I hardly manage to use other than trial and error) are not fruitful.

有没有办法改变的成员函数的调用约定?

Is there a way to change the calling convention of a member function?

这是我现在使用的回调函数,它实际传递到WinPcap的定义

This is the definition of the callback function I use now and the actual passing of it to winpcap

void pcapCallback( byte* param, const struct pcap_pkthdr* header, const byte* pkt_data );

pcap_loop( adhandle, 0, pcapCallback, NULL );

该pcap_loop只是需要的函数的名称(这是在全球范围内的时刻)。这是函数指针参数的定义(第3个参数<一个href=\"http://www.winpcap.org/docs/docs_40_2/html/group__wpcapfunc.html#g6bcb7c5c59d76ec16b8a699da136b5de\"相对=nofollow> pcap_loop )。由于这是第三方code我真的不能改变这一点。我必须有一个可以采取这种形式的成员函数:

The pcap_loop just takes the name of the function (which is on the global scope at the moment). This is the definition of the function pointer parameter (3rd parameter of pcap_loop). Since this is third party code I can't really change this. I would have to have a member function that can take this form:

typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *, const u_char *);

据我了解,成员函数将使用thiscall和C函数指针想要一个CDECL

As far as I understand it, the member function will be using thiscall and the c function pointer wants a cdecl

推荐答案

请参考详细的主题关于

如何实现回调到一个静态的C ++成员函数?

如何实现回调到非静态的C ++成员函数?

http://www.newty.de/fpt/callback.html

这篇关于如何指向一个成员函数传递给C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆