求助 使用libusb ,调用libusb_claim_interface 返回-6 如何禁止系统自动加载

空持百千偈 空持百千偈 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
操作系统: UOS 20
为了在用户权限下访问USB设备 添加了rules文件
SUBSYSTEM=="usb", ATTRS{idVendor}=="1111", MODE="0666"


示例代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "libusb.h"

libusb_context *g_stUsbCtx = 0;
struct libusb_device **pDevList = 0, *pCurDev = 0;
struct libusb_device_handle *pDevHandle = 0;
int main()
{
int iRtn = 0;
if (g_stUsbCtx == 0)
{
iRtn = libusb_init(&g_stUsbCtx);
if (iRtn < 0)
{
iRtn = -1;
return iRtn;
}
}
iRtn = libusb_get_device_list(g_stUsbCtx, &pDevList);
if (iRtn < 0)
{
iRtn = -1;
return iRtn;
}
pDevHandle = libusb_open_device_with_vid_pid(g_stUsbCtx,0x1111,0x2222);
if (pDevHandle <= 0)
{
iRtn = -1;
libusb_close(pDevHandle);
pDevHandle = 0;
return iRtn;
}


uint8_t ucInterfaceIndex = 0;
iRtn = libusb_claim_interface(pDevHandle, ucInterfaceIndex);

if ((iRtn != LIBUSB_SUCCESS) && (ucInterfaceIndex == 0))
{
iRtn = libusb_detach_kernel_driver(pDevHandle, ucInterfaceIndex);

iRtn = libusb_claim_interface(pDevHandle, ucInterfaceIndex);
}
iRtn = libusb_release_interface(pDevHandle, ucInterfaceIndex);


//close
if (pDevHandle)
{

if (ucInterfaceIndex != (uint8_t)(-1))
{
libusb_detach_kernel_driver(pDevHandle, ucInterfaceIndex);
}
libusb_close(pDevHandle);
pDevHandle = 0;
}
return 0;
}



UOS 系统会自动加载我的USB设备 ,导致 第一次 libusb_claim_interface必定失败,需要调用 libusb_detach_kernel_driver 来卸载设备.
这个时候问题就来了,每次调用libusb_detach_kernel_driver ,系统都会弹框提示 “设备已拔出” 。
有没有办法让它不提示,最好是让系统不自动加载我的设备,或者在不需要卸载的情况下发指令。

谢谢各位大佬!
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部