c++ - libusb_hotplug_register_callback 不接受我的回调函数

标签 c++ visual-studio-2012 compilation libusb-1.0

我正在使用 visual studio 2012 开发一个应用程序来检测何时在 vc++ 中插入和拔出 USB。我添加了 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 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 获得了这段代码,此时我真的迷路了。异常说:

 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

关于c++ - libusb_hotplug_register_callback 不接受我的回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35159163/

相关文章:

c++ - Visual C++ 中的二进制再现性

c++ - 在 netbeans 7.1.1 中 boost 库

Cocoa 中的 C++ 类

c++ - 如何比较两个 Qstring?

c++ - 错误 : cannot convert 'int (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'int (*)[100]' for argument '1' to 'int determ(int (*)[100], int)' |

c# - Visual Studio 2012 的增强型滚动条?

C++:指针与指针的指针在二叉树中插入节点

c++ - 如何使用带代码块的 G++ 为 64 位编译?

C++ 显示有组织的单词列表

c# - 无法加载文件或程序集 Microsoft.Office.SharePoint.Tools