(1)获取设备路径
通过IMoniker的BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag)方法可以获取IPropertyBag对象,然后通过IPropertyBag对象的Read(L"DevicePath", &varDevPath, NULL)方法获取设备路径。
void GetMonikerDeviceInfo(IMoniker *pM)
{
HRESULT hr;
IPropertyBag *pBag = 0;
USES_CONVERSION;
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
if(SUCCEEDED(hr))
{
VARIANT varDevPath;
varDevPath.vt = VT_BSTR;
hr = pBag->Read(L"DevicePath", &varDevPath, NULL);
{
if(SUCCEEDED(hr))
{
_tprintf(TEXT("DevicePath = %s\n"), W2T(varDevPath.bstrVal));
GetDevPathInfo(W2T(varDevPath.bstrVal));
SysFreeString(varDevPath.bstrVal);
}
}
SAFE_RELEASE(pBag);
}
}
(2)通过设备路径获取设备属性
通过SetupDiOpenDeviceInterface函数打开设备路径,在通过SetupDiGetDeviceInterfaceDetail函数获取SP_DEVINFO_DATA结构的值,有了这个结构的值就可以调用SetupDiGetDeviceRegistryProperty函数获取设备的一系列属性值了。
void GetDevPathInfo(LPCTSTR szIntfPath)
{
HDEVINFO hDevInfo = SetupDiCreateDeviceInfoList(NULL, NULL);
if (INVALID_HANDLE_VALUE == hDevInfo)
{
return;
}
BYTE Buf[1024] = {0};
TCHAR szTemp[MAX_PATH] = {0};
SP_DEVICE_INTERFACE_DATA spdid = {0};
spdid.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf;
pspdidd->cbSize = sizeof(*pspdidd);
SP_DEVINFO_DATA spdd = {0};
spdd.cbSize = sizeof(spdd);
DWORD dwSize = 0;
do
{
if (!SetupDiOpenDeviceInterface(
hDevInfo,
szIntfPath,
0,
&spdid))
{
break;
}
dwSize = sizeof(Buf);
if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &spdid, pspdidd, dwSize, &dwSize, &spdd))
{
break;
}
//INFO(TEXT("DevicePath = %s\n"), pspdidd->DevicePath);
if(SetupDiClassNameFromGuid(&spdd.ClassGuid, szTemp, MAX_PATH, &dwSize))
{
INFO(TEXT("Class = %s\n"), szTemp);
}
if (SetupDiGetClassDescription(&spdd.ClassGuid, szTemp, MAX_PATH, &dwSize))
{
INFO(TEXT("ClassDescription = %s\n"), szTemp);
}
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spdd, SPDRP_DEVICEDESC, NULL, (PBYTE)szTemp, MAX_PATH - 1, NULL))
{
INFO(TEXT("DeviceDesc = %s\n"), szTemp);
}
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spdd, SPDRP_SERVICE, NULL, (PBYTE)szTemp, MAX_PATH - 1, NULL))
{
INFO(TEXT("Service = %s\n"), szTemp);
}
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spdd, SPDRP_LOCATION_PATHS, NULL, (PBYTE)szTemp, MAX_PATH - 1, NULL))
{
INFO(TEXT("LocationPath = %s\n"), szTemp);
}
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spdd, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szTemp, MAX_PATH - 1, NULL))
{
INFO(TEXT("FriendlyName = %s\n"), szTemp);
}
GetLocationInfo(spdd.DevInst, szTemp, MAX_PATH);
INFO(TEXT("\n"));
}
while (0);
SetupDiDestroyDeviceInfoList(hDevInfo);
}
老夫电脑上输出如下: