Win32 API 日本語リファレンス
ホームSystem.Power › RegisterPowerSettingNotification

RegisterPowerSettingNotification

関数
指定電源設定の変更通知を受け取るよう登録する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

HPOWERNOTIFY RegisterPowerSettingNotification(
    HANDLE hRecipient,
    const GUID* PowerSettingGuid,
    REGISTER_NOTIFICATION_FLAGS Flags
);

パラメーター

名前方向
hRecipientHANDLEin
PowerSettingGuidGUID*in
FlagsREGISTER_NOTIFICATION_FLAGSin

戻り値の型: HPOWERNOTIFY

各言語での呼び出し定義

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

HPOWERNOTIFY RegisterPowerSettingNotification(
    HANDLE hRecipient,
    const GUID* PowerSettingGuid,
    REGISTER_NOTIFICATION_FLAGS Flags
);
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr RegisterPowerSettingNotification(
    IntPtr hRecipient,   // HANDLE
    ref Guid PowerSettingGuid,   // GUID*
    uint Flags   // REGISTER_NOTIFICATION_FLAGS
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function RegisterPowerSettingNotification(
    hRecipient As IntPtr,   ' HANDLE
    ByRef PowerSettingGuid As Guid,   ' GUID*
    Flags As UInteger   ' REGISTER_NOTIFICATION_FLAGS
) As IntPtr
End Function
' hRecipient : HANDLE
' PowerSettingGuid : GUID*
' Flags : REGISTER_NOTIFICATION_FLAGS
Declare PtrSafe Function RegisterPowerSettingNotification Lib "user32" ( _
    ByVal hRecipient As LongPtr, _
    ByVal PowerSettingGuid 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

RegisterPowerSettingNotification = ctypes.windll.user32.RegisterPowerSettingNotification
RegisterPowerSettingNotification.restype = ctypes.c_ssize_t
RegisterPowerSettingNotification.argtypes = [
    wintypes.HANDLE,  # hRecipient : HANDLE
    ctypes.c_void_p,  # PowerSettingGuid : GUID*
    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')
RegisterPowerSettingNotification = Fiddle::Function.new(
  lib['RegisterPowerSettingNotification'],
  [
    Fiddle::TYPE_VOIDP,  # hRecipient : HANDLE
    Fiddle::TYPE_VOIDP,  # PowerSettingGuid : GUID*
    -Fiddle::TYPE_INT,  # Flags : REGISTER_NOTIFICATION_FLAGS
  ],
  Fiddle::TYPE_INTPTR_T)
#[link(name = "user32")]
extern "system" {
    fn RegisterPowerSettingNotification(
        hRecipient: *mut core::ffi::c_void,  // HANDLE
        PowerSettingGuid: *const GUID,  // GUID*
        Flags: u32  // REGISTER_NOTIFICATION_FLAGS
    ) -> isize;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", SetLastError = true)]
public static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient, ref Guid PowerSettingGuid, uint Flags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_RegisterPowerSettingNotification' -Namespace Win32 -PassThru
# $api::RegisterPowerSettingNotification(hRecipient, PowerSettingGuid, Flags)
#uselib "USER32.dll"
#func global RegisterPowerSettingNotification "RegisterPowerSettingNotification" sptr, sptr, sptr
; RegisterPowerSettingNotification hRecipient, varptr(PowerSettingGuid), Flags   ; 戻り値は stat
; hRecipient : HANDLE -> "sptr"
; PowerSettingGuid : GUID* -> "sptr"
; Flags : REGISTER_NOTIFICATION_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global RegisterPowerSettingNotification "RegisterPowerSettingNotification" sptr, var, int
; res = RegisterPowerSettingNotification(hRecipient, PowerSettingGuid, Flags)
; hRecipient : HANDLE -> "sptr"
; PowerSettingGuid : GUID* -> "var"
; Flags : REGISTER_NOTIFICATION_FLAGS -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HPOWERNOTIFY RegisterPowerSettingNotification(HANDLE hRecipient, GUID* PowerSettingGuid, REGISTER_NOTIFICATION_FLAGS Flags)
#uselib "USER32.dll"
#cfunc global RegisterPowerSettingNotification "RegisterPowerSettingNotification" intptr, var, int
; res = RegisterPowerSettingNotification(hRecipient, PowerSettingGuid, Flags)
; hRecipient : HANDLE -> "intptr"
; PowerSettingGuid : GUID* -> "var"
; Flags : REGISTER_NOTIFICATION_FLAGS -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procRegisterPowerSettingNotification = user32.NewProc("RegisterPowerSettingNotification")
)

// hRecipient (HANDLE), PowerSettingGuid (GUID*), Flags (REGISTER_NOTIFICATION_FLAGS)
r1, _, err := procRegisterPowerSettingNotification.Call(
	uintptr(hRecipient),
	uintptr(PowerSettingGuid),
	uintptr(Flags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HPOWERNOTIFY
function RegisterPowerSettingNotification(
  hRecipient: THandle;   // HANDLE
  PowerSettingGuid: PGUID;   // GUID*
  Flags: DWORD   // REGISTER_NOTIFICATION_FLAGS
): NativeInt; stdcall;
  external 'USER32.dll' name 'RegisterPowerSettingNotification';
result := DllCall("USER32\RegisterPowerSettingNotification"
    , "Ptr", hRecipient   ; HANDLE
    , "Ptr", PowerSettingGuid   ; GUID*
    , "UInt", Flags   ; REGISTER_NOTIFICATION_FLAGS
    , "Ptr")   ; return: HPOWERNOTIFY
●RegisterPowerSettingNotification(hRecipient, PowerSettingGuid, Flags) = DLL("USER32.dll", "int RegisterPowerSettingNotification(void*, void*, dword)")
# 呼び出し: RegisterPowerSettingNotification(hRecipient, PowerSettingGuid, Flags)
# hRecipient : HANDLE -> "void*"
# PowerSettingGuid : GUID* -> "void*"
# Flags : REGISTER_NOTIFICATION_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。