Exemplo n.º 1
0
        /// <summary>
        /// New version which gets the handle automatically for specified directory
        /// Only for registering! Unregister with the old version of this function...
        /// </summary>
        /// <param name="register"></param>
        /// <param name="dirPath">e.g. C:\\dir</param>
        private void RegisterForDeviceChange(string dirPath)
        {
            IntPtr handle = NativeMethods.OpenDirectory(dirPath);

            if (handle == IntPtr.Zero)
            {
                this.deviceNotifyHandle = IntPtr.Zero;
                return;
            }
            else
            {
                this.dirHandle = handle;    // save handle for closing it when unregistering
            }

            // Register for handle
            DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE {
                dbch_devicetype = DBT_DEVTYP_HANDLE,
                dbch_reserved   = 0,
                dbch_nameoffset = 0,
                dbch_handle     = handle,
                dbch_hdevnotify = (IntPtr)0,
            };
            int size = Marshal.SizeOf(data);

            data.dbch_size = size;
            IntPtr buffer = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(data, buffer, true);

            this.deviceNotifyHandle = NativeMethods.RegisterDeviceNotification(this.recipientHandle, buffer, 0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers to be notified when the volume is about to be removed
        /// This is requierd if you want to get the QUERY REMOVE messages
        /// </summary>
        /// <param name="register">true to register, false to unregister</param>
        /// <param name="fileHandle">handle of a file opened on the removable drive</param>
        private void RegisterForDeviceChange(bool register, SafeFileHandle fileHandle)
        {
            if (register)
            {
                // Register for handle
                DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE {
                    dbch_devicetype = DBT_DEVTYP_HANDLE,
                    dbch_reserved   = 0,
                    dbch_nameoffset = 0,
                    dbch_handle     = fileHandle.DangerousGetHandle(),
                    dbch_hdevnotify = (IntPtr)0,
                };
                int size = Marshal.SizeOf(data);
                data.dbch_size = size;
                IntPtr buffer = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(data, buffer, true);

                this.deviceNotifyHandle = NativeMethods.RegisterDeviceNotification(this.recipientHandle, buffer, 0);
            }
            else
            {
                // close the directory handle
                if (this.dirHandle != IntPtr.Zero)
                {
                    NativeMethods.CloseDirectoryHandle(this.dirHandle);
                }

                // unregister
                if (this.deviceNotifyHandle != IntPtr.Zero)
                {
                    _ = NativeMethods.UnregisterDeviceNotification(this.deviceNotifyHandle);
                }

                this.deviceNotifyHandle = IntPtr.Zero;
                this.dirHandle          = IntPtr.Zero;

                this.currentDrive = string.Empty;
                this.fileOnFlash?.Close();
                this.fileOnFlash = null;
            }
        }