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. 百度地图API开发指南

    简介什么是百度地图API? 百度地图API是一套由JavaScript语言编写的应用程序接口,它能够帮助您在网站中构建功能丰富.交互性强的地图应用.百度地图API包含了构建地图基本功能的各种接口,提供 ...

  2. SSH框架面试题

    Hibernate工作原理及为什么要用? 原理: 1. 读取并解析配置文件 2. 读取并解析映射信息,创建SessionFactory 3. 打开Sesssion 4. 创建事务Transation ...

  3. C#-gdi绘图,双缓冲绘图,Paint事件的触发

    一. 画面闪烁问题与双缓冲技术 1.1 导致画面闪烁的关键原因分析: 1  绘制窗口由于大小位置状态改变进行重绘操作时 绘图窗口内容或大小每改变一次,都要调用Paint事件进行重绘操作,该操作会使画面 ...

  4. 提高CSS开发能力的技巧集

    1. 使用:not()给导航条添加间隔线 我们通常使用如下代码给导航条增加间隔线 /* add border */ .nav li { border-right: 1px solid #666; } ...

  5. html --- SVG --- javascript --- 旋转矩形

    可缩放矢量图形(英语:Scalable Vector Graphics,SVG)是基于可扩展标记语言(XML), 用于描述二维矢量图形的一种图形格式.SVG由W3C制定,是一个开放标准. 在 Inte ...

  6. Hadoop-- MapReduce简单理解

    1.Hadoop和MapReduce概念 Mapreduce是一种模式. Hadoop是一种框架. Hadoop是一个实现了mapreduce模式的开源的分布式并行编程框架. 2.Hadoop框架   ...

  7. lego blocks

    1.题目描述 https://www.hackerrank.com/challenges/lego-blocks 2.解法分析 这题乍看一下觉得应该可以用动态规划来做,但是却死活想不到最优子结构,在网 ...

  8. EasyUI DataGrid 窗口大小自适用--------------未测试

    EasyUI 新版本里添加了 fit 属性,不需要老版本的那么复杂,重新load DataGrid.但是昨天用的时间发现只有一个DataGrid的时候用fit:true 很好使,但是如果有其它元素,如 ...

  9. hdu5072-Coprime(容斥原理)

    题意:给N个互不相同的数,选择出两两互质或者两两不互质的三个数,有多少种选法. 题解:一共有C(N,3)中选择方式,减去不符合要求的,剩下的就是答案. 详见 http://blog.csdn.net/ ...

  10. JVM系列五:JVM监测&工具[整理中]

    前几篇篇文章介绍了介绍了JVM的参数设置并给出了一些生产环境的JVM参数配置参考方案.正如之前文章中提到的JVM参数的设置需要根据应用的特性来进行设置,每个参数的设置都需要对JVM进行长时间的监测,并 ...