12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include "stdafx.h"
- #include <winioctl.h>
- #include <setupapi.h>
- #include "WDK_VidPidQuery.h"
- #pragma comment (lib, "Setupapi.lib")
- #define DeviceInstanceIdSize 256 // 设备实例ID最大长度
- // 获取系统的VID和PID集合
- INT WINAPI WDK_WhoAllVidPid( HIDD_VIDPID* pVidPid, INT iCapacity, const GUID* SetupClassGuid, const GUID* InterfaceClassGuid )
- {
- // 检测入口参数
- if (pVidPid == NULL || iCapacity <= 0) return 0;
- // 根据设备安装类GUID创建空的设备信息集合
- HDEVINFO DeviceInfoSet = SetupDiCreateDeviceInfoList( SetupClassGuid, NULL );
- if (DeviceInfoSet == INVALID_HANDLE_VALUE) return -1;
-
- // 根据设备安装类GUID获取设备信息集合
- HDEVINFO hDevInfo;
- if(InterfaceClassGuid == NULL)
- hDevInfo = SetupDiGetClassDevsEx( NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE | DIGCF_PRESENT, DeviceInfoSet, NULL, NULL );
- else
- hDevInfo = SetupDiGetClassDevsEx( InterfaceClassGuid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT, DeviceInfoSet, NULL, NULL );
- if (hDevInfo == INVALID_HANDLE_VALUE) return -1;
- // 存储得到的VID和PID数目
- INT iTotal = 0;
- // 存储设备实例ID
- TCHAR DeviceInstanceId[DeviceInstanceIdSize];
- // 存储设备信息数据
- SP_DEVINFO_DATA DeviceInfoData;
- DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
- // 获取设备信息数据
- DWORD DeviceIndex = 0;
- while (SetupDiEnumDeviceInfo( hDevInfo, DeviceIndex++, &DeviceInfoData))
- {
- // 获取设备实例ID
- if (SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, DeviceInstanceId, DeviceInstanceIdSize, NULL))
- {
- // 从设备实例ID中提取VID和PID
- TCHAR* pVidIndex = _tcsstr(DeviceInstanceId, TEXT("VID_"));
- if (pVidIndex == NULL) continue;
- TCHAR* pPidIndex = _tcsstr(pVidIndex + 4, TEXT("PID_"));
- if (pPidIndex == NULL) continue;
- USHORT VendorID = _tcstoul(pVidIndex + 4, NULL, 16);
- USHORT ProductID = _tcstoul(pPidIndex + 4, NULL, 16);
- // 剔除重复的VID和PID
- if (!WDK_isExistVidPid( VendorID, ProductID, pVidPid, iTotal ))
- {
- pVidPid[iTotal].VendorID = VendorID;
- pVidPid[iTotal].ProductID = ProductID;
- if (++iTotal >= iCapacity) break;
- }
- }
- }
- return iTotal;
- }
- // 判断VID和PID是否已经存在
- BOOL WINAPI WDK_isExistVidPid( USHORT VendorID, USHORT ProductID, const HIDD_VIDPID* pVidPid, INT iSize )
- {
- if (pVidPid != NULL)
- {
- for (INT i = 0; i < iSize; i++)
- {
- if (pVidPid[i].VendorID == VendorID && pVidPid[i].ProductID == ProductID)
- return TRUE;
- }
- }
- return FALSE;
- }
|