也可以通过SetupAPI更正式地获取此信息。传递dbcc_name
给传入SetupDiOpenDeviceInterface
的友好名称。SetupDiGetDeviceRegistryProperty
SPDRP_FRIENDLYNAME
这是一些可以做到的Delphi代码。(抱歉,您必须独立翻译成 C#)。
function ConvertDbccNameToFriendlyName(aDeviceInterfaceDbccName : string) : string;
var
deviceInfoHandle : HDEVINFO;
deviceInfoData : SP_DEVINFO_DATA;
deviceInterfaceData : SP_DEVICE_INTERFACE_DATA;
deviceInstanceId : string;
memberIndex : Cardinal;
begin
result := '';
// Create a new empty "device info set"
deviceInfoHandle := SetupDiCreateDeviceInfoList(nil, 0);
if deviceInfoHandle <> INVALID_HANDLE_VALUE then
begin
try
// Add "aDeviceInterfaceDbccName" to the device info set
FillChar(deviceInterfaceData, SizeOf(deviceInterfaceData), 0);
deviceInterfaceData.cbSize := SizeOf(deviceInterfaceData);
if SetupDiOpenDeviceInterface(deviceInfoHandle, PChar(aDeviceInterfaceDbccName), 0, @deviceInterfaceData) then
begin
try
// iterate over the device info set
// (though I only expect it to contain one item)
memberIndex := 0;
while true do
begin
// get device info that corresponds to the next memberIndex
FillChar(deviceInfoData, SizeOf(deviceInfoData), 0);
deviceInfoData.cbSize := SizeOf(deviceInfoData);
if not SetupDiEnumDeviceInfo(deviceInfoHandle, memberIndex, deviceInfoData) then
begin
// The enumerator is exhausted when SetupDiEnumDeviceInfo returns false
break;
end
else
begin
Inc(memberIndex);
end;
// Get the friendly name for that device info
if TryGetDeviceFriendlyName(deviceInfoHandle, deviceInfoData, {out} friendlyName) then
begin
result := friendlyName;
break;
end;
end;
finally
SetupDiDeleteDeviceInterfaceData(deviceInfoHandle, deviceInterfaceData);
end;
end;
finally
SetupDiDestroyDeviceInfoList(deviceInfoHandle);
end;
end;
end;
function TryGetDeviceFriendlyName(
var aDeviceInfoHandle : HDEVINFO;
var aDeviceInfoData : SP_DEVINFO_DATA;
out aFriendlyName : string) : boolean;
var
valueBuffer : array of byte;
regProperty : Cardinal;
propertyRegDataType : DWord;
friendlyNameByteSize : Cardinal;
success : boolean;
begin
aFriendlyName := '';
result := false;
// Get the size of the friendly device name
regProperty := SPDRP_FRIENDLYNAME;
friendlyNameByteSize := 0;
SetupDiGetDeviceRegistryProperty(
aDeviceInfoHandle, // handle to device information set
aDeviceInfoData, // pointer to SP_DEVINFO_DATA structure
regProperty, // property to be retrieved
propertyRegDataType, // pointer to variable that receives the data type of the property
nil, // pointer to PropertyBuffer that receives the property
0, // size, in bytes, of the PropertyBuffer buffer.
friendlyNameByteSize); // pointer to variable that receives the required size of PropertyBuffer
// Prepare a buffer for the friendly device name (plus space for a null terminator)
SetLength(valueBuffer, friendlyNameByteSize + sizeof(char));
success := SetupDiGetDeviceRegistryProperty(
aDeviceInfoHandle,
aDeviceInfoData,
regProperty,
propertyRegDataType,
@valueBuffer[0],
friendlyNameByteSize,
friendlyNameByteSize);
if success then
begin
// Ensure that only 'friendlyNameByteSize' bytes are used.
// Ensure that the string is null-terminated.
PChar(@valueBuffer[friendlyNameByteSize])^ := char(0);
// Get the returned value as a string
aFriendlyName := StrPas(PChar(@valueBuffer[0]));
end;
result := success;
end;
最后......如果您需要一种唯一标识 USB 设备的方法(不是您要求的,但通常这也是需要的),请查看SetupDiGetDeviceInstanceId
.