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

PowerOpenUserPowerKey

関数
ユーザー固有の電源設定レジストリキーを開く。
DLLPOWRPROF.dll呼出規約winapi

シグネチャ

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

DWORD PowerOpenUserPowerKey(
    HKEY* phUserPowerKey,
    DWORD Access,
    BOOL OpenExisting
);

パラメーター

名前方向説明
phUserPowerKeyHKEY*out開いたユーザー電源キーのハンドルを受け取るHKEYへのポインタ。出力。使用後はRegCloseKeyで閉じる。
AccessDWORDin要求するアクセス権(KEY_READ等のレジストリSAMフラグ)を示すDWORD。
OpenExistingBOOLin既存キーのみを開く場合はTRUE、無ければ作成する場合はFALSE。

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD PowerOpenUserPowerKey(
    HKEY* phUserPowerKey,
    DWORD Access,
    BOOL OpenExisting
);
[DllImport("POWRPROF.dll", ExactSpelling = true)]
static extern uint PowerOpenUserPowerKey(
    IntPtr phUserPowerKey,   // HKEY* out
    uint Access,   // DWORD
    bool OpenExisting   // BOOL
);
<DllImport("POWRPROF.dll", ExactSpelling:=True)>
Public Shared Function PowerOpenUserPowerKey(
    phUserPowerKey As IntPtr,   ' HKEY* out
    Access As UInteger,   ' DWORD
    OpenExisting As Boolean   ' BOOL
) As UInteger
End Function
' phUserPowerKey : HKEY* out
' Access : DWORD
' OpenExisting : BOOL
Declare PtrSafe Function PowerOpenUserPowerKey Lib "powrprof" ( _
    ByVal phUserPowerKey As LongPtr, _
    ByVal Access As Long, _
    ByVal OpenExisting As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PowerOpenUserPowerKey = ctypes.windll.powrprof.PowerOpenUserPowerKey
PowerOpenUserPowerKey.restype = wintypes.DWORD
PowerOpenUserPowerKey.argtypes = [
    ctypes.c_void_p,  # phUserPowerKey : HKEY* out
    wintypes.DWORD,  # Access : DWORD
    wintypes.BOOL,  # OpenExisting : BOOL
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('POWRPROF.dll')
PowerOpenUserPowerKey = Fiddle::Function.new(
  lib['PowerOpenUserPowerKey'],
  [
    Fiddle::TYPE_VOIDP,  # phUserPowerKey : HKEY* out
    -Fiddle::TYPE_INT,  # Access : DWORD
    Fiddle::TYPE_INT,  # OpenExisting : BOOL
  ],
  -Fiddle::TYPE_INT)
#[link(name = "powrprof")]
extern "system" {
    fn PowerOpenUserPowerKey(
        phUserPowerKey: *mut *mut core::ffi::c_void,  // HKEY* out
        Access: u32,  // DWORD
        OpenExisting: i32  // BOOL
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("POWRPROF.dll")]
public static extern uint PowerOpenUserPowerKey(IntPtr phUserPowerKey, uint Access, bool OpenExisting);
"@
$api = Add-Type -MemberDefinition $sig -Name 'POWRPROF_PowerOpenUserPowerKey' -Namespace Win32 -PassThru
# $api::PowerOpenUserPowerKey(phUserPowerKey, Access, OpenExisting)
#uselib "POWRPROF.dll"
#func global PowerOpenUserPowerKey "PowerOpenUserPowerKey" sptr, sptr, sptr
; PowerOpenUserPowerKey phUserPowerKey, Access, OpenExisting   ; 戻り値は stat
; phUserPowerKey : HKEY* out -> "sptr"
; Access : DWORD -> "sptr"
; OpenExisting : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "POWRPROF.dll"
#cfunc global PowerOpenUserPowerKey "PowerOpenUserPowerKey" sptr, int, int
; res = PowerOpenUserPowerKey(phUserPowerKey, Access, OpenExisting)
; phUserPowerKey : HKEY* out -> "sptr"
; Access : DWORD -> "int"
; OpenExisting : BOOL -> "int"
; DWORD PowerOpenUserPowerKey(HKEY* phUserPowerKey, DWORD Access, BOOL OpenExisting)
#uselib "POWRPROF.dll"
#cfunc global PowerOpenUserPowerKey "PowerOpenUserPowerKey" intptr, int, int
; res = PowerOpenUserPowerKey(phUserPowerKey, Access, OpenExisting)
; phUserPowerKey : HKEY* out -> "intptr"
; Access : DWORD -> "int"
; OpenExisting : BOOL -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	powrprof = windows.NewLazySystemDLL("POWRPROF.dll")
	procPowerOpenUserPowerKey = powrprof.NewProc("PowerOpenUserPowerKey")
)

// phUserPowerKey (HKEY* out), Access (DWORD), OpenExisting (BOOL)
r1, _, err := procPowerOpenUserPowerKey.Call(
	uintptr(phUserPowerKey),
	uintptr(Access),
	uintptr(OpenExisting),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function PowerOpenUserPowerKey(
  phUserPowerKey: Pointer;   // HKEY* out
  Access: DWORD;   // DWORD
  OpenExisting: BOOL   // BOOL
): DWORD; stdcall;
  external 'POWRPROF.dll' name 'PowerOpenUserPowerKey';
result := DllCall("POWRPROF\PowerOpenUserPowerKey"
    , "Ptr", phUserPowerKey   ; HKEY* out
    , "UInt", Access   ; DWORD
    , "Int", OpenExisting   ; BOOL
    , "UInt")   ; return: DWORD
●PowerOpenUserPowerKey(phUserPowerKey, Access, OpenExisting) = DLL("POWRPROF.dll", "dword PowerOpenUserPowerKey(void*, dword, bool)")
# 呼び出し: PowerOpenUserPowerKey(phUserPowerKey, Access, OpenExisting)
# phUserPowerKey : HKEY* out -> "void*"
# Access : DWORD -> "dword"
# OpenExisting : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef PowerOpenUserPowerKeyNative = Uint32 Function(Pointer<Void>, Uint32, Int32);
typedef PowerOpenUserPowerKeyDart = int Function(Pointer<Void>, int, int);
final PowerOpenUserPowerKey = DynamicLibrary.open('POWRPROF.dll')
    .lookupFunction<PowerOpenUserPowerKeyNative, PowerOpenUserPowerKeyDart>('PowerOpenUserPowerKey');
// phUserPowerKey : HKEY* out -> Pointer<Void>
// Access : DWORD -> Uint32
// OpenExisting : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PowerOpenUserPowerKey(
  phUserPowerKey: Pointer;   // HKEY* out
  Access: DWORD;   // DWORD
  OpenExisting: BOOL   // BOOL
): DWORD; stdcall;
  external 'POWRPROF.dll' name 'PowerOpenUserPowerKey';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PowerOpenUserPowerKey"
  c_PowerOpenUserPowerKey :: Ptr () -> Word32 -> CInt -> IO Word32
-- phUserPowerKey : HKEY* out -> Ptr ()
-- Access : DWORD -> Word32
-- OpenExisting : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let poweropenuserpowerkey =
  foreign "PowerOpenUserPowerKey"
    ((ptr void) @-> uint32_t @-> int32_t @-> returning uint32_t)
(* phUserPowerKey : HKEY* out -> (ptr void) *)
(* Access : DWORD -> uint32_t *)
(* OpenExisting : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library powrprof (t "POWRPROF.dll"))
(cffi:use-foreign-library powrprof)

(cffi:defcfun ("PowerOpenUserPowerKey" power-open-user-power-key :convention :stdcall) :uint32
  (ph-user-power-key :pointer)   ; HKEY* out
  (access :uint32)   ; DWORD
  (open-existing :int32))   ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PowerOpenUserPowerKey = Win32::API::More->new('POWRPROF',
    'DWORD PowerOpenUserPowerKey(HANDLE phUserPowerKey, DWORD Access, BOOL OpenExisting)');
# my $ret = $PowerOpenUserPowerKey->Call($phUserPowerKey, $Access, $OpenExisting);
# phUserPowerKey : HKEY* out -> HANDLE
# Access : DWORD -> DWORD
# OpenExisting : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。