views:

14

answers:

1

I've been trying to watch a USB subsystem to detect when devices are added or removed, but I'm having trouble decoding the PDEV_BROADCAST_DEVICEINTERFACE::dbcc_name field. My code is based on an example over on codeproject.

If I right-click in my C++/CLI application in visual studio and go to the declaration in Dbt.h I get:

typedef struct _DEV_BROADCAST_DEVICEINTERFACE_W {
    DWORD       dbcc_size;
    DWORD       dbcc_devicetype;
    DWORD       dbcc_reserved;
    GUID        dbcc_classguid;
    wchar_t     dbcc_name[1];
} DEV_BROADCAST_DEVICEINTERFACE_W, *PDEV_BROADCAST_DEVICEINTERFACE_W;

Which suggests that the name should be a wchar_t. Feeding this into a new System::String just comes up with random characters. Is there more to converting a wchar_t string?

[edit] Essentially I want to do this in C++-cli but how do I port that?

+1  A: 

No idea what "feeding" might mean. Use Marshal::PtrToStringUni() to convert the string. If the window that receives this message was created with CreateWindowExA() then you need Marshal::PtrToStringAnsi().

Hans Passant
Feeding = passing and your suggestion (PtrToStringUni) seems to work well. Thanks very much - I was beginning to tear my heair out a little there! :-)
Jon Cage