Win32 API 日本語リファレンス
ホームDevices.DeviceAndDriverInstallation › CM_Register_Notification

CM_Register_Notification

関数
デバイスやインターフェイスの変更通知を登録する。
DLLCFGMGR32.dll呼出規約winapi対応OSwindows8.0

シグネチャ

// CFGMGR32.dll
#include <windows.h>

CONFIGRET CM_Register_Notification(
    CM_NOTIFY_FILTER* pFilter,
    void* pContext,   // optional
    PCM_NOTIFY_CALLBACK pCallback,
    HCMNOTIFICATION* pNotifyContext
);

パラメーター

名前方向説明
pFilterCM_NOTIFY_FILTER*inCM_NOTIFY_FILTER 構造体へのポインター。
pContextvoid*inoptionalpCallback のコールバックルーチンに渡されるコンテキストを格納した、呼び出し元が割り当てたバッファーへのポインター。
pCallbackPCM_NOTIFY_CALLBACKin

指定した PnP イベントが発生したときに呼び出されるルーチンへのポインター。コールバック関数のプロトタイプについては、「解説」セクションを参照してください。

コールバックルーチンの Action パラメーターには、CM_NOTIFY_ACTION 列挙型の値が渡されます。

通知を受け取ったとき、コールバックが通知をどのように調べるかは、コールバックルーチンの EventData パラメーターの FilterType メンバーによって異なります。

CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE

コールバックは EventData->u.DeviceInterface を調べます。

CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE

コールバックは EventData->u.DeviceHandle を調べます。

CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE

コールバックは EventData->u.DeviceInstance を調べます。

pNotifyContextHCMNOTIFICATION*outこの登録呼び出しに対応する HCMNOTIFICATION ハンドルを受け取るポインター。

戻り値の型: CONFIGRET

公式ドキュメント

CM_Register_Notification 関数は、指定した種類の PnP イベントが発生したときに呼び出されるアプリケーションのコールバックルーチンを登録します。

戻り値

操作が成功した場合、この関数は CR_SUCCESS を返します。それ以外の場合は、Cfgmgr32.h で定義されている CR_ プレフィックス付きのエラーコードのいずれかを返します。

解説(Remarks)

プラグ アンド プレイのデバイスイベントは、できる限り速やかに処理してください。イベントハンドラーで実行をブロックする可能性のある処理 (I/O など) を行う場合は、別のスレッドを開始してその処理を非同期に実行することをお勧めします。

CM_Register_Notification 関数は、既存のデバイスインターフェイスについては通知しません。既存のインターフェイスを取得するには、まず CM_Register_Notification を呼び出し、その後 CM_Get_Device_Interface_List を呼び出します。ドライバーが CM_Register_Notification を呼び出した後、CM_Get_Device_Interface_List を呼び出す前にインターフェイスが有効になった場合、ドライバーはそのインターフェイスの到着通知を受け取り、さらにそのインターフェイスは CM_Get_Device_Interface_List が返すデバイスインターフェイスインスタンスの一覧にも現れます。

CM_Register_Notification が返す HCMNOTIFICATION ハンドルは、不要になった時点で CM_Unregister_Notification 関数を呼び出して閉じる必要があります。

コールバックルーチンは次の関数プロトタイプを使用します。

typedef __callback DWORD (CALLBACK *PCM_NOTIFY_CALLBACK)(
    _In_ HCMNOTIFICATION       hNotify,
    _In_opt_ PVOID             Context,
    _In_ CM_NOTIFY_ACTION      Action,
    _In_reads_bytes_(EventDataSize) PCM_NOTIFY_EVENT_DATA EventData,
    _In_ DWORD                 EventDataSize
    );

CM_NOTIFY_ACTION_DEVICEQUERYREMOVE 通知に応答する場合、PCM_NOTIFY_CALLBACK コールバックは状況に応じて ERROR_SUCCESS または ERROR_CANCELLED のいずれかを返す必要があります。それ以外の場合、コールバックは ERROR_SUCCESS を返す必要があります。コールバックはこれら以外の値を返してはなりません。その他のアクションの説明については、CM_NOTIFY_ACTION のドキュメントを参照してください。また、このコールバックが EventData パラメーターで受け取る構造体については、CM_NOTIFY_EVENT_DATA を参照してください。

例については、デバイスインターフェイスの到着とデバイスの削除の通知の登録を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// CFGMGR32.dll
#include <windows.h>

CONFIGRET CM_Register_Notification(
    CM_NOTIFY_FILTER* pFilter,
    void* pContext,   // optional
    PCM_NOTIFY_CALLBACK pCallback,
    HCMNOTIFICATION* pNotifyContext
);
[DllImport("CFGMGR32.dll", ExactSpelling = true)]
static extern uint CM_Register_Notification(
    IntPtr pFilter,   // CM_NOTIFY_FILTER*
    IntPtr pContext,   // void* optional
    IntPtr pCallback,   // PCM_NOTIFY_CALLBACK
    IntPtr pNotifyContext   // HCMNOTIFICATION* out
);
<DllImport("CFGMGR32.dll", ExactSpelling:=True)>
Public Shared Function CM_Register_Notification(
    pFilter As IntPtr,   ' CM_NOTIFY_FILTER*
    pContext As IntPtr,   ' void* optional
    pCallback As IntPtr,   ' PCM_NOTIFY_CALLBACK
    pNotifyContext As IntPtr   ' HCMNOTIFICATION* out
) As UInteger
End Function
' pFilter : CM_NOTIFY_FILTER*
' pContext : void* optional
' pCallback : PCM_NOTIFY_CALLBACK
' pNotifyContext : HCMNOTIFICATION* out
Declare PtrSafe Function CM_Register_Notification Lib "cfgmgr32" ( _
    ByVal pFilter As LongPtr, _
    ByVal pContext As LongPtr, _
    ByVal pCallback As LongPtr, _
    ByVal pNotifyContext As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CM_Register_Notification = ctypes.windll.cfgmgr32.CM_Register_Notification
CM_Register_Notification.restype = wintypes.DWORD
CM_Register_Notification.argtypes = [
    ctypes.c_void_p,  # pFilter : CM_NOTIFY_FILTER*
    ctypes.POINTER(None),  # pContext : void* optional
    ctypes.c_void_p,  # pCallback : PCM_NOTIFY_CALLBACK
    ctypes.c_void_p,  # pNotifyContext : HCMNOTIFICATION* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CFGMGR32.dll')
CM_Register_Notification = Fiddle::Function.new(
  lib['CM_Register_Notification'],
  [
    Fiddle::TYPE_VOIDP,  # pFilter : CM_NOTIFY_FILTER*
    Fiddle::TYPE_VOIDP,  # pContext : void* optional
    Fiddle::TYPE_VOIDP,  # pCallback : PCM_NOTIFY_CALLBACK
    Fiddle::TYPE_VOIDP,  # pNotifyContext : HCMNOTIFICATION* out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "cfgmgr32")]
extern "system" {
    fn CM_Register_Notification(
        pFilter: *mut CM_NOTIFY_FILTER,  // CM_NOTIFY_FILTER*
        pContext: *mut (),  // void* optional
        pCallback: *const core::ffi::c_void,  // PCM_NOTIFY_CALLBACK
        pNotifyContext: *mut *mut core::ffi::c_void  // HCMNOTIFICATION* out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("CFGMGR32.dll")]
public static extern uint CM_Register_Notification(IntPtr pFilter, IntPtr pContext, IntPtr pCallback, IntPtr pNotifyContext);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CFGMGR32_CM_Register_Notification' -Namespace Win32 -PassThru
# $api::CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext)
#uselib "CFGMGR32.dll"
#func global CM_Register_Notification "CM_Register_Notification" sptr, sptr, sptr, sptr
; CM_Register_Notification varptr(pFilter), pContext, pCallback, pNotifyContext   ; 戻り値は stat
; pFilter : CM_NOTIFY_FILTER* -> "sptr"
; pContext : void* optional -> "sptr"
; pCallback : PCM_NOTIFY_CALLBACK -> "sptr"
; pNotifyContext : HCMNOTIFICATION* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CFGMGR32.dll"
#cfunc global CM_Register_Notification "CM_Register_Notification" var, sptr, sptr, sptr
; res = CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext)
; pFilter : CM_NOTIFY_FILTER* -> "var"
; pContext : void* optional -> "sptr"
; pCallback : PCM_NOTIFY_CALLBACK -> "sptr"
; pNotifyContext : HCMNOTIFICATION* out -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; CONFIGRET CM_Register_Notification(CM_NOTIFY_FILTER* pFilter, void* pContext, PCM_NOTIFY_CALLBACK pCallback, HCMNOTIFICATION* pNotifyContext)
#uselib "CFGMGR32.dll"
#cfunc global CM_Register_Notification "CM_Register_Notification" var, intptr, intptr, intptr
; res = CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext)
; pFilter : CM_NOTIFY_FILTER* -> "var"
; pContext : void* optional -> "intptr"
; pCallback : PCM_NOTIFY_CALLBACK -> "intptr"
; pNotifyContext : HCMNOTIFICATION* out -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	cfgmgr32 = windows.NewLazySystemDLL("CFGMGR32.dll")
	procCM_Register_Notification = cfgmgr32.NewProc("CM_Register_Notification")
)

// pFilter (CM_NOTIFY_FILTER*), pContext (void* optional), pCallback (PCM_NOTIFY_CALLBACK), pNotifyContext (HCMNOTIFICATION* out)
r1, _, err := procCM_Register_Notification.Call(
	uintptr(pFilter),
	uintptr(pContext),
	uintptr(pCallback),
	uintptr(pNotifyContext),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // CONFIGRET
function CM_Register_Notification(
  pFilter: Pointer;   // CM_NOTIFY_FILTER*
  pContext: Pointer;   // void* optional
  pCallback: Pointer;   // PCM_NOTIFY_CALLBACK
  pNotifyContext: Pointer   // HCMNOTIFICATION* out
): DWORD; stdcall;
  external 'CFGMGR32.dll' name 'CM_Register_Notification';
result := DllCall("CFGMGR32\CM_Register_Notification"
    , "Ptr", pFilter   ; CM_NOTIFY_FILTER*
    , "Ptr", pContext   ; void* optional
    , "Ptr", pCallback   ; PCM_NOTIFY_CALLBACK
    , "Ptr", pNotifyContext   ; HCMNOTIFICATION* out
    , "UInt")   ; return: CONFIGRET
●CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext) = DLL("CFGMGR32.dll", "dword CM_Register_Notification(void*, void*, void*, void*)")
# 呼び出し: CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext)
# pFilter : CM_NOTIFY_FILTER* -> "void*"
# pContext : void* optional -> "void*"
# pCallback : PCM_NOTIFY_CALLBACK -> "void*"
# pNotifyContext : HCMNOTIFICATION* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "cfgmgr32" fn CM_Register_Notification(
    pFilter: [*c]CM_NOTIFY_FILTER, // CM_NOTIFY_FILTER*
    pContext: ?*anyopaque, // void* optional
    pCallback: ?*anyopaque, // PCM_NOTIFY_CALLBACK
    pNotifyContext: ?*anyopaque // HCMNOTIFICATION* out
) callconv(std.os.windows.WINAPI) u32;
proc CM_Register_Notification(
    pFilter: ptr CM_NOTIFY_FILTER,  # CM_NOTIFY_FILTER*
    pContext: pointer,  # void* optional
    pCallback: pointer,  # PCM_NOTIFY_CALLBACK
    pNotifyContext: pointer  # HCMNOTIFICATION* out
): uint32 {.importc: "CM_Register_Notification", stdcall, dynlib: "CFGMGR32.dll".}
pragma(lib, "cfgmgr32");
extern(Windows)
uint CM_Register_Notification(
    CM_NOTIFY_FILTER* pFilter,   // CM_NOTIFY_FILTER*
    void* pContext,   // void* optional
    void* pCallback,   // PCM_NOTIFY_CALLBACK
    void* pNotifyContext   // HCMNOTIFICATION* out
);
ccall((:CM_Register_Notification, "CFGMGR32.dll"), stdcall, UInt32,
      (Ptr{CM_NOTIFY_FILTER}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
      pFilter, pContext, pCallback, pNotifyContext)
# pFilter : CM_NOTIFY_FILTER* -> Ptr{CM_NOTIFY_FILTER}
# pContext : void* optional -> Ptr{Cvoid}
# pCallback : PCM_NOTIFY_CALLBACK -> Ptr{Cvoid}
# pNotifyContext : HCMNOTIFICATION* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t CM_Register_Notification(
    void* pFilter,
    void* pContext,
    void* pCallback,
    void* pNotifyContext);
]]
local cfgmgr32 = ffi.load("cfgmgr32")
-- cfgmgr32.CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext)
-- pFilter : CM_NOTIFY_FILTER*
-- pContext : void* optional
-- pCallback : PCM_NOTIFY_CALLBACK
-- pNotifyContext : HCMNOTIFICATION* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CFGMGR32.dll');
const CM_Register_Notification = lib.func('__stdcall', 'CM_Register_Notification', 'uint32_t', ['void *', 'void *', 'void *', 'void *']);
// CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext)
// pFilter : CM_NOTIFY_FILTER* -> 'void *'
// pContext : void* optional -> 'void *'
// pCallback : PCM_NOTIFY_CALLBACK -> 'void *'
// pNotifyContext : HCMNOTIFICATION* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。
const lib = Deno.dlopen("CFGMGR32.dll", {
  CM_Register_Notification: { parameters: ["pointer", "pointer", "pointer", "pointer"], result: "u32" },
});
// lib.symbols.CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext)
// pFilter : CM_NOTIFY_FILTER* -> "pointer"
// pContext : void* optional -> "pointer"
// pCallback : PCM_NOTIFY_CALLBACK -> "pointer"
// pNotifyContext : HCMNOTIFICATION* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t CM_Register_Notification(
    void* pFilter,
    void* pContext,
    void* pCallback,
    void* pNotifyContext);
C, "CFGMGR32.dll");
// $ffi->CM_Register_Notification(pFilter, pContext, pCallback, pNotifyContext);
// pFilter : CM_NOTIFY_FILTER*
// pContext : void* optional
// pCallback : PCM_NOTIFY_CALLBACK
// pNotifyContext : HCMNOTIFICATION* out
// 構造体/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 Cfgmgr32 extends StdCallLibrary {
    Cfgmgr32 INSTANCE = Native.load("cfgmgr32", Cfgmgr32.class);
    int CM_Register_Notification(
        Pointer pFilter,   // CM_NOTIFY_FILTER*
        Pointer pContext,   // void* optional
        Callback pCallback,   // PCM_NOTIFY_CALLBACK
        Pointer pNotifyContext   // HCMNOTIFICATION* out
    );
}
@[Link("cfgmgr32")]
lib LibCFGMGR32
  fun CM_Register_Notification = CM_Register_Notification(
    pFilter : CM_NOTIFY_FILTER*,   # CM_NOTIFY_FILTER*
    pContext : Void*,   # void* optional
    pCallback : Void*,   # PCM_NOTIFY_CALLBACK
    pNotifyContext : Void*   # HCMNOTIFICATION* out
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CM_Register_NotificationNative = Uint32 Function(Pointer<Void>, Pointer<Void>, Pointer<Void>, Pointer<Void>);
typedef CM_Register_NotificationDart = int Function(Pointer<Void>, Pointer<Void>, Pointer<Void>, Pointer<Void>);
final CM_Register_Notification = DynamicLibrary.open('CFGMGR32.dll')
    .lookupFunction<CM_Register_NotificationNative, CM_Register_NotificationDart>('CM_Register_Notification');
// pFilter : CM_NOTIFY_FILTER* -> Pointer<Void>
// pContext : void* optional -> Pointer<Void>
// pCallback : PCM_NOTIFY_CALLBACK -> Pointer<Void>
// pNotifyContext : HCMNOTIFICATION* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CM_Register_Notification(
  pFilter: Pointer;   // CM_NOTIFY_FILTER*
  pContext: Pointer;   // void* optional
  pCallback: Pointer;   // PCM_NOTIFY_CALLBACK
  pNotifyContext: Pointer   // HCMNOTIFICATION* out
): DWORD; stdcall;
  external 'CFGMGR32.dll' name 'CM_Register_Notification';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CM_Register_Notification"
  c_CM_Register_Notification :: Ptr () -> Ptr () -> Ptr () -> Ptr () -> IO Word32
-- pFilter : CM_NOTIFY_FILTER* -> Ptr ()
-- pContext : void* optional -> Ptr ()
-- pCallback : PCM_NOTIFY_CALLBACK -> Ptr ()
-- pNotifyContext : HCMNOTIFICATION* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let cm_register_notification =
  foreign "CM_Register_Notification"
    ((ptr void) @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> returning uint32_t)
(* pFilter : CM_NOTIFY_FILTER* -> (ptr void) *)
(* pContext : void* optional -> (ptr void) *)
(* pCallback : PCM_NOTIFY_CALLBACK -> (ptr void) *)
(* pNotifyContext : HCMNOTIFICATION* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library cfgmgr32 (t "CFGMGR32.dll"))
(cffi:use-foreign-library cfgmgr32)

(cffi:defcfun ("CM_Register_Notification" cm-register-notification :convention :stdcall) :uint32
  (p-filter :pointer)   ; CM_NOTIFY_FILTER*
  (p-context :pointer)   ; void* optional
  (p-callback :pointer)   ; PCM_NOTIFY_CALLBACK
  (p-notify-context :pointer))   ; HCMNOTIFICATION* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CM_Register_Notification = Win32::API::More->new('CFGMGR32',
    'DWORD CM_Register_Notification(LPVOID pFilter, LPVOID pContext, LPVOID pCallback, HANDLE pNotifyContext)');
# my $ret = $CM_Register_Notification->Call($pFilter, $pContext, $pCallback, $pNotifyContext);
# pFilter : CM_NOTIFY_FILTER* -> LPVOID
# pContext : void* optional -> LPVOID
# pCallback : PCM_NOTIFY_CALLBACK -> LPVOID
# pNotifyContext : HCMNOTIFICATION* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

公式の関連項目
使用する型