Here it is, if I can help with any questions I will.
#include "stdafx.h"
#include "USBHubArray.h"
#include "..\\COMMON\\Registry.h"
#include <cfg.h>
#include <cfgmgr32.h>
#include <Dbt.h>
#include <setupapi.h>
#pragma comment (lib, "Setupapi.lib")
... Rest of class (CUSBHubArray) omitted for clarity
BOOL CUSBHubArray::Enum(CString* pError)
{
Clear();
HDEVINFO hDevices = SetupDiGetClassDevs(
&GUID_DEVINT_USB_HUB,
0, 0, DIGCF_DEVICEINTERFACE);
if(hDevices == INVALID_HANDLE_VALUE)
{
if(pError)
*pError = GetLastWin32Error(
_T("CUSBHubArray::Enum"),
_T("SetupDiGetClassDevs")
);
return FALSE;
}
const unsigned int PROP_BUFF_SZ = 1000;
LPTSTR pBuffer = (LPTSTR)LocalAlloc(LPTR, PROP_BUFF_SZ);
ASSERT(pBuffer);
memset(pBuffer, 0, PROP_BUFF_SZ);
SP_DEVINFO_DATA did = {0};
did.cbSize = sizeof(SP_DEVINFO_DATA);
for(DWORD dw = 0; SetupDiEnumDeviceInfo(hDevices, dw, &did); dw++)
{
if(!SetupDiGetDeviceRegistryProperty(hDevices, &did,
SPDRP_DEVICEDESC, NULL, (PBYTE)pBuffer, PROP_BUFF_SZ, NULL))
{
if(pError)
*pError = GetLastWin32Error(
_T("CUSBHubArray::Enum"),
_T("SetupDiGetDeviceRegistryProperty")
);
LocalFree(pBuffer);
SetupDiDestroyDeviceInfoList(hDevices);
return FALSE;
}
if( (_tcsstr(pBuffer, _T("Standard-USB-Hub")) == 0) &&
(_tcsstr(pBuffer, _T("Generic USB Hub")) == 0) )
continue;
CUSBHubItem* pHubInfo = new CUSBHubItem;
Add(pHubInfo);
ULONG ulProblem = 0;
ULONG ulStatus = 0;
CONFIGRET cr = CM_Get_DevNode_Status(&ulStatus, &ulProblem, (DEVINST)did.DevInst, 0);
if(cr & CR_FAILURE)
pHubInfo->m_bPresent = false;
if(ulProblem & CM_PROB_DISABLED)
pHubInfo->m_bDisabled = true;
pHubInfo->m_cstrDeviceName = pBuffer;
memset(pBuffer, 0, PROP_BUFF_SZ);
if(!SetupDiGetDeviceInstanceId(hDevices,
&did, pBuffer, PROP_BUFF_SZ, NULL))
{
if(pError)
*pError = GetLastWin32Error(
_T("CUSBHubArray::Enum"),
_T("SetupDiGetDeviceInstanceId")
);
LocalFree(pBuffer);
SetupDiDestroyDeviceInfoList(hDevices);
return FALSE;
}
pHubInfo->m_cstrInstanceID = pBuffer;
memset(pBuffer, 0, PROP_BUFF_SZ);
if(!SetupDiGetDeviceRegistryProperty(hDevices, &did,
SPDRP_HARDWAREID, NULL, (PBYTE)pBuffer, PROP_BUFF_SZ, NULL))
{
if(pError)
*pError = GetLastWin32Error(
_T("CUSBHubArray::Enum"),
_T("SetupDiGetDeviceRegistryProperty")
);
LocalFree(pBuffer);
SetupDiDestroyDeviceInfoList(hDevices);
return FALSE;
}
pHubInfo->m_cstrHardwareID = pBuffer;
memset(pBuffer, 0, PROP_BUFF_SZ);
CString strRegKey = _T("SYSTEM\\CurrentControlSet\\Enum\\USB\\");
strRegKey += pHubInfo->m_cstrInstanceID.Mid(4);
pHubInfo->m_cstrRegKey = strRegKey;
CString strVal = _T("");
GetRegStr(HKEY_LOCAL_MACHINE, strRegKey,
_T("ParentIdPrefix"), strVal.GetBuffer(MAX_PATH));
strVal.ReleaseBuffer();
pHubInfo->m_cstrPIDP = strVal;
CString strDPRegKey = strRegKey;
strDPRegKey += _T("\\Device Parameters");
GetRegStr(HKEY_LOCAL_MACHINE, strDPRegKey,
_T("SymbolicName"), strVal.GetBuffer(MAX_PATH));
strVal.ReleaseBuffer();
pHubInfo->m_cstrSymbName = strVal;
pHubInfo->m_dwGENumber = GetRegDWORD(HKEY_LOCAL_MACHINE, strDPRegKey, _T("GENumber"), 0);
}
LocalFree(pBuffer);
SetupDiDestroyDeviceInfoList(hDevices);
return TRUE;
}