如何从 DEV_BROADCAST_DEVICEINTERFACE 和设备实例 ID 获取友好的设备名称
我已使用 RegisterDeviceNotification 注册了一个窗口,并且可以成功接收 DEV_BROADCAST_DEVICEINTERFACE 消息。但是,返回的结构中的 dbcc_name 字段始终为空。我的结构定义如下:
[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;
}
我在 WM_DEVICECHANGE 消息。
这应该有效吗?
或者甚至更好...是否有其他方法可以在连接时获取设备的名称?
编辑(02/05/2010 20:56GMT):
我发现了如何通过这样做来填充 dbcc_name 字段:
[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。它看起来像下面这样:
\?\USB#VID_05AC&PID_1294&MI_00#0#{6bdd1fc6-810f-11d0-bec7-08002be2092f}
我真的只想说“Apple iPhone”(这就是本例中的设备)。
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.
Should this be working?
Or even better... Is there an alternative way to get the name of a device upon connection?
EDIT (02/05/2010 20:56GMT):
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;
}
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}
And I really just want it to say "Apple iPhone" (which is what the device is in this case).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,如上所述,我发现了如何正确填充 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:
还可以通过 SetupAPI。将
dbcc_name
传递给SetupDiOpenDeviceInterface
,并通过传入SPDRP_FRIENDLYNAME
的SetupDiGetDeviceRegistryProperty
获取友好名称。这里有一些 Delphi 代码可以做到这一点。 (抱歉,您必须独立翻译为 C#)。
最后...如果您需要一种唯一标识 USB 设备的方法(不是您所要求的,但通常也是需要的),请查看
SetupDiGetDeviceInstanceId
。This information can also be acquired more formally through SetupAPI. Pass
dbcc_name
toSetupDiOpenDeviceInterface
and get the friendly name withSetupDiGetDeviceRegistryProperty
passing inSPDRP_FRIENDLYNAME
.Here's some Delphi code that will do it. (Sorry, you'll have to translate to C# independently).
Finally... if you need a way to uniquely identify a USB device (not what you asked for, but commonly this is also needed), look into
SetupDiGetDeviceInstanceId
.您可能需要稍微更改一下
将
dbcc_size
设置为 255,并构造 StringBuilder,如下所示:然后传入该结构,应填充
dbcc_name
的值。编辑:在窃笑的评论之后...我想到了另一种方式...
设置
dbcc_size
到 255,然后从那里获取...编辑#2:这很有趣...现在不太确定,我发现这篇文章在<上使用了
RegisterDeviceNotification
a href="http://www.codeproject.com/KB/system/DriveDetector.aspx" rel="nofollow noreferrer">Codeproject ,它使用不同的 RegisterDeviceNotification 方式,该结构被编组为IntPtr
用于调用API...It is likely you need to change this slightly
Set the
dbcc_size
to 255, and construct the StringBuilder as shown below:Then pass that structure in, the value of
dbcc_name
should be populated.Edit: after snicker's comment...I thought of this another way...
Set the
dbcc_size
to 255, and take it from there...Edit#2: This is interesting...am not so sure now, I found this article that uses
RegisterDeviceNotification
on Codeproject and it uses a different way of RegisterDeviceNotification in that the struct is marshalled into aIntPtr
and is used to call the API...解析设备实例的设备接口不是一个好主意。
DEVPKEY_NAME 是您的最佳查询应该做的。
首先,您必须将设备接口名称转换为设备实例 ID。
像这样的SetupAPI:
或者使用 更现代的cfgmgr32 API:
Parsing device interface of device instance is not a good idea.
DEVPKEY_NAME is a best query you should do.
First, you have to convert device interface name to device instance id.
Something like this with SetupAPI:
Or with more modern cfgmgr32 API: