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. MBTiles地图瓦片管理工具

    采用C#+GMap.NET+MBTiles数据库来实现. 具有以下功能: 1)  支持单个文件追加到mbtiles数据库. 2)  支持批量导入Google切片组织的地图瓦片. 3)  支持直接对MB ...

  2. 对 HTTP 304 的理解

    最近和同事一起看Web的Cache问题,又进一步理解了 HTTP 中的 304 又有了一些了解. 304 的标准解释是: Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提 ...

  3. 怎样预防Ddos攻击

    一.为何要DDOS? 随着Internet互联网络带宽的增加和多种DDOS黑客工具的不断发布,DDOS拒绝服务攻击的实施越来越容易,DDOS攻击事件正在成上升趋势.出于商业竞争.打击报复和网络敲诈等多 ...

  4. bjfu1253 最大上升子序列和

    n^2的算法就行,很简单的动态规划.直接上代码 /* * Author : ben */ #include <cstdio> #include <cstdlib> #inclu ...

  5. linux实现自动远程备份(scp+ssh)

    刚上线的服务器需要备份日志,要备份到另一台服务器上去,为了减少工作量,采用linux的定时任务去自动执行.因服务器都是linux的,因此采用linux的远程复制scp命令.但这里涉及到一个问题,就是s ...

  6. selenium python (一) 开发环境搭建

    1.工具下载: python工具共包括三个:python.setuptools.pip ²  python:http://python.org/getit/     python开发环境: ²  se ...

  7. [转]Linux文件权限详解

    转自:http://blog.chinaunix.net/uid-25052030-id-174343.html 在linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问 ...

  8. 游戏BI,起步了。

    思索许久,终于决定自己的发展将会是游戏的BI. 即说即做,本文是我未来BI工作的开端. 传统的游戏BI,只是将运营的工作数据化,流量的变现指标化.和网站类似,无外乎用户导入,流失,保有,付费,回访等等 ...

  9. UDP模式与TCP模式的区别

    1.TCP有连接状态,而UDP没有. 2.TCP应用层使用无需考虑包的大小及发送情况,而UDP需要. 3.TCP中IP包大小的决定者是Socket,而UDP为应用层.

  10. Spark RDD概念学习系列之RDD的checkpoint(九)

     RDD的检查点 首先,要清楚.为什么spark要引入检查点机制?引入RDD的检查点?  答:如果缓存丢失了,则需要重新计算.如果计算特别复杂或者计算耗时特别多,那么缓存丢失对于整个Job的影响是不容 ...