首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libusb_hotplug_register_callback不接受我的回调函数

libusb_hotplug_register_callback不接受我的回调函数
EN

Stack Overflow用户
提问于 2016-02-03 00:48:11
回答 3查看 2.8K关注 0票数 1

我正在用visual studio 2012开发一个应用程序来检测vc++中USB的插入和拔出情况。我已经添加了libusb 1.0库,它现在是一个跨平台库。

当我尝试注册事件处理程序时,我遇到了回调函数的编译问题。

代码语言:javascript
复制
#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 device\n");
            }
        } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
            if (handle) {
                libusb_close(handle);
                handle = NULL;
            }
        } else {
                printf("Unhandled event %d\n", 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 callback\n");
    libusb_exit(NULL);
    return EXIT_FAILURE;
  }
  while (count < 2) {
    _sleep(10000);
  }
  libusb_hotplug_deregister_callback(NULL, handle);
  libusb_exit(NULL);
  return 0;
}

我从libusb API获得了这段代码,现在我真的搞不懂了。异常是这样的:

代码语言:javascript
复制
 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)”

非常感谢

EN

回答 3

Stack Overflow用户

发布于 2016-04-01 02:02:23

是这样的: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

票数 1
EN

Stack Overflow用户

发布于 2016-02-09 05:27:08

我在VS 2013上也遇到了同样的错误,在libusb_hotplug_register_callback()中只需更改

代码语言:javascript
复制
hotplug_callback

代码语言:javascript
复制
(libusb_hotplug_callback_fn) hotplug_callback
票数 0
EN

Stack Overflow用户

发布于 2018-01-10 21:43:55

这很可能是由于

代码语言:javascript
复制
 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); 

尝试使用

代码语言:javascript
复制
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运算后转换为整数。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35159163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档