Пример #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="dirPath">e.g. C:\\dir</param>
        private void RegisterForDeviceChange(string dirPath)
        {
            var handle = Native.OpenDirectory(dirPath);

            if (handle == IntPtr.Zero)
            {
                _deviceNotifyHandle = IntPtr.Zero;
                return;
            }

            _directoryHandle = handle; // save handle for closing it when unregistering

            // Register for handle
            var data = new DEV_BROADCAST_HANDLE
            {
                dbch_devicetype = DBT_DEVTYP_HANDLE,
                dbch_reserved   = 0,
                dbch_nameoffset = 0,
                dbch_handle     = handle,
                dbch_hdevnotify = (IntPtr)0
            };

            //data.dbch_data = null;
            //data.dbch_eventguid = 0;

            var size = Marshal.SizeOf(data);

            data.dbch_size = size;

            var buffer = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(data, buffer, true);

            _deviceNotifyHandle = Native.RegisterDeviceNotification(_recipientHandle, buffer, 0);
        }
Пример #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
                var data = new DEV_BROADCAST_HANDLE
                {
                    dbch_devicetype = DBT_DEVTYP_HANDLE,
                    dbch_reserved   = 0,
                    dbch_nameoffset = 0,
                    dbch_handle     = fileHandle.DangerousGetHandle(),
                    dbch_hdevnotify = (IntPtr)0
                };
                //data.dbch_data = null;
                //data.dbch_eventguid = 0;
                //Marshal. fileHandle;
                var size = Marshal.SizeOf(data);

                data.dbch_size = size;

                var buffer = Marshal.AllocHGlobal(size);

                Marshal.StructureToPtr(data, buffer, true);

                _deviceNotifyHandle = Native.RegisterDeviceNotification(_recipientHandle, buffer, 0);
            }
            else
            {
                // close the directory handle
                if (_directoryHandle != IntPtr.Zero)
                {
                    Native.CloseDirectoryHandle(_directoryHandle);
                    //    string er = Marshal.GetLastWin32Error().ToString();
                }

                // unregister
                if (_deviceNotifyHandle != IntPtr.Zero)
                {
                    Native.UnregisterDeviceNotification(_deviceNotifyHandle);
                }

                _deviceNotifyHandle = IntPtr.Zero;

                _directoryHandle = IntPtr.Zero;

                HookedDrive = "";

                if (OpenedFile == null)
                {
                    return;
                }

                OpenedFile.Close();

                OpenedFile = null;
            }
        }