#include <wtypes.h>
#include <setupapi.h>
#pragma comment(lib, "setupapi.lib")
BOOL EnumUSBDevice(WORD uVID, WORD uPID, //USB VID PID
BOOL bPresentFlag, // 0
TCHAR szDevPath[MAX_PATH+1] = NULL, //
int iIndex=0) // N ( )
{
BOOL bRet = FALSE;
int iFoundCount = 0; //
//
DWORD dwFlage = DIGCF_ALLCLASSES|DIGCF_DEVICEINTERFACE;
if(bPresentFlag)
dwFlage |= DIGCF_PRESENT;
HDEVINFO hdev = SetupDiGetClassDevs(NULL, NULL, NULL, dwFlage);
if (INVALID_HANDLE_VALUE != hdev)
{
SP_DEVINFO_DATA DeviceInfoData = {0};
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD DeviceIndex = 0;
while (SetupDiEnumDeviceInfo(hdev, DeviceIndex, &DeviceInfoData))
{
DeviceIndex++;
int idev = 0;
while(1)
{
SP_DEVICE_INTERFACE_DATA did = {0};
did.cbSize = sizeof(did);
if(SetupDiEnumDeviceInterfaces(hdev, NULL, &DeviceInfoData.ClassGuid, idev, &did))
{
idev++;
DWORD cbRequired = 0;
SetupDiGetDeviceInterfaceDetail(hdev, &did, 0, 0, &cbRequired, 0);
if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
{
PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd =
(PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR,
cbRequired);
if (pdidd)
{
pdidd->cbSize = sizeof(*pdidd);
if (SetupDiGetDeviceInterfaceDetail(hdev, &did, pdidd, cbRequired, &cbRequired, 0))
{
OutputDebugString(pdidd->DevicePath);
OutputDebugString(_T("\r
"));
if(uPID && uVID)
{
LPTSTR pszVid = _tcsstr(pdidd->DevicePath, _T("vid_"));
LPTSTR pszPid = _tcsstr(pdidd->DevicePath, _T("pid_"));
if(pszVid && pszPid)
{
TCHAR szVID[5]={0}, szPID[5]={0};
_tcsncpy_s(szVID, pszVid+4, 4);
_tcsncpy_s(szPID, pszPid+4, 4);
UINT pid = (UINT)_tcstol(szPID, NULL, 16);
UINT vid = (UINT)_tcstol(szVID, NULL, 16);
if(pid == uPID && vid == uVID)
{
if(iFoundCount == iIndex)
{
if(szDevPath)
{
_tcscpy_s(szDevPath, MAX_PATH, pdidd->DevicePath);
}
bRet = TRUE;
}
iFoundCount++;
}
}
}
}
LocalFree(pdidd);
}
}
}
else
{
if (ERROR_NO_MORE_ITEMS == GetLastError())
{
break; // Enumeration failed - perhaps there are no more items
}
}
}
}
SetupDiDestroyDeviceInfoList(hdev);
}
return bRet;
}
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szDevPath[MAX_PATH+1] = {0};
if(EnumUSBDevice(0x403, 0x6001, TRUE, szDevPath))
{
MessageBox(NULL, szDevPath, _T("Usb Device"), MB_OK);
}
return 0;
}