头文件
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
#pragma comment(lib,"setupapi.lib")
DEFINE_GUID (UsbClassGuid, 0xa5dcbf10L, 0x6530, 0x11d2, 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed);
//获取USB设备VID和PID
string GetUsbInfo()
{
HDEVINFO hDevInfo;
SP_DEVICE_INTERFACE_DATA spDevData;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
BOOL bRes = TRUE;
int nCount = 0;
string csResult;
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)
{
string szStr = pDetail->DevicePath;
csResult += szStr +"\n";
nCount ++;
}
}
}
::GlobalFree(pDetail);
::SetupDiDestroyDeviceInfoList(hDevInfo);
}
return csResult;
}
测试程序
int main(void)
{
string csStr = GetUsbInfo();