Пример #1
0
        public void RegisterDeviceNotification(string deviceName)
        {
            IntPtr fileHandle = SafeNativeMethods.CreateFile(deviceName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
                                                             0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_NORMAL, 0);

            if (fileHandle != INVALID_HANDLE_VALUE)
            {
                DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE
                {
                    DBCH_DeviceType = DBT_DEVTYP_HANDLE,
                    DBCH_Reserved   = 0,
                    DBCH_NameOffset = 0,
                    DBCH_Handle     = fileHandle,
                    DBCH_HDevNotify = IntPtr.Zero
                };

                int size = Marshal.SizeOf(data);
                data.DBCH_Size = size;
                IntPtr buffer = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(data, buffer, true);

                IntPtr deviceHandle = SafeNativeMethods.RegisterDeviceNotification(SpongeWindow.Handle, buffer, 0);
                if (deviceHandle != IntPtr.Zero)
                {
                    DeviceHandles.Add(deviceHandle, deviceName);
                }
            }
        }
Пример #2
0
 private void UnregisterDeviceNotification(IntPtr deviceHandle, IntPtr fileHandle)
 {
     if (DeviceHandles.ContainsKey(deviceHandle))
     {
         SafeNativeMethods.CloseHandle(fileHandle);
         SafeNativeMethods.UnregisterDeviceNotification(deviceHandle);
     }
 }
Пример #3
0
 private void UnregisterDeviceNotification(IntPtr deviceHandle, IntPtr fileHandle)
 {
     if (DeviceHandles.ContainsKey(deviceHandle))
     {
         CloseHandle(fileHandle);
         UnregisterDeviceNotification(deviceHandle);
     }
 }
Пример #4
0
        private void ProcessMessage(Message message)
        {
            if (message.Msg == WM_DEVICECHANGE)
            {
                switch (message.WParam.ToInt32())
                {
                case DBT_DEVICEARRIVAL:
                    if (Marshal.ReadInt32(message.LParam, 4) == DBT_DEVTYP_VOLUME)
                    {
                        DEV_BROADCAST_VOLUME volume = Marshal.PtrToStructure <DEV_BROADCAST_VOLUME>(message.LParam);
                        string deviceName           = DeviceMaskToDeviceName(volume.DBVC_UnitMask);

                        DeviceArrived?.Invoke(this, new DeviceNotificationEventArgs(deviceName));
                        RegisterDeviceNotification(deviceName);
                    }
                    break;

                case DBT_DEVICEQUERYREMOVE:
                    if (Marshal.ReadInt32(message.LParam, 4) == DBT_DEVTYP_HANDLE)
                    {
                        DEV_BROADCAST_HANDLE data = Marshal.PtrToStructure <DEV_BROADCAST_HANDLE>(message.LParam);
                        if (DeviceHandles.ContainsKey(data.DBCH_HDevNotify))
                        {
                            DeviceQueryRemove?.Invoke(this, new DeviceNotificationEventArgs(DeviceHandles[data.DBCH_HDevNotify]));
                        }

                        UnregisterDeviceNotification(data.DBCH_HDevNotify, data.DBCH_Handle);
                    }
                    break;

                case DBT_DEVICEREMOVECOMPLETE:
                    if (Marshal.ReadInt32(message.LParam, 4) == DBT_DEVTYP_VOLUME)
                    {
                        DEV_BROADCAST_VOLUME volume = Marshal.PtrToStructure <DEV_BROADCAST_VOLUME>(message.LParam);
                        string deviceName           = DeviceMaskToDeviceName(volume.DBVC_UnitMask);

                        DeviceRemoveComplete?.Invoke(this, new DeviceNotificationEventArgs(deviceName));
                    }
                    break;
                }
            }
        }