win7 下面 libusb 开发 研究 记录

下载:
http://libusb.sourceforge.net/api-1.0/modules.html

下载:libusb-master.zip

进入 libusb-master\msvc
双击 libusb_static_2017.vcxproj
编译
会生成 libusb-master\Win32\Debug\lib\libusb-1.0.lib 静态文件

然后打开:
libusb-master\msvc\listdevs_2017.vcxproj

编译:
会生成能用的 exe文件
在这个exe文件的基础上修改:
listdevs.c

/*
 * libusb example program to list devices on the bus
 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>

#include "libusb.h"

static void print_devs(libusb_device **devs)
{
	libusb_device *dev;
	int i = 0, j = 0;
	uint8_t path[8]; 

	while ((dev = devs[i++]) != NULL) {
		struct libusb_device_descriptor desc;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return;
		}

		printf("%04x:%04x (bus %d, device %d)",
			desc.idVendor, desc.idProduct,
			libusb_get_bus_number(dev), libusb_get_device_address(dev));

		r = libusb_get_port_numbers(dev, path, sizeof(path));
		if (r > 0) {
			printf(" path: %d", path[0]);
			for (j = 1; j < r; j++)
				printf(".%d", path[j]);
		}
		printf("\n");
	}
}



static libusb_device_handle *dev_handle = NULL;

int main(void)
{
	libusb_device **devs;
	int r;
	ssize_t cnt;
	int ret;
	r = libusb_init(NULL);
	if (r < 0)
		return r;


	dev_handle = libusb_open_device_with_vid_pid(NULL, 0x8866, 0x0100);
	if (dev_handle == NULL) {
		perror("Cannot open device\n");
	}
	else {
		printf("Device Opened\n");
	}

	ret = libusb_kernel_driver_active(dev_handle, 0);

	if (ret == 1) {
		printf("Kernel Driver Active\n");
		if (libusb_detach_kernel_driver(dev_handle, 0) == 0) {
			printf("Kernel Driver Detached!\n");
		}
		else {
			printf("libusb_detach_kernel_driver error ret = %d\n",ret);
		}
	}
	else {
		printf("libusb_kernel_driver_active error ret = %d\n", ret);
	}


	libusb_exit(NULL);
	return 0;
}

能够打开usb设备,但是执行 libusb_kernel_driver_active 会返回 -12
在这里插入图片描述
这个-12 是啥意思呢?
在这里插入图片描述
仔细看libusb_kernel_driver_active 的实现:
在这里插入图片描述

visual studio c++ 单独建立一个 测试libusb dll动态库的工程如何建立?
visual studio c++ 单独建立一个 测试libusb lib静态库的工程如何建立?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值