RegisterDeviceNotificationA
関数シグネチャ
// USER32.dll (ANSI / -A)
#include <windows.h>
HDEVNOTIFY RegisterDeviceNotificationA(
HANDLE hRecipient,
void* NotificationFilter,
REGISTER_NOTIFICATION_FLAGS Flags
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hRecipient | HANDLE | in | NotificationFilter パラメーターで指定されたデバイスのデバイスイベントを受け取る、ウィンドウまたはサービスへのハンドルです。同じウィンドウハンドルを RegisterDeviceNotification の複数回の呼び出しで使用できます。 サービスは、ウィンドウハンドルまたはサービスステータスハンドルのいずれかを指定できます。 | ||||||||||
| NotificationFilter | void* | in | 通知を送信するデバイスの種類を指定するデータブロックへのポインターです。このブロックは常に DEV_BROADCAST_HDR 構造体で始まります。このヘッダーに続くデータは、dbch_devicetype メンバーの値に依存し、DBT_DEVTYP_DEVICEINTERFACE または DBT_DEVTYP_HANDLE を指定できます。詳細については、「解説」を参照してください。 | ||||||||||
| Flags | REGISTER_NOTIFICATION_FLAGS | in | このパラメーターには、次のいずれかの値を指定できます。
さらに、次の値を指定できます。
|
戻り値の型: HDEVNOTIFY
公式ドキュメント
ウィンドウが通知を受け取るデバイス、またはデバイスの種類を登録します。(ANSI)
戻り値
関数が成功した場合、戻り値はデバイス通知ハンドルです。
関数が失敗した場合、戻り値は NULL です。拡張エラー情報を取得するには、GetLastError を呼び出します。
解説(Remarks)
アプリケーションは、BroadcastSystemMessage 関数を使用してイベント通知を送信します。トップレベルウィンドウを持つアプリケーションは、WM_DEVICECHANGE メッセージを処理することで、基本的な通知を受け取ることができます。アプリケーションは、RegisterDeviceNotification 関数を使用して、デバイス通知を受け取るための登録を行うことができます。
サービスは、RegisterDeviceNotification 関数を使用して、デバイス通知を受け取るための登録を行うことができます。サービスが hRecipient パラメーターにウィンドウハンドルを指定した場合、通知はウィンドウプロシージャに送信されます。hRecipient がサービスステータスハンドルの場合、SERVICE_CONTROL_DEVICEEVENT 通知がサービス制御ハンドラーに送信されます。サービス制御ハンドラーの詳細については、HandlerEx を参照してください。
プラグアンドプレイのデバイスイベントは、できるだけ速やかに処理してください。そうしないと、システムが応答しなくなる場合があります。イベントハンドラーで実行を妨げる可能性のある操作(I/O など)を行う場合は、別のスレッドを開始して操作を非同期に実行するのが最善です。
RegisterDeviceNotification によって返されたデバイス通知ハンドルは、不要になったときに UnregisterDeviceNotification 関数を呼び出して閉じる必要があります。
DBT_DEVICEARRIVAL および DBT_DEVICEREMOVECOMPLETE イベントは、ポートデバイスについてはすべてのトップレベルウィンドウに自動的にブロードキャストされます。したがって、ポートに対して RegisterDeviceNotification を呼び出す必要はなく、dbch_devicetype メンバーが DBT_DEVTYP_PORT の場合、この関数は失敗します。ボリューム通知もトップレベルウィンドウにブロードキャストされるため、dbch_devicetype が DBT_DEVTYP_VOLUME の場合、この関数は失敗します。OEM 定義デバイスはシステムによって直接使用されないため、dbch_devicetype が DBT_DEVTYP_OEM の場合、この関数は失敗します。
例
例については、デバイス通知の登録を参照してください。
winuser.h ヘッダーは、RegisterDeviceNotification を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディングニュートラルなエイリアスの使用を、エンコーディングニュートラルでないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不一致が生じる可能性があります。詳細については、関数プロトタイプの規約を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// USER32.dll (ANSI / -A)
#include <windows.h>
HDEVNOTIFY RegisterDeviceNotificationA(
HANDLE hRecipient,
void* NotificationFilter,
REGISTER_NOTIFICATION_FLAGS Flags
);[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr RegisterDeviceNotificationA(
IntPtr hRecipient, // HANDLE
IntPtr NotificationFilter, // void*
uint Flags // REGISTER_NOTIFICATION_FLAGS
);<DllImport("USER32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function RegisterDeviceNotificationA(
hRecipient As IntPtr, ' HANDLE
NotificationFilter As IntPtr, ' void*
Flags As UInteger ' REGISTER_NOTIFICATION_FLAGS
) As IntPtr
End Function' hRecipient : HANDLE
' NotificationFilter : void*
' Flags : REGISTER_NOTIFICATION_FLAGS
Declare PtrSafe Function RegisterDeviceNotificationA Lib "user32" ( _
ByVal hRecipient As LongPtr, _
ByVal NotificationFilter As LongPtr, _
ByVal Flags As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RegisterDeviceNotificationA = ctypes.windll.user32.RegisterDeviceNotificationA
RegisterDeviceNotificationA.restype = ctypes.c_void_p
RegisterDeviceNotificationA.argtypes = [
wintypes.HANDLE, # hRecipient : HANDLE
ctypes.POINTER(None), # NotificationFilter : void*
wintypes.DWORD, # Flags : REGISTER_NOTIFICATION_FLAGS
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
RegisterDeviceNotificationA = Fiddle::Function.new(
lib['RegisterDeviceNotificationA'],
[
Fiddle::TYPE_VOIDP, # hRecipient : HANDLE
Fiddle::TYPE_VOIDP, # NotificationFilter : void*
-Fiddle::TYPE_INT, # Flags : REGISTER_NOTIFICATION_FLAGS
],
Fiddle::TYPE_VOIDP)#[link(name = "user32")]
extern "system" {
fn RegisterDeviceNotificationA(
hRecipient: *mut core::ffi::c_void, // HANDLE
NotificationFilter: *mut (), // void*
Flags: u32 // REGISTER_NOTIFICATION_FLAGS
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr RegisterDeviceNotificationA(IntPtr hRecipient, IntPtr NotificationFilter, uint Flags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_RegisterDeviceNotificationA' -Namespace Win32 -PassThru
# $api::RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags)#uselib "USER32.dll"
#func global RegisterDeviceNotificationA "RegisterDeviceNotificationA" sptr, sptr, sptr
; RegisterDeviceNotificationA hRecipient, NotificationFilter, Flags ; 戻り値は stat
; hRecipient : HANDLE -> "sptr"
; NotificationFilter : void* -> "sptr"
; Flags : REGISTER_NOTIFICATION_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "USER32.dll"
#cfunc global RegisterDeviceNotificationA "RegisterDeviceNotificationA" sptr, sptr, int
; res = RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags)
; hRecipient : HANDLE -> "sptr"
; NotificationFilter : void* -> "sptr"
; Flags : REGISTER_NOTIFICATION_FLAGS -> "int"; HDEVNOTIFY RegisterDeviceNotificationA(HANDLE hRecipient, void* NotificationFilter, REGISTER_NOTIFICATION_FLAGS Flags)
#uselib "USER32.dll"
#cfunc global RegisterDeviceNotificationA "RegisterDeviceNotificationA" intptr, intptr, int
; res = RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags)
; hRecipient : HANDLE -> "intptr"
; NotificationFilter : void* -> "intptr"
; Flags : REGISTER_NOTIFICATION_FLAGS -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procRegisterDeviceNotificationA = user32.NewProc("RegisterDeviceNotificationA")
)
// hRecipient (HANDLE), NotificationFilter (void*), Flags (REGISTER_NOTIFICATION_FLAGS)
r1, _, err := procRegisterDeviceNotificationA.Call(
uintptr(hRecipient),
uintptr(NotificationFilter),
uintptr(Flags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HDEVNOTIFYfunction RegisterDeviceNotificationA(
hRecipient: THandle; // HANDLE
NotificationFilter: Pointer; // void*
Flags: DWORD // REGISTER_NOTIFICATION_FLAGS
): THandle; stdcall;
external 'USER32.dll' name 'RegisterDeviceNotificationA';result := DllCall("USER32\RegisterDeviceNotificationA"
, "Ptr", hRecipient ; HANDLE
, "Ptr", NotificationFilter ; void*
, "UInt", Flags ; REGISTER_NOTIFICATION_FLAGS
, "Ptr") ; return: HDEVNOTIFY●RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags) = DLL("USER32.dll", "void* RegisterDeviceNotificationA(void*, void*, dword)")
# 呼び出し: RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags)
# hRecipient : HANDLE -> "void*"
# NotificationFilter : void* -> "void*"
# Flags : REGISTER_NOTIFICATION_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "user32" fn RegisterDeviceNotificationA(
hRecipient: ?*anyopaque, // HANDLE
NotificationFilter: ?*anyopaque, // void*
Flags: u32 // REGISTER_NOTIFICATION_FLAGS
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc RegisterDeviceNotificationA(
hRecipient: pointer, # HANDLE
NotificationFilter: pointer, # void*
Flags: uint32 # REGISTER_NOTIFICATION_FLAGS
): pointer {.importc: "RegisterDeviceNotificationA", stdcall, dynlib: "USER32.dll".}pragma(lib, "user32");
extern(Windows)
void* RegisterDeviceNotificationA(
void* hRecipient, // HANDLE
void* NotificationFilter, // void*
uint Flags // REGISTER_NOTIFICATION_FLAGS
);ccall((:RegisterDeviceNotificationA, "USER32.dll"), stdcall, Ptr{Cvoid},
(Ptr{Cvoid}, Ptr{Cvoid}, UInt32),
hRecipient, NotificationFilter, Flags)
# hRecipient : HANDLE -> Ptr{Cvoid}
# NotificationFilter : void* -> Ptr{Cvoid}
# Flags : REGISTER_NOTIFICATION_FLAGS -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* RegisterDeviceNotificationA(
void* hRecipient,
void* NotificationFilter,
uint32_t Flags);
]]
local user32 = ffi.load("user32")
-- user32.RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags)
-- hRecipient : HANDLE
-- NotificationFilter : void*
-- Flags : REGISTER_NOTIFICATION_FLAGS
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const RegisterDeviceNotificationA = lib.func('__stdcall', 'RegisterDeviceNotificationA', 'void *', ['void *', 'void *', 'uint32_t']);
// RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags)
// hRecipient : HANDLE -> 'void *'
// NotificationFilter : void* -> 'void *'
// Flags : REGISTER_NOTIFICATION_FLAGS -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
RegisterDeviceNotificationA: { parameters: ["pointer", "pointer", "u32"], result: "pointer" },
});
// lib.symbols.RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags)
// hRecipient : HANDLE -> "pointer"
// NotificationFilter : void* -> "pointer"
// Flags : REGISTER_NOTIFICATION_FLAGS -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* RegisterDeviceNotificationA(
void* hRecipient,
void* NotificationFilter,
uint32_t Flags);
C, "USER32.dll");
// $ffi->RegisterDeviceNotificationA(hRecipient, NotificationFilter, Flags);
// hRecipient : HANDLE
// NotificationFilter : void*
// Flags : REGISTER_NOTIFICATION_FLAGS
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface User32 extends StdCallLibrary {
User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.ASCII_OPTIONS);
Pointer RegisterDeviceNotificationA(
Pointer hRecipient, // HANDLE
Pointer NotificationFilter, // void*
int Flags // REGISTER_NOTIFICATION_FLAGS
);
}@[Link("user32")]
lib LibUSER32
fun RegisterDeviceNotificationA = RegisterDeviceNotificationA(
hRecipient : Void*, # HANDLE
NotificationFilter : Void*, # void*
Flags : UInt32 # REGISTER_NOTIFICATION_FLAGS
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef RegisterDeviceNotificationANative = Pointer<Void> Function(Pointer<Void>, Pointer<Void>, Uint32);
typedef RegisterDeviceNotificationADart = Pointer<Void> Function(Pointer<Void>, Pointer<Void>, int);
final RegisterDeviceNotificationA = DynamicLibrary.open('USER32.dll')
.lookupFunction<RegisterDeviceNotificationANative, RegisterDeviceNotificationADart>('RegisterDeviceNotificationA');
// hRecipient : HANDLE -> Pointer<Void>
// NotificationFilter : void* -> Pointer<Void>
// Flags : REGISTER_NOTIFICATION_FLAGS -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegisterDeviceNotificationA(
hRecipient: THandle; // HANDLE
NotificationFilter: Pointer; // void*
Flags: DWORD // REGISTER_NOTIFICATION_FLAGS
): THandle; stdcall;
external 'USER32.dll' name 'RegisterDeviceNotificationA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegisterDeviceNotificationA"
c_RegisterDeviceNotificationA :: Ptr () -> Ptr () -> Word32 -> IO (Ptr ())
-- hRecipient : HANDLE -> Ptr ()
-- NotificationFilter : void* -> Ptr ()
-- Flags : REGISTER_NOTIFICATION_FLAGS -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let registerdevicenotificationa =
foreign "RegisterDeviceNotificationA"
((ptr void) @-> (ptr void) @-> uint32_t @-> returning (ptr void))
(* hRecipient : HANDLE -> (ptr void) *)
(* NotificationFilter : void* -> (ptr void) *)
(* Flags : REGISTER_NOTIFICATION_FLAGS -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("RegisterDeviceNotificationA" register-device-notification-a :convention :stdcall) :pointer
(h-recipient :pointer) ; HANDLE
(notification-filter :pointer) ; void*
(flags :uint32)) ; REGISTER_NOTIFICATION_FLAGS
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegisterDeviceNotificationA = Win32::API::More->new('USER32',
'HANDLE RegisterDeviceNotificationA(HANDLE hRecipient, LPVOID NotificationFilter, DWORD Flags)');
# my $ret = $RegisterDeviceNotificationA->Call($hRecipient, $NotificationFilter, $Flags);
# hRecipient : HANDLE -> HANDLE
# NotificationFilter : void* -> LPVOID
# Flags : REGISTER_NOTIFICATION_FLAGS -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f RegisterDeviceNotificationW (Unicode版) — デバイス変更通知の受信を登録する(Unicode)。
- s DEV_BROADCAST_HDR
- c LPHANDLER_FUNCTION_EX
- f UnregisterDeviceNotification — デバイス変更通知の登録を解除する。