libusb在 windows上开启实时权限
时间: 2024-06-09 21:04:58 浏览: 9
在 Windows 上开启 libusb 实时权限需要执行以下步骤:
1. 下载 Zadig 工具,它可以帮助我们安装 WinUSB 驱动。下载链接:https://zadig.akeo.ie/
2. 插入 USB 设备,并进入设备管理器。
3. 在设备管理器中找到需要使用 libusb 的设备,右键单击它,并选择“属性”。
4. 在设备属性窗口中,点击“详细信息”选项卡,选择“硬件 ID”属性。
5. 复制硬件 ID,并使用 Zadig 工具安装 WinUSB 驱动。在 Zadig 工具中,点击“选项”菜单,启用“列表所有设备”选项,并选择“WinUSB”驱动。
6. 点击“安装驱动”按钮,等待安装完成。
7. 安装完成后,进入 libusb 库的源代码目录,找到“libusb\os\windows\io.c”文件,将其中的以下注释代码取消注释:
```
if (TransferFlags & USBD_TRANSFER_DIRECTION_IN)
{
// FIXME: we need to set the event here to avoid a race condition
// if the transfer completes before we can set it below.
//SetEvent(Event); //这一行取消注释
do
{
// wait for the transfer to complete
DWORD dwRet = WaitForSingleObject(Event, 0);
if (dwRet == WAIT_OBJECT_0)
{
// the transfer has completed
return USBD_STATUS_SUCCESS;
}
else if (dwRet == WAIT_TIMEOUT)
{
// the transfer is still pending
DWORD dwMilliseconds = GetTimeoutMilliseconds(timeout);
dwRet = WaitForSingleObject(Event, dwMilliseconds);
if (dwRet == WAIT_OBJECT_0)
{
// the transfer has completed
return USBD_STATUS_SUCCESS;
}
else if (dwRet == WAIT_TIMEOUT)
{
// the transfer timed out
return USBD_STATUS_TIMEOUT;
}
else
{
// some other error occurred
return USBD_STATUS_IO_FAILED;
}
}
else
{
// some other error occurred
return USBD_STATUS_IO_FAILED;
}
}
while (1);
}
```
8. 编译 libusb 库。
9. 在使用 libusb 的应用程序中,调用 libusb_set_option() 函数设置 LIBUSB_OPTION_USE_USBDK 选项为 false。
10. 运行应用程序即可获得实时权限。