转自:【VC】 获取电脑USB设备VID和PID等信息。
1,添加头文件和宏定义[cpp]view plain copy print ? #include<setupapi.h> #include<initguid.h> #pragmacomment(lib,"setupapi.lib")DEFINE_GUID(UsbClassGuid,0xa5dcbf10L,0x6530,0x11d2,0x90,0x1f,0x00,0xc0,0x4f,0xb9,0x51,0xed);
#include <setupapi.h>#include <initguid.h>#pragma comment(lib,"setupapi.lib")DEFINE_GUID (UsbClassGuid, 0xa5dcbf10L, 0x6530, 0x11d2, 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed);
[cpp]view plain copy print ? HDEVINFOhDevInfo;SP_DEVICE_INTERFACE_DATAspDevData;PSP_DEVICE_INTERFACE_DETAIL_DATApDetail;BOOLbRes=TRUE;intnCount=0;hDevInfo=::SetupDiGetClassDevs((LPGUID)&UsbClassGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);if(hDevInfo!=INVALID_HANDLE_VALUE){pDetail=(PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT,1024);pDetail->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);while(bRes){spDevData.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);bRes=::SetupDiEnumDeviceInterfaces(hDevInfo,NULL,(LPGUID)&UsbClassGuid,nCount,&spDevData);if(bRes){bRes=::SetupDiGetInterfaceDeviceDetail(hDevInfo,&spDevData,pDetail,1024,NULL,NULL);if(bRes){CStringszStr=pDetail->DevicePath;nCount++;}}}::GlobalFree(pDetail);::SetupDiDestroyDeviceInfoList(hDevInfo);}
HDEVINFO hDevInfo;SP_DEVICE_INTERFACE_DATA spDevData;PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;BOOL bRes = TRUE;int nCount = 0;hDevInfo = ::SetupDiGetClassDevs((LPGUID)&UsbClassGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);if (hDevInfo != INVALID_HANDLE_VALUE){pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT,1024);pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);while (bRes){spDevData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);bRes = ::SetupDiEnumDeviceInterfaces(hDevInfo,NULL,(LPGUID)&UsbClassGuid,nCount,&spDevData);if (bRes){bRes = ::SetupDiGetInterfaceDeviceDetail(hDevInfo,&spDevData,pDetail,1024,NULL,NULL);if (bRes){CString szStr = pDetail->DevicePath;nCount ++;}}}::GlobalFree(pDetail);::SetupDiDestroyDeviceInfoList(hDevInfo);}
pDetail->DevicePath = 0x00224f04 " \\?\usb#vid_0451&pid_f432#3aff466e374b2516#{a5dcbf10-6530-11d2-901f-00c04fb951ed }"