问题描述
我已经使用 RegisterDeviceNotification注册了一个窗口a> 并且可以成功接收 DEV_BROADCAST_DEVICEINTERFACE 消息.但是,返回结构中的 dbcc_name
字段始终为空.我的结构定义如下:
I've registered a window with RegisterDeviceNotification and can successfully recieve DEV_BROADCAST_DEVICEINTERFACE messages. However, the dbcc_name
field in the returned struct is always empty. The struct I have is defined as such:
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
[MarshalAs(UnmanagedType.LPStr)]
public string dbcc_name;
}
我在 消息.
And I'm using Marshal.PtrToStructure
on the LParam of the WM_DEVICECHANGE message.
这应该有效吗?
或者更好...有没有其他方法可以在连接时获取设备名称?
Or even better... Is there an alternative way to get the name of a device upon connection?
编辑(02/05/2010 20:56GMT):
我通过这样做找到了如何填充 dbcc_name 字段:
I found out how to get the dbcc_name field to populate by doing this:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)]
public string dbcc_name;
}
但我仍然需要一种方法来从 int dbcc_name 中获取友好"名称.如下所示:
but I still need a way to get a "Friendly" name from what is int dbcc_name. It looks like the following:
?USB#VID_05AC&PID_1294&MI_00#0#{6bdd1fc6-810f-11d0-bec7-08002be2092f}
我真的只是想让它说Apple iPhone"(在这种情况下就是设备).
And I really just want it to say "Apple iPhone" (which is what the device is in this case).
推荐答案
好吧,如上所述,我发现了如何正确填充 dbcc_name.我发现这是获取设备名称的最简单方法:
Well, as noted above I found out how to get dbcc_name to populate correctly. I found that this was the easiest way to get the device name:
private static string GetDeviceName(DEV_BROADCAST_DEVICEINTERFACE dvi)
{
string[] Parts = dvi.dbcc_name.Split('#');
if (Parts.Length >= 3)
{
string DevType = Parts[0].Substring(Parts[0].IndexOf(@"?") + 2);
string DeviceInstanceId = Parts[1];
string DeviceUniqueID = Parts[2];
string RegPath = @"SYSTEMCurrentControlSetEnum" + DevType + "\" + DeviceInstanceId + "\" + DeviceUniqueID;
RegistryKey key = Registry.LocalMachine.OpenSubKey(RegPath);
if (key != null)
{
object result = key.GetValue("FriendlyName");
if (result != null)
return result.ToString();
result = key.GetValue("DeviceDesc");
if (result != null)
return result.ToString();
}
}
return String.Empty;
}
这篇关于如何从 DEV_BROADCAST_DEVICEINTERFACE 和设备实例 ID 获取友好的设备名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!