views:

546

answers:

2

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).

A: 

It is likely you need to change this slightly

[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 StringBuilder dbcc_name;
}

Set the dbcc_size to 255, and construct the StringBuilder as shown below:

DEV_BROADCAST_DEVICEINTERFACE dbd = new DEV_BROADCAST_DEVICEINTERFACE;
dbd.dbcc_size = 255;
dbd.dbcc_name = new StringBuilder(dbd.dbcc_size);

Then pass that structure in, the value of dbcc_name should be populated.

Edit: after snicker's comment...I thought of this another way...

public struct DEV_BROADCAST_DEVICEINTERFACE
{
    public int dbcc_size;
    public int dbcc_devicetype;
    public int dbcc_reserved;
    public Guid dbcc_classguid;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 255, ArraySubType = System.Runtime.InteropServices.UnmanagedType.LPArray)]
    public string dbcc_name;
}

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 a IntPtr and is used to call the API...

Hope this helps, Best regards, Tom.

tommieb75
You cannot marshal fields that are StringBuilders. This doesn't work.
snicker
@Snicker: just realized...will edit this answer a bit more..
tommieb75
Tom. Your edit still doesn't work. You can only marshal strings to LPStr, LPWStr, LPTStr, BStr, or ByValTStr.
snicker
@Snicker: have you tried changing the attribute ' [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr, SizeConst = 255, ArraySubType = System.Runtime.InteropServices.UnmanagedType.LPStr)]'
tommieb75
Same results. No Name
snicker
The problem is not with RegisterDeviceNotification. The problem is marshaling a message into a new struct. It's possible the names just aren't coming though, but that seems unlikely
snicker
+3  A: 

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;
}
snicker