USB Device Finder

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[1024];

    // 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, 0, 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 = 0;

        // 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, 0, &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,
                                0,
                                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(), 0, DevIntfDetailData);

            // Continue looping
            SetupDiEnumDeviceInterfaces(
                hDevInfo, NULL, &GUID_DEVINTERFACE_USB_DEVICE, ++dwMemberIdx, &DevIntfData);
        }

        SetupDiDestroyDeviceInfoList(hDevInfo);
    }

    return 0;
}
时间: 2024-10-12 15:35:47

USB Device Finder的相关文章

USB device &amp; USB controller &amp; USB passthrough

近期往 openstack 里倒腾 USB passthrough[1],遂把 USB 知识做较为全面的整理,以供分享. USB device 什么是 USB device, 上图机智的小萌狗就是 USB device,你的鼠标是 USB device, 键盘是 USB device,U 盘更是典型的 USB device.说了这么多例子,还是得用一个专业的名词一语概之,所谓 USB,即是 Universal Serial Bus(通用串行总线),它是用来连接 USB device 和计算机,从

usb host和usb device

S3C2440的数据手册将USB功能分为两章--usb host和usb device.具体什么意思呢? usb host: 微处理器作为usb主设备,可以挂接U盘之类的从属设备. usb device: 微处理器作为usb从属设备,其常用作接受PC机发送的命令. 参考网页:USB Host和USB Device的区别 usb host和usb device,布布扣,bubuko.com

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 the STM32F4 Discovery board but am having trouble getting windows to recognise it. Using USBlyzer all the descriptor info seems ok but windows will onl

U盘做为系统盘安装系统,出现start booting from usb device和boot failed解决方案

最近在旧机子上安装Centos系统的时候,用大白菜或者是UltraISO制作完U盘的系统盘后,出现了start booting from usb device和boot failed,一直很郁闷,以为是系统的原因.网上说把syslinux那个文件修改或者是替换,这些都不管用.最后发现是制作U启动的时候的格式,与bios启动U盘的格式不相符. BOIS启动USB有几种形式:USB-HDD,USB-FDD,USB-ZIP,USB-CDROM 1. ZIP模式是指把U盘模拟成ZIP驱动器模式,启动后U

Android Studio关于USB device not found的解决办法

Android Studio关于USB device not found的解决办法 我们使用Android Studio进行Android开发时,当我们使用真机进行调试时,很可能会出现USB device not found的问题.网上提出了很多决解办法,很多都是错误的.现给出正确的解决方法: 1. If yon don't have Android SDK installed, please install it first. 2. Open Start menu. Select Androi

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 installed in Fedora 15. Will first show how to do this manually, and second how to do it using the virt-manager tool. This HOWTO is limited to UHCI devices

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 operating systems rely on a unique device path to uniquely identify each USB device/interface connected to the system. A device path string is used to

【转载】How to Reset USB Device in Linux

USB devices are anywhere nowadays, even many embedded devices replace the traditional serial devices with usb devices. However, I experienced that USB devices hang from time to time. In most cases, a manual unplug and replug will solve the issue. Act

西部数据出现“WD SES Device USB Device”怎么办,而且说明书全是英文。

您好.wd ses device driver这个驱动程序可以在baidu中输入关键词找到,什么驱动之家.驱动人生之类的专业驱动网站也都是有的. western digital的移动硬盘驱动程序安装步骤请见下图: 西部数据出现"WD SES Device USB Device"怎么办,而且说明书全是英文.