Событие вставки флешки
От: Decoy Литва  
Дата: 21.02.08 08:47
Оценка: 11 (2)
#Имя: FAQ.dotnet.DEVICEARRIVAL
Здравствуйте, Аноним, Вы писали:

А>Виндовое сообщение DBT_DEVICEARRIVAL...


Да, совершенно верно, приходит сообщение WM_DEVICECHANGE с WParam=DBT_DEVICEARRIVAL. В конкретном случае для флешки код на C# будет таков :

using System.Collections;
using System.Runtime.InteropServices;

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WM_DEVICECHANGE)
            {
                switch (m.WParam.ToInt32())
                {
                    case DBT_DEVNODES_CHANGED:
                        break;

                    case DBT_DEVICEARRIVAL:
                        break;

                    case DBT_DEVICEREMOVECOMPLETE:
                        DEV_BROADCAST_HDR dbh = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
                        if (dbh.dbch_devicetype == DBT_DEVTYP_VOLUME)
                        {
                            DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
                            BitArray bArray = new BitArray(new byte[] 
                            {
                                (byte)(dbv.dbcv_unitmask & 0x00FF),
                                (byte)((dbv.dbcv_unitmask & 0xFF00) >> 8),
                                (byte)((dbv.dbcv_unitmask & 0xFF0000) >> 16),
                                (byte)((dbv.dbcv_unitmask & 0xFF000000) >> 24)
                            });

                            int driveLetter = Char.ConvertToUtf32("A", 0);
                            for (int i = 0; i < bArray.Length; i++)
                            {
                                if (bArray.Get(i))
                                {
                                    Console.WriteLine("Removed driveLetter={0}", driveLetter);
                                    Console.WriteLine("Removed drive={0}", Char.ConvertFromUtf32(driveLetter));
                                }
                                driveLetter += 1;
                            }
                        }
                        break;
                }
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct DEV_BROADCAST_HDR
        {
            public uint dbch_size;
            public uint dbch_devicetype;
            public uint dbch_reserved;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct DEV_BROADCAST_VOLUME
        {
            public uint dbcv_size;
            public uint dbcv_devicetype;
            public uint dbcv_reserved;
            public uint dbcv_unitmask;
            public ushort dbcv_flags;//public System.UInt16
        }

        private const int WM_DEVICECHANGE = 0x0219;

        private const int DBT_DEVNODES_CHANGED = 0x0007;

        private const int DBT_DEVICEARRIVAL = 0x8000;  // system detected a new device
        private const int DBT_DEVICEQUERYREMOVE = 0x8001;  // wants to remove, may fail
        private const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;  // removal aborted
        private const int DBT_DEVICEREMOVEPENDING = 0x8003;  // about to remove, still avail.
        private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;  // device is gone
        private const int DBT_DEVICETYPESPECIFIC = 0x8005;  // type specific event

        private const uint DBT_DEVTYP_OEM = 0x00000000;  // oem-defined device type
        private const uint DBT_DEVTYP_DEVNODE = 0x00000001;  // devnode number
        private const uint DBT_DEVTYP_VOLUME = 0x00000002;  // logical volume
        private const uint DBT_DEVTYP_PORT = 0x00000003;  // serial, parallel
        private const uint DBT_DEVTYP_NET = 0x00000004;  // network resource

        private const ushort DBTF_MEDIA = 0x0001;          // media comings and goings


Обработка DBT_DEVICEARRIVAL такая же, как и для DBT_DEVICEREMOVECOMPLETE, с разницей лишь
Console.WriteLine("Inserted driveLetter={0}", driveLetter);
Console.WriteLine("Inserted drive={0}", Char.ConvertFromUtf32(driveLetter));
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.