SETUPAPI函数脉络梳理
2023-10-12
58
0
SETUPAPI函数集是一个大大的集合,里面包括的函数巨多。其功能强大,但终因无法梳理出有效有脉络需让人望而生畏。
微软对此也只是一个简单的图示:
今天我们写一段代码,来概括上面的图示:
#include <windows.h>
#include <setupapi.h>
#include <initguid.h>
#include <stdio.h>
#include <string.h>
#include <winioctl.h>
#include <locale.h>
#include <tchar.h>
#pragma comment(lib,"Setupapi.lib ")
/* f18a0e88-c30c-11d0-8815-00a0c906bed8 */
DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB, 0xf18a0e88, 0xc30c, 0x11d0, 0x88, 0x15, 0x00, \
0xa0, 0xc9, 0x06, 0xbe, 0xd8);
/* A5DCBF10-6530-11D2-901F-00C04FB951ED */
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, \
0xC0, 0x4F, 0xB9, 0x51, 0xED);
void CUsb::TestSetup()
{
const GUID* pGuid = &(GUID_DEVINTERFACE_USB_DEVICE);
HDEVINFO dev = SetupDiGetClassDevs((GUID*)pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (dev == INVALID_HANDLE_VALUE)
{
printf("No HDEVINFO available for this GUID\n");
return ;
}
SP_DEVINFO_DATA dev_info_data;
dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
for (int i = 0; i < 255; i++)
{
if (!SetupDiEnumDeviceInfo(dev, i, &dev_info_data))
{
break;
}
TCHAR id_inst[MAX_PATH] = { 0 };
if (!SetupDiGetDeviceInstanceId(dev, &dev_info_data, id_inst, MAX_PATH, NULL))
{
break;
}
printf("%d\t %ws\n", i, id_inst);
{
TCHAR HardwareId[MAX_PATH] = { 0 };
ULONG length = 0;
SetupDiGetDeviceRegistryProperty(dev, &dev_info_data, SPDRP_HARDWAREID,NULL, (PBYTE)HardwareId, MAX_PATH, &length);
for (TCHAR* p = HardwareId; *p ;p += _tcslen(p) + 1)
{
printf("\t %ws\n", p);
}
}
for (int index = 0;; index++)
{
SP_DEVICE_INTERFACE_DATA dev_interface_data;
dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces(dev, &dev_info_data, pGuid, index, &dev_interface_data))
{
break;
}
ULONG len = 0;
if (!SetupDiGetDeviceInterfaceDetail(dev, &dev_interface_data, NULL, 0, &len, NULL))
{
if (0x7a != GetLastError())
{
break;
}
}
PSP_DEVICE_INTERFACE_DETAIL_DATA pdev_interface_detail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(len);
if (pdev_interface_detail)
{
pdev_interface_detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SetupDiGetDeviceInterfaceDetail(dev, &dev_interface_data, pdev_interface_detail, len, &len, NULL);
printf("%d.%d\t %ws\n", i,index, pdev_interface_detail->DevicePath);
free(pdev_interface_detail);
}
}
}
SetupDiDestroyDeviceInfoList(dev);
}
在本人电脑上执行的结果如下:
0.0 \\?\usb#vid_0bda&pid_8153#000001#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
0 USB\VID_0BDA&PID_8153\000001
USB\VID_0BDA&PID_8153&REV_3000
USB\VID_0BDA&PID_8153
0.0 \\?\usb#vid_0bda&pid_8153#000001#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
1 USB\VID_093A&PID_2510\6&26EF342A&0&4
USB\VID_093A&PID_2510&REV_0100
USB\VID_093A&PID_2510
1.0 \\?\usb#vid_093a&pid_2510#6&26ef342a&0&4#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
2 USB\VID_12D1&PID_3A07\029000
USB\VID_12D1&PID_3A07&REV_0100
USB\VID_12D1&PID_3A07
2.0 \\?\usb#vid_12d1&pid_3a07#029000#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
3 USB\VID_1058&PID_2655\MSFT30323331324430343032343533
USB\VID_1058&PID_2655&REV_6002
USB\VID_1058&PID_2655
3.0 \\?\usb#vid_1058&pid_2655#msft30323331324430343032343533#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
4 USB\VID_0000&PID_F001&REV_0100\1&79F5D87&35&01
USB\VID_0000&PID_F001&REV_0100
4.0 \\?\usb#vid_0000&pid_f001&rev_0100#1&79f5d87&35&01#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
5 USB\VID_0CF3&PID_E009\5&2E89C326&0&10
USB\VID_0CF3&PID_E009&REV_0001
USB\VID_0CF3&PID_E009
5.0 \\?\usb#vid_0cf3&pid_e009#5&2e89c326&0&10#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
6 USB\VID_0C45&PID_671E\5&2E89C326&0&6
USB\VID_0C45&PID_671E&REV_8502
USB\VID_0C45&PID_671E