コード例 #1
0
        private void RegisterDeviceNotification()
        {
            _debugger.Trace();

            // Creating dbh-structure, that will contain all necessary information,
            // that required by system in order to register notification.
            var deviceHandle = new DeviceBroadcastHandle();

            deviceHandle.Size       = Marshal.SizeOf(deviceHandle);
            deviceHandle.DeviceType = (int)DeviceType.DBT_DEVTYP_HANDLE;

            // Creating handle to an empty file. Handle will be placed in dbh-structure and this instance.
            Handle = deviceHandle.DeviceHandle = NativeMethods.CreateFile(DriveInfo.Name, (uint)FileDesiredAccess.GENERIC_READ, (uint)FileShare.ReadWrite,
                                                                          0, (uint)FileCreationDisposition.OPEN_EXISTING, (uint)FileFlags.BACKUP_SEMANTICS | (uint)Native.FileAttributes.NORMAL, 0);

            // Allocating memory and converting dbh-structure to IntPtr.
            var data = Marshal.AllocHGlobal(deviceHandle.Size);

            Marshal.StructureToPtr(deviceHandle, data, true);

            // Registering device notification and catching win32-exception
            _deviceNotificationHandle = NativeMethods.RegisterDeviceNotification(_deviceManager.Handle, data, (uint)DeviceNotifyFlags.WINDOW_HANDLE);
            var innerException = new Win32Exception(Marshal.GetLastWin32Error());

            // Rethrowing exception, if something went wrong
            if (_deviceNotificationHandle == IntPtr.Zero || Handle == IntPtr.Zero)
            {
                throw new Exception("Failed to register device notification handle.", innerException);
            }
        }
コード例 #2
0
        internal void RegisterDeviceRemoval(IntPtr deviceHandle)
        {
            DeviceBroadcastHandle dbh = new DeviceBroadcastHandle();

            dbh.Size       = Marshal.SizeOf(dbh);
            dbh.DeviceType = 0x6;
            dbh.Handle     = deviceHandle;

            _deviceHandle        = deviceHandle;
            _handleDeviceRemoval = RegisterDeviceNotification(Handle, ref dbh, 0);

            if (_handleDeviceRemoval == IntPtr.Zero)
            {
                throw new Exception(string.Format("Failed in call to RegisterDeviceNotification ({0})", GetLastError()));
            }
        }
コード例 #3
0
        internal void RegisterDeviceRemoval(IntPtr deviceHandle)
        {
            DeviceBroadcastHandle dbh = new DeviceBroadcastHandle();

            dbh.Size       = Marshal.SizeOf(dbh);
            dbh.DeviceType = 0x6;
            dbh.Handle     = deviceHandle;

            _deviceHandle        = deviceHandle;
            _handleDeviceRemoval = RegisterDeviceNotification(Handle, ref dbh, 0);
            int lastError = Marshal.GetLastWin32Error();

            if (_handleDeviceRemoval == IntPtr.Zero)
            {
                throw new Win32Exception(lastError);
            }
        }
コード例 #4
0
        private void OnDeviceRemoval(DeviceBroadcastHeader header, IntPtr ptr)
        {
            if (header.DeviceType == 0x06)
            {
                DeviceBroadcastHandle dbh = (DeviceBroadcastHandle)Marshal.PtrToStructure(ptr, typeof(DeviceBroadcastHandle));

                if (dbh.Handle != _deviceHandle.DangerousGetHandle())
                {
                    return;
                }

                CancelIo(_deviceHandle);
                UnregisterDeviceRemoval();

                if (DeviceRemoval != null)
                {
                    DeviceRemoval(this, EventArgs.Empty);
                }
            }
        }
コード例 #5
0
        internal void RegisterDeviceRemoval(SafeHandle deviceHandle)
        {
            DeviceBroadcastHandle dbh = new DeviceBroadcastHandle();

            dbh.Size       = Marshal.SizeOf(dbh);
            dbh.DeviceType = 0x6;
            dbh.Handle     = deviceHandle.DangerousGetHandle();

            _deviceHandle = deviceHandle;
            try
            {
                _handleDeviceRemoval = new SafeFileHandle(RegisterDeviceNotification(Handle, ref dbh, 0), true);
            }
            catch
            {
                MceRemoteReceiver.LogInfo("DeviceWatcher.RegisterDeviceRemoval: Error={0}.", Marshal.GetLastWin32Error());
            }

            if (_handleDeviceRemoval.IsInvalid)
            {
                throw new Exception(string.Format("Failed in call to RegisterDeviceNotification ({0})", GetLastError()));
            }
        }
コード例 #6
0
ファイル: DeviceWatcher.cs プロジェクト: chekiI/MediaPortal-2
 private static extern IntPtr RegisterDeviceNotification(IntPtr handle, ref DeviceBroadcastHandle filter, int flags);
コード例 #7
0
ファイル: DeviceWatcher.cs プロジェクト: chekiI/MediaPortal-2
    internal void RegisterDeviceRemoval(SafeHandle deviceHandle)
    {
      DeviceBroadcastHandle dbh = new DeviceBroadcastHandle();

      dbh.Size = Marshal.SizeOf(dbh);
      dbh.DeviceType = 0x6;
      dbh.Handle = deviceHandle.DangerousGetHandle();

      _deviceHandle = deviceHandle;
      try
      {
        _handleDeviceRemoval = new SafeFileHandle(RegisterDeviceNotification(Handle, ref dbh, 0), true);
      }
      catch
      {
        MceRemoteReceiver.LogInfo("DeviceWatcher.RegisterDeviceRemoval: Error={0}.", Marshal.GetLastWin32Error());
      }

      if (_handleDeviceRemoval.IsInvalid)
      {
        throw new Exception(string.Format("Failed in call to RegisterDeviceNotification ({0})", GetLastError()));
      }
    }
コード例 #8
0
 private static extern IntPtr RegisterDeviceNotification(IntPtr handle, ref DeviceBroadcastHandle filter, int flags);
コード例 #9
0
    internal void RegisterDeviceRemoval(IntPtr deviceHandle)
    {
      DeviceBroadcastHandle dbh = new DeviceBroadcastHandle();

      dbh.Size = Marshal.SizeOf(dbh);
      dbh.DeviceType = 0x6;
      dbh.Handle = deviceHandle;

      _deviceHandle = deviceHandle;
      _handleDeviceRemoval = RegisterDeviceNotification(Handle, ref dbh, 0);
      int lastError = Marshal.GetLastWin32Error();
      if (_handleDeviceRemoval == IntPtr.Zero)
        throw new Win32Exception(lastError);
    }
コード例 #10
0
    internal void RegisterDeviceRemoval(IntPtr deviceHandle)
    {
      DeviceBroadcastHandle dbh = new DeviceBroadcastHandle();

      dbh.Size = Marshal.SizeOf(dbh);
      dbh.DeviceType = 0x6;
      dbh.Handle = deviceHandle;

      _deviceHandle = deviceHandle;
      _handleDeviceRemoval = RegisterDeviceNotification(Handle, ref dbh, 0);

      if (_handleDeviceRemoval == IntPtr.Zero)
        throw new Exception(string.Format("Failed in call to RegisterDeviceNotification ({0})", GetLastError()));
    }