Visual Studio:回调函数问题(__cdecl *) [英] Visual Studio: Callback function problem (__cdecl *)
问题描述
我有一个在Ubuntu上运行良好的C应用程序,但是我必须将其移植到Visual Studio,这就是问题开始的地方.我正在使用Visual Studio 2017,而我的问题是使用的回调函数.在代码中,我必须注册两个回调函数(几乎相同的代码,只是一个用于发送数据,另一个用于接收数据).
I have a C application that works good on Ubuntu, but I have to port it to Visual Studio, and thats where problems start. I'm using Visual Studio 2017, and my problem is with callback functions that I use. Within the code, I have to register two callback functions (almost the same code, it's just that one is for sending data, other is for receiving it).
libusb_fill_bulk_transfer(recv_transfer_desc, //transfer descriptor struct
usb_dev_handle, //usb device handle
EP1_IN, //unsigned char
in_buffer, //unsigned char*
len, //tranfer length, int
recv_transfer_finished_cb, //callback func
&my_recv_cb_data, //void*, callback user data
timeout); //unsigned int
这将导致以下错误: C2440'function':无法从'void(__cdecl *)(libusb_transfer *)'转换为'libusb_transfer_cb_fn'
我从不使用VS,所以我真的不知道如何解决此问题.对我来说,它缝起来就像需要将某些东西铸成某种东西,但无法弄清楚.
I never use VS, so I really don't know how to fix this issue. To me it seams like something needs to be casted into something, but just can't figure it out.
这是我要处理的回调函数的签名: static void recv_transfer_finished_cb(struct libusb_transfer *);
This is the signature of callback function I'm trying to handle:
static void recv_transfer_finished_cb(struct libusb_transfer *);
推荐答案
我已经弄清楚了,经过反复试验.
I've just figured it out, trough trial and error.
问题在于,VS中的默认调用是 __ cdecl
,并且不能用于回调函数,该回调函数必须是 __ stdcall
.
The problem was that default call in VS is __cdecl
, and that can't be used for callback functions, which must be __stdcall
.
要解决我的问题,我需要将回调函数的签名从以下位置更改:静态无效send_transfer_finished_cb(struct libusb_transfer *);
至: static void __stdcall send_transfer_finished_cb(struct libusb_transfer *);
To solve my problem, I needed to change signature of callback function from:
static void send_transfer_finished_cb(struct libusb_transfer *);
To:
static void __stdcall send_transfer_finished_cb(struct libusb_transfer *);
这篇关于Visual Studio:回调函数问题(__cdecl *)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!