#include "stdafx.h"
#include <atlstr.h>
#include <stdio.h>
#include <vector>
using std::vector;
#include <setupapi.h>
#pragma comment(lib, "Setupapi.lib")
void GetAppleDeviceByEnumUSB(std::vector<CString>& devices);
int main( int argc, char *argv[ ], char *envp[ ] )
{
std::vector<CString> devices;
GetAppleDeviceByEnumUSB(devices);
getchar();
return 0;
}
void GetAppleDeviceByEnumUSB(std::vector<CString>& devices)
{
// get all device of system
DWORD dwFlag = (DIGCF_ALLCLASSES | DIGCF_PRESENT);
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, dwFlag);
if( INVALID_HANDLE_VALUE == hDevInfo )
{
printf("Failed to get system device list");
return;
}
// to enum USB device
SP_DEVINFO_DATA sDevInfoData;
sDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
CString strText;
TCHAR szDIS[MAX_PATH] = {0};
// Device Identification Strings,
DWORD nSize = 0 ;
for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo,i,&sDevInfoData); i++ )
{
nSize = 0;
if ( !SetupDiGetDeviceInstanceId(hDevInfo, &sDevInfoData, szDIS, sizeof(szDIS), &nSize) )
{
printf("get device id failed\n");
break;
}
// filter : USB\VID_XXXX&PID_XXXX\00000xxxxxxx
CString strDIS( szDIS );
strDIS.MakeUpper();
if( strDIS.Left( 3 ) == _T("USB") && -1 != strDIS.Find(L"VID_05AC"))
{
wprintf(L"device id : %s\n", strDIS);
devices.push_back(strDIS);
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}