http://www.velleman.eu/images/tmp/usbfind.c

#ifdef __cplusplus
extern "C" {
#endif #include <windows.h>
#include <tchar.h> #include <setupapi.h>
#include <initguid.h> #include <stdio.h> // This is the GUID for the USB device class
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
// (A5DCBF10-6530-11D2-901F-00C04FB951ED) int main()
{
HDEVINFO hDevInfo;
SP_DEVICE_INTERFACE_DATA DevIntfData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DevIntfDetailData;
SP_DEVINFO_DATA DevData; DWORD dwSize, dwType, dwMemberIdx;
HKEY hKey;
BYTE lpData[]; // We will try to get device information set for all USB devices that have a
// device interface and are currently present on the system (plugged in).
hDevInfo = SetupDiGetClassDevs(
&GUID_DEVINTERFACE_USB_DEVICE, NULL, , DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); if (hDevInfo != INVALID_HANDLE_VALUE)
{
// Prepare to enumerate all device interfaces for the device information
// set that we retrieved with SetupDiGetClassDevs(..)
DevIntfData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
dwMemberIdx = ; // Next, we will keep calling this SetupDiEnumDeviceInterfaces(..) until this
// function causes GetLastError() to return ERROR_NO_MORE_ITEMS. With each
// call the dwMemberIdx value needs to be incremented to retrieve the next
// device interface information. SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE,
dwMemberIdx, &DevIntfData); while(GetLastError() != ERROR_NO_MORE_ITEMS)
{
// As a last step we will need to get some more details for each
// of device interface information we are able to retrieve. This
// device interface detail gives us the information we need to identify
// the device (VID/PID), and decide if it's useful to us. It will also
// provide a DEVINFO_DATA structure which we can use to know the serial
// port name for a virtual com port. DevData.cbSize = sizeof(DevData); // Get the required buffer size. Call SetupDiGetDeviceInterfaceDetail with
// a NULL DevIntfDetailData pointer, a DevIntfDetailDataSize
// of zero, and a valid RequiredSize variable. In response to such a call,
// this function returns the required buffer size at dwSize. SetupDiGetDeviceInterfaceDetail(
hDevInfo, &DevIntfData, NULL, , &dwSize, NULL); // Allocate memory for the DeviceInterfaceDetail struct. Don't forget to
// deallocate it later!
DevIntfDetailData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
DevIntfDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &DevIntfData,
DevIntfDetailData, dwSize, &dwSize, &DevData))
{
// Finally we can start checking if we've found a useable device,
// by inspecting the DevIntfDetailData->DevicePath variable.
// The DevicePath looks something like this:
//
// \\?\usb#vid_04d8&pid_0033#5&19f2438f&0&2#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
//
// The VID for Velleman Projects is always 10cf. The PID is variable
// for each device:
//
// -------------------
// | Device | PID |
// -------------------
// | K8090 | 8090 |
// | VMB1USB | 0b1b |
// -------------------
//
// As you can see it contains the VID/PID for the device, so we can check
// for the right VID/PID with string handling routines. if (NULL != _tcsstr((TCHAR*)DevIntfDetailData->DevicePath, _T("vid_10cf&pid_8090")))
{
// To find out the serial port for our K8090 device,
// we'll need to check the registry: hKey = SetupDiOpenDevRegKey(
hDevInfo,
&DevData,
DICS_FLAG_GLOBAL,
,
DIREG_DEV,
KEY_READ
); dwType = REG_SZ;
dwSize = sizeof(lpData);
RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, lpData, &dwSize);
RegCloseKey(hKey); // Eureka!
wprintf(_T("Found a device on port '%s'\n"), lpData);
}
} HeapFree(GetProcessHeap(), , DevIntfDetailData); // Continue looping
SetupDiEnumDeviceInterfaces(
hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE, ++dwMemberIdx, &DevIntfData);
} SetupDiDestroyDeviceInfoList(hDevInfo);
} return ;
}

USB Device Finder的更多相关文章

  1. Install Slax on USB device (Slax U 盘安装)

    Slax is a modern, portable, small and fast Linux operating system with a modular approach and outsta ...

  2. What is a Windows USB device path and how is it formatted?

    http://community.silabs.com/t5/Interface-Knowledge-Base/Windows-USB-Device-Path/ta-p/114059 Windows ...

  3. Assigning Host USB device to a Guest VM

    Example Assigning Host USB device to a Guest VM This example is based on qemu-kvm (0.15.0) as instal ...

  4. usb host和usb device

    S3C2440的数据手册将USB功能分为两章--usb host和usb device.具体什么意思呢? usb host: 微处理器作为usb主设备,可以挂接U盘之类的从属设备. usb devic ...

  5. "The connection for the USB device '###' was unsuccessful. The device is currently in use"

    一.问题描述 1.情景描述 笔者的物理主机系统是“windows7 64位”,想使用“摄像头录像大师”.这个软件在录制视频的过程中,需要调用windows自带的"windows media ...

  6. USB device & USB controller & USB passthrough

    目录 USB device USB controller Linux 相关命令 Python 相关库 Libvirt USB passthrough 参考资料 近期往 openstack 里倒腾 US ...

  7. usb device address error 110

    ubuntu失灵了,怎么都起不来,报一堆错误usb device descriptor read/64, error 110......重启,换kvm的接口,usb键盘鼠标...终于在试了下面这个方法 ...

  8. STM32F4 HAL Composite USB Device Example : CDC + MSC

    STM32F4 USB Composite CDC + MSC I'm in the process of building a USB composite CDC + MSC device on t ...

  9. How to match between physical usb device and its drive letter?

    struct tagDrives { WCHAR letter; WCHAR volume[ BUFFER_SIZE ]; } g_drives[ ]; // WCHAR GetUSBDrive( ) ...

随机推荐

  1. 嵌入式 hi3518平台指定网卡测试是否通外网

    版权声明:本文为博主原创文章,未经博主允许不得转载. /********************************** (C) COPYRIGHT *********************** ...

  2. mybatis Java API

    既然你已经知道如何配置 MyBatis 和创建映射文件,你就已经准备好来提升技能了. MyBatis 的 Java API 就是你收获你所做的努力的地方.正如你即将看到的,和 JDBC 相比, MyB ...

  3. jQuery需要掌握的技巧

    检查 jQuery 是否加载 在使用 jQuery 进行任何操作之前,你需要先确认它已经加载: if (typeof jQuery == 'undefined') { console.log('jQu ...

  4. HTML 学习进度备忘

    书签:”HTML 高级教程“及后面的内容尚未学习,另外跳过的内容有待跟进 __________________ 学习资源:W3School. 开始时间:2013.11.20 简述:此网址做为学习教程相 ...

  5. 线程局部变量ThreadLocal的原理及使用范围_1

    线程局部变量ThreadLocal的原理及使用范围 使用原理 每个Thread中都有一个ThreadLocalMap成员, 该成员是ThreadLocal的内部类ThreadLocalMap类型.每使 ...

  6. GridView導出Excel

    1.aspx頁面需要添加:EnableEventValidation="false" 實例:<%@ Page Language="C#" AutoEven ...

  7. I-frame、B-frame、P-frame及DTS、PTS的关系(转)

    基本概念: I frame :帧内编码帧 又称intra picture,I 帧通常是每个 GOP(MPEG 所使用的一种视频压缩技术)的第一个帧,经过适度地压缩,做为随机访问的参考点,可以当成图象. ...

  8. 如何创建Asp.net MVC ViewModel

    ASP.NET MVC View Model Patterns Since MVC has been released I have observed much confusion about how ...

  9. 分布式文件系统--GFS

    分布式文件系统 分布式文件系统:当数据集的大小超过一台独立物理计算机的存储能力时,就有必要对它进行分区(partition)并存储到若干台单独的计算机上.管理网络中夸多台计算机存储的文件系统.这种系统 ...

  10. 【WPF】ContentControl Style定义与使用出现问题后 -- 引发的思考

    一.背景  使用WPF的朋友,大家都很喜欢采用定义控件的公共样式,以便整个框架对该资源的使用,好处就是可以达到代码复用.系统风格统一等: 1. 定义资源       <Style TargetT ...