在我的 UMDF 设备驱动程序中,我以编程方式为我的设备设置了一个友好名称:
HRESULT CMyDevice::SetFriendlyDeviceName(WCHAR * FriendlyName)
{
HRESULT hr = HRESULT_FROM_WIN32(ERROR_GEN_FAILURE);
CComPtr<IWDFUnifiedPropertyStoreFactory> upsf = NULL;
hr = m_FxDevice->QueryInterface(&upsf);
if (SUCCEEDED(hr))
{
CComPtr<IWDFUnifiedPropertyStore> ups = NULL;
WDF_PROPERTY_STORE_ROOT rs;
RtlZeroMemory(&rs, sizeof(WDF_PROPERTY_STORE_ROOT));
rs.LengthCb = sizeof(WDF_PROPERTY_STORE_ROOT);
rs.RootClass = WdfPropertyStoreRootClassHardwareKey;
rs.Qualifier.HardwareKey.ServiceName = NULL;
hr = upsf->RetrieveUnifiedDevicePropertyStore(&rs, &ups);
if (SUCCEEDED(hr))
{
ULONG len = ((ULONG)wcslen(FriendlyName) * sizeof(WCHAR)) + sizeof(WCHAR);
hr = ups->SetPropertyData(&DEVPKEY_Device_FriendlyName, 0, 0, DEVPROP_TYPE_STRING, len, FriendlyName);
if (FAILED(hr))
TraceEvents(TRACE_LEVEL_WARNING, TRACE_DEVICE, "Failed to set friendly name for device: %x", hr);
else
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "Friendly name for device successfully set");
}
else
TraceEvents(TRACE_LEVEL_WARNING, TRACE_DEVICE, "Failed to retrieve unified device property store: %x", hr);
}
else
TraceEvents(TRACE_LEVEL_WARNING, TRACE_DEVICE, "Failed to retrieve unified property store factory: %x", hr);
}
它适用于 Windows 8 和 10。问题是它不适用于 Windows 7 和 Vista。虽然执行上述代码时我没有收到任何错误,并且显示“设备的友好名称已成功设置”,但设备管理器中的友好名称并没有改变。刷新设备列表后它也不会改变。当我在注册表中查看设备的硬件密钥时,HKLM\SYSTEM\CurrentControlSet\Enum\USB\VID_XXXX&PID_XXXX\XXXX
缺少“FriendlyName”的值。当我手动添加它时,它会显示在设备管理器中。