最終更新:2017-10-17 (火) 14:57:09 (2380d)  

RegisterDeviceNotification
Top / RegisterDeviceNotification

通知を受け取るデバイスまたはデバイスタイプを指定できるようにします。

https://msdn.microsoft.com/ja-jp/library/cc429173.aspx

https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa363431(v=vs.85).aspx

  • HDEVNOTIFY RegisterDeviceNotification(
      HANDLE hRecipient,
      LPVOID NotificationFilter,
      DWORD Flags
    );

パラメータ

hRecipient

  • NotificationFilter? パラメータで指定されたデバイスに関係するデバイスイベントを受け取るウィンドウのハンドルを指定します。複数の RegisterDeviceNotification 関数の呼び出しで同じウィンドウハンドルを利用できます。 サービスではウィンドウハンドルかサービス状態ハンドルのいずれかを指定できます。

NotificationFilter?

  • 通知の送信先となるデバイスタイプを指定するデータブロックへのポインタを指定します。このブロックの先頭には、必ず 構造体が置かれます。このヘッダーの後に続くデータは、dbch_devicetype メンバの値に依存します。
  • A pointer to a block of data that specifies the type of device for which notifications should be sent.
  • This block always begins with the DEV_BROADCAST_HDR structure.
    • The data following this header is dependent on the value of the dbch_devicetype member, which can be DBT_DEVTYP_DEVICEINTERFACE or DBT_DEVTYP_HANDLE?.

DEV_BROADCAST_HDR.dbch_devicetype

  • 名前Meaning
    DBT_DEVTYP_DEVICEINTERFACE0x00000005Class of devices. This structure is a DEV_BROADCAST_DEVICEINTERFACE structure.
    DBT_DEVTYP_HANDLE?0x00000006File system handle. This structure is a DEV_BROADCAST_HANDLE? structure.
    DBT_DEVTYP_OEM?0x00000000OEM- or IHV-defined device type. This structure is a DEV_BROADCAST_OEM? structure.
    DBT_DEVTYP_PORT?0x00000003Port device (serial or parallel). This structure is a DEV_BROADCAST_PORT? structure.
    DBT_DEVTYP_VOLUME?0x00000002Logical volume. This structure is a DEV_BROADCAST_VOLUME? structure.

Flags

C♯

  • Win32Wrapper.DEV_BROADCAST_DEVICEINTERFACE deviceInterface = new Win32Wrapper.DEV_BROADCAST_DEVICEINTERFACE();
    int size = Marshal.SizeOf(deviceInterface);
    deviceInterface.dbcc_size = size;
    deviceInterface.dbcc_devicetype = (Int32)Win32Wrapper.DBTDEVTYP.DBT_DEVTYP_DEVICEINTERFACE;
    IntPtr buffer = default(IntPtr);
    buffer = Marshal.AllocHGlobal(size);
    Marshal.StructureToPtr(deviceInterface, buffer, true);
    deviceEventHandle = Win32Wrapper.RegisterDeviceNotification(WindowsHandle, buffer, (int)(Win32Wrapper.DEVICE_NOTIFY.DEVICE_NOTIFY_WINDOW_HANDLE | Win32Wrapper.DEVICE_NOTIFY.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES));
    Status = (deviceEventHandle != IntPtr.Zero);
    if (!Status)
    {
        LastError = Marshal.GetLastWin32Error();
    }
    Marshal.FreeHGlobal(buffer);

C♯RegisterDeviceNotification

Remarks

  • The DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE? events are automatically broadcast to all top-level windows for port devices.
  • Therefore, it is not necessary to call RegisterDeviceNotification for ports, and the function fails if the dbch_devicetype member is DBT_DEVTYP_PORT?.
  • Volume notifications are also broadcast to top-level windows, so the function fails if dbch_devicetype is DBT_DEVTYP_VOLUME?.
  • OEM-defined devices are not used directly by the system, so the function fails if dbch_devicetype is DBT_DEVTYP_OEM?.

DLL

ウィンドウプロシージャ

関連

  • UnregisterDeviceNotification?
  • BroadcastSystemMessage?

参考