如何从DEV_BROADCAST_DEVICEINTERFACE和设备实例ID得到友好的设备名称 [英] How to get friendly device name from DEV_BROADCAST_DEVICEINTERFACE and Device Instance ID

查看:3313
本文介绍了如何从DEV_BROADCAST_DEVICEINTERFACE和设备实例ID得到友好的设备名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册了一个窗口<一个href=\"http://msdn.microsoft.com/en-us/library/aa363431%28VS.85%29.aspx\">RegisterDeviceNotification并能成功收到<一个href=\"http://msdn.microsoft.com/en-us/library/aa363244%28VS.85%29.aspx\">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;
}

和我使用 Marshal.PtrToStructure 上的 WM_DEVICECHANGE 消息。

如果有这样的工作吗?

Should this be working?

甚至更好?是否有其他方式连接时获得设备的名称?

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}

\?\USB#VID_05AC&PID_1294&MI_00#0#{6bdd1fc6-810f-11d0-bec7-08002be2092f}

和我真的只是希望它说:苹果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 = @"SYSTEM\CurrentControlSet\Enum\" + 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得到友好的设备名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆