libusb_hotplug_register_callback不接受我的回调函数
libusb_hotplug_register_callback does not accept my callback function
我正在开发一个应用程序来检测USB何时插入和拔出vc ++与Visual Studio 2012。我添加了libusb 1.0库,它现在是一个跨平台的库。
我在尝试注册事件处理程序时遇到使用回调函数进行编译的问题。
#include "detect_usb_libusb.h"
#include <QtWidgets/QApplication>
#include <time.h>
#include <stdio.h>
#include <libusb.h>
static int count = 0;
int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
libusb_hotplug_event event, void *user_data) {
static libusb_device_handle *handle = NULL;
struct libusb_device_descriptor desc;
int rc;
(void)libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
rc = libusb_open(dev, &handle);
if (LIBUSB_SUCCESS != rc) {
printf("Could not open USB devicen");
}
} else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
if (handle) {
libusb_close(handle);
handle = NULL;
}
} else {
printf("Unhandled event %dn", event);
}
count++;
return 0;
}
int main (void) {
libusb_hotplug_callback_handle handle;
int rc;
libusb_init(NULL);
rc = libusb_hotplug_register_callback( NULL, (libusb_hotplug_event) (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), LIBUSB_HOTPLUG_ENUMERATE,
0x2047, LIBUSB_HOTPLUG_MATCH_ANY,
LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL,
&handle);
if (LIBUSB_SUCCESS != rc) {
printf("Error creating a hotplug callbackn");
libusb_exit(NULL);
return EXIT_FAILURE;
}
while (count < 2) {
_sleep(10000);
}
libusb_hotplug_deregister_callback(NULL, handle);
libusb_exit(NULL);
return 0;
}
我从libusb API获得了这段代码,在这一点上我真的很迷茫。例外说:
error C2664: 'libusb_hotplug_register_callback' : cannot convert parameter 7 from 'int (__cdecl *)(libusb_context *,libusb_device *,libusb_hotplug_event,void *)' to 'libusb_hotplug_callback_fn'
但我不知道如何将我的"int (__cdecl *)"函数转换为"int( * libusb_hotplug_callback_fn)"
多谢
这是这样的: static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
来源: https://github.com/libusb/libusb/blob/master/examples/hotplugtest.c
我在 VS 2013 上遇到了同样的错误,在 libusb_hotplug_register_callback() 中只是更改
hotplug_callback
自
(libusb_hotplug_callback_fn) hotplug_callback
这
很可能是由于
rc = libusb_hotplug_register_callback( NULL, (libusb_hotplug_event) (**LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT**), LIBUSB_HOTPLUG_ENUMERATE,0x2047, LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL,&handle);
尝试使用
rc = libusb_hotplug_register_callback( NULL, (libusb_hotplug_event) (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED), LIBUSB_HOTPLUG_ENUMERATE,0x2047, LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL,&handle);
这是因为由于 or ( |
) 运算符,它在按位 OR 运算后转换为整数。
相关文章:
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- C++存储带有可变参数的回调
- 处理类内的回调时,必须调用对非静态成员函数的引用
- C++ 事件管理器的回调,使用 std::function 和 std:bind 以及派生类作为参数
- 使用带有闭包的 lambda 的回调
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 从其存储的回调中删除 std::函数是否安全
- 如何实现从C++函数到 Swift 的回调
- C++ Qt5 中带有 pigpio 库的回调函数
- libevent是否允许在不同的线程中运行timer/io的回调
- v8 源代码中 ArrayMap 函数的回调 fn 参数是什么?
- Poco 计时器,具有来自同一类的回调
- 为什么没有调用我的回调?
- SetWindowsHookEx没有调用我的回调
- 为什么 DNSServiceProcessResult 两次调用我的回调
- libusb_hotplug_register_callback不接受我的回调函数
- (如何)当std::future准备就绪时,我可以获得boost::asio::io_service的回调吗
- ROS:subscribe函数无法识别我的回调方法
- GStreamer似乎没有调用我的回调
- 我可以从OpenGL的显示器调用我的回调函数吗