1

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();
Salmonela
  • 95
  • 1
  • 2
  • 11
  • check this answer http://stackoverflow.com/questions/16245706/check-for-device-change-add-remove-events also check http://stackoverflow.com/questions/620144/detecting-usb-drive-insertion-and-removal-using-windows-service-and-c-sharp I used both and it works. I would recommend the second solution. – BudBrot May 07 '14 at 08:46
  • you used both with portable devices? i couldn't make the WndProc solution to work with mobile phones, as i stated above. – Salmonela May 07 '14 at 10:47
  • Im sorry. I interpreted "mobile" devices as "movable" devices . I havnt tested it with phones, i used USBs (as a dongle). – BudBrot May 07 '14 at 11:18
  • well for removable devices and hdd's the [Dolinay](http://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-a-C-Program) solution works great. thanks for the try :) – Salmonela May 07 '14 at 11:30

0 Answers0