Example #1
0
        private void Lock()
        {
            var file = API.CreateFile(string.Format(@"\\.\{0}:", Path[0]),
                                      API.FileAccessRights.GENERIC_READ,
                                      FileShare.ReadWrite,
                                      IntPtr.Zero,
                                      FileMode.Open,
                                      API.CreateFileOptions.FILE_OPTIONS_NOT_SET,
                                      IntPtr.Zero);

            if (file == IntPtr.Zero || file == InvalidHandle)
            {
                Log.Raw("Cannot CreateFile " + Path);
                return; //might be real fixed drive
            }

            _hDrive = file;

            var msg = new API.DEV_BROADCAST_HANDLE {
                dbch_handle = file
            };

            _hNotification = API.RegisterDeviceNotification(_reporter, msg, API.RDNFlags.DEVICE_NOTIFY_WINDOW_HANDLE);

            if (_hNotification == IntPtr.Zero || _hNotification == InvalidHandle)
            {
                Log.Raw("Cannot lock " + Path);
                Unlock(); //this drive appears to be non-notifiable
            }
            else
            {
                Log.Raw("Locked " + Path);
            }
        }
Example #2
0
        private bool Process(API.DeviceChangeMessages wParam, IntPtr lParam)
        {
            var o = new API.DEV_BROADCAST_HANDLE();

            if (lParam != IntPtr.Zero)
            {
                Marshal.PtrToStructure(lParam, o);
            }

            if (o.dbch_hdevnotify != _hNotification)
            {
                return(false);
            }

            Log.Fmt("Device event {0}, custom event guid {1}", wParam, o.dbch_eventguid);

            switch (wParam)
            {
            case API.DeviceChangeMessages.DBT_DEVICEQUERYREMOVE:
                EnableRaisingEvents = false;
                break;

            case API.DeviceChangeMessages.DBT_DEVICEQUERYREMOVEFAILED:
                EnableRaisingEvents = true;
                break;

            case API.DeviceChangeMessages.DBT_CUSTOMEVENT:
                if (API.DevEvent.Queryable.Contains(o.dbch_eventguid))
                {
                    EnableRaisingEvents = false;
                }
                else if (API.DevEvent.Failed.Contains(o.dbch_eventguid))
                {
                    EnableRaisingEvents = true;
                }
                break;
            }
            return(true);
        }