i'm using a piece of c# code that detects connected devices and list them. the code is supposed to run on a kisok computer and help customers easily access and upload their files. the below code works fine except it does not recognize portable devices such as mobile devices.
public void WndProc(ref Message m)
{
int devType;
char c;
if (m.Msg == WM_DEVICECHANGE)
{
// WM_DEVICECHANGE can have several meanings depending on the WParam value...
switch (m.WParam.ToInt32())
{
// New device has just arrived
case DBT_DEVICEARRIVAL:
devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)
Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
// Get the drive letter
c = DriveMaskToLetter(vol.dbcv_unitmask);
/* rest of handler is here */
}
break;
case DBT_DEVNODES_CHANGED:
devType = Marshal.ReadInt32(m.LParam, 4); // m.LParam in this case is 0
// handle portable device
break;
}
}
}
so the mWParam received when connecting a mobile device is equal to DBT_DEVNODES_CHANGED (0x0007) and the mLParam is 0. is it possible from this event to recognize a portable device? is there a related detection API i can add here?
I've tried the following WMI code but like before HandleEvent is only fired when usb is connected but not a mobile phone :(
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
watcher.EventArrived += new EventArrivedEventHandler(HandleEvent);
watcher.Query = query;
watcher.Start();
watcher.WaitForNextEvent();