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

RegLoadAppKeyW

関数
アプリ専用のハイブファイルをプライベートキーとして読み込む。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

WIN32_ERROR RegLoadAppKeyW(
    LPCWSTR lpFile,
    HKEY* phkResult,
    DWORD samDesired,
    DWORD dwOptions,
    DWORD Reserved   // optional
);

パラメーター

名前方向説明
lpFileLPCWSTRinハイブ ファイルの名前。このハイブは RegSaveKey 関数または RegSaveKeyEx 関数で作成されている必要があります。ファイルが存在しない場合は、指定した名前で空のハイブ ファイルが作成されます。
phkResultHKEY*out

読み込まれたハイブのルート キーへのハンドルを指すポインター。

ハイブ内のキーにアクセスする唯一の手段は、このハンドルです。レジストリは、キーへの絶対パスを使用してアプリケーションがこのハイブ内のキーにアクセスすることを防ぎます。そのため、レジストリの名前空間を通じてこのハイブへ移動することはできません。

samDesiredDWORDin返されるルート キーに対して要求するアクセス権を指定するマスク。詳細については、 Registry Key Security and Access Rights を参照してください。
dwOptionsDWORDinこのパラメーターが REG_PROCESS_APPKEY の場合、呼び出し元によって読み込まれている間、ハイブを再度読み込むことはできません。これにより、別の呼び出し元によるこのレジストリ ハイブへのアクセスが防止されます。
ReservedDWORDoptionalこのパラメーターは予約されています。

戻り値の型: WIN32_ERROR

公式ドキュメント

指定したレジストリ ハイブをアプリケーション ハイブとして読み込みます。(Unicode)

戻り値

関数が成功した場合、戻り値は ERROR_SUCCESS です。

関数が失敗した場合、戻り値は Winerror.h で定義されている 0 以外のエラー コードです。 FormatMessage 関数を FORMAT_MESSAGE_FROM_SYSTEM フラグとともに使用すると、エラーの一般的な説明を取得できます。

解説(Remarks)

RegLoadKey とは異なり、RegLoadAppKey はハイブを HKEY_LOCAL_MACHINEHKEY_USERS の下に読み込みません。代わりに、列挙できない特別なルートの下にハイブが読み込まれます。そのため、RegLoadAppKey によって現在読み込まれているハイブを列挙する方法はありません。RegLoadAppKey で読み込まれたハイブに対するすべての操作は、phkResult で返されるハンドルを基準にして実行する必要があります。

2 つのプロセスが同じハイブに対して操作を行う必要がある場合、各プロセスは RegLoadAppKey を呼び出してハンドルを取得する必要があります。RegLoadAppKey の操作中に、レジストリはファイルが既に読み込まれているかどうかを確認します。既に読み込まれている場合、レジストリはハイブを再度読み込むのではなく、以前に読み込まれたハイブへのハンドルを返します。

ハイブ内のすべてのキーは同じセキュリティ記述子を持つ必要があり、そうでない場合は関数が失敗します。このセキュリティ記述子は、samDesired パラメーターで指定されたアクセスを呼び出し元に許可する必要があり、そうでない場合は関数が失敗します。ハイブ内のいずれのキーに対しても RegSetKeySecurity 関数を使用することはできません。

Windows 8 以降では、各プロセスは RegLoadAppKey を呼び出して複数のハイブを読み込むことができます。Windows 7 以前では、各プロセスは一度に RegLoadAppKey を使用して 1 つのハイブのみを読み込むことができます。

RegLoadAppKey を使用して読み込まれたハイブは、ハイブ内のキーへのすべてのハンドルが RegCloseKey を使用して閉じられると、自動的にアンロードされます。

この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0600 以上として定義します。詳細については、 Using the Windows Headers を参照してください。

メモ

winreg.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして RegLoadAppKey を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイル エラーや実行時エラーを引き起こす不一致が生じる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

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

各言語での呼び出し定義

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

WIN32_ERROR RegLoadAppKeyW(
    LPCWSTR lpFile,
    HKEY* phkResult,
    DWORD samDesired,
    DWORD dwOptions,
    DWORD Reserved   // optional
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegLoadAppKeyW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpFile,   // LPCWSTR
    IntPtr phkResult,   // HKEY* out
    uint samDesired,   // DWORD
    uint dwOptions,   // DWORD
    uint Reserved   // DWORD optional
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegLoadAppKeyW(
    <MarshalAs(UnmanagedType.LPWStr)> lpFile As String,   ' LPCWSTR
    phkResult As IntPtr,   ' HKEY* out
    samDesired As UInteger,   ' DWORD
    dwOptions As UInteger,   ' DWORD
    Reserved As UInteger   ' DWORD optional
) As UInteger
End Function
' lpFile : LPCWSTR
' phkResult : HKEY* out
' samDesired : DWORD
' dwOptions : DWORD
' Reserved : DWORD optional
Declare PtrSafe Function RegLoadAppKeyW Lib "advapi32" ( _
    ByVal lpFile As LongPtr, _
    ByVal phkResult As LongPtr, _
    ByVal samDesired As Long, _
    ByVal dwOptions As Long, _
    ByVal Reserved As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

RegLoadAppKeyW = ctypes.windll.advapi32.RegLoadAppKeyW
RegLoadAppKeyW.restype = wintypes.DWORD
RegLoadAppKeyW.argtypes = [
    wintypes.LPCWSTR,  # lpFile : LPCWSTR
    ctypes.c_void_p,  # phkResult : HKEY* out
    wintypes.DWORD,  # samDesired : DWORD
    wintypes.DWORD,  # dwOptions : DWORD
    wintypes.DWORD,  # Reserved : DWORD optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
RegLoadAppKeyW = Fiddle::Function.new(
  lib['RegLoadAppKeyW'],
  [
    Fiddle::TYPE_VOIDP,  # lpFile : LPCWSTR
    Fiddle::TYPE_VOIDP,  # phkResult : HKEY* out
    -Fiddle::TYPE_INT,  # samDesired : DWORD
    -Fiddle::TYPE_INT,  # dwOptions : DWORD
    -Fiddle::TYPE_INT,  # Reserved : DWORD optional
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn RegLoadAppKeyW(
        lpFile: *const u16,  // LPCWSTR
        phkResult: *mut *mut core::ffi::c_void,  // HKEY* out
        samDesired: u32,  // DWORD
        dwOptions: u32,  // DWORD
        Reserved: u32  // DWORD optional
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode)]
public static extern uint RegLoadAppKeyW([MarshalAs(UnmanagedType.LPWStr)] string lpFile, IntPtr phkResult, uint samDesired, uint dwOptions, uint Reserved);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegLoadAppKeyW' -Namespace Win32 -PassThru
# $api::RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved)
#uselib "ADVAPI32.dll"
#func global RegLoadAppKeyW "RegLoadAppKeyW" wptr, wptr, wptr, wptr, wptr
; RegLoadAppKeyW lpFile, phkResult, samDesired, dwOptions, Reserved   ; 戻り値は stat
; lpFile : LPCWSTR -> "wptr"
; phkResult : HKEY* out -> "wptr"
; samDesired : DWORD -> "wptr"
; dwOptions : DWORD -> "wptr"
; Reserved : DWORD optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global RegLoadAppKeyW "RegLoadAppKeyW" wstr, sptr, int, int, int
; res = RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved)
; lpFile : LPCWSTR -> "wstr"
; phkResult : HKEY* out -> "sptr"
; samDesired : DWORD -> "int"
; dwOptions : DWORD -> "int"
; Reserved : DWORD optional -> "int"
; WIN32_ERROR RegLoadAppKeyW(LPCWSTR lpFile, HKEY* phkResult, DWORD samDesired, DWORD dwOptions, DWORD Reserved)
#uselib "ADVAPI32.dll"
#cfunc global RegLoadAppKeyW "RegLoadAppKeyW" wstr, intptr, int, int, int
; res = RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved)
; lpFile : LPCWSTR -> "wstr"
; phkResult : HKEY* out -> "intptr"
; samDesired : DWORD -> "int"
; dwOptions : DWORD -> "int"
; Reserved : DWORD optional -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRegLoadAppKeyW = advapi32.NewProc("RegLoadAppKeyW")
)

// lpFile (LPCWSTR), phkResult (HKEY* out), samDesired (DWORD), dwOptions (DWORD), Reserved (DWORD optional)
r1, _, err := procRegLoadAppKeyW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFile))),
	uintptr(phkResult),
	uintptr(samDesired),
	uintptr(dwOptions),
	uintptr(Reserved),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function RegLoadAppKeyW(
  lpFile: PWideChar;   // LPCWSTR
  phkResult: Pointer;   // HKEY* out
  samDesired: DWORD;   // DWORD
  dwOptions: DWORD;   // DWORD
  Reserved: DWORD   // DWORD optional
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegLoadAppKeyW';
result := DllCall("ADVAPI32\RegLoadAppKeyW"
    , "WStr", lpFile   ; LPCWSTR
    , "Ptr", phkResult   ; HKEY* out
    , "UInt", samDesired   ; DWORD
    , "UInt", dwOptions   ; DWORD
    , "UInt", Reserved   ; DWORD optional
    , "UInt")   ; return: WIN32_ERROR
●RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved) = DLL("ADVAPI32.dll", "dword RegLoadAppKeyW(char*, void*, dword, dword, dword)")
# 呼び出し: RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved)
# lpFile : LPCWSTR -> "char*"
# phkResult : HKEY* out -> "void*"
# samDesired : DWORD -> "dword"
# dwOptions : DWORD -> "dword"
# Reserved : DWORD optional -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "advapi32" fn RegLoadAppKeyW(
    lpFile: [*c]const u16, // LPCWSTR
    phkResult: ?*anyopaque, // HKEY* out
    samDesired: u32, // DWORD
    dwOptions: u32, // DWORD
    Reserved: u32 // DWORD optional
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc RegLoadAppKeyW(
    lpFile: WideCString,  # LPCWSTR
    phkResult: pointer,  # HKEY* out
    samDesired: uint32,  # DWORD
    dwOptions: uint32,  # DWORD
    Reserved: uint32  # DWORD optional
): uint32 {.importc: "RegLoadAppKeyW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "advapi32");
extern(Windows)
uint RegLoadAppKeyW(
    const(wchar)* lpFile,   // LPCWSTR
    void* phkResult,   // HKEY* out
    uint samDesired,   // DWORD
    uint dwOptions,   // DWORD
    uint Reserved   // DWORD optional
);
ccall((:RegLoadAppKeyW, "ADVAPI32.dll"), stdcall, UInt32,
      (Cwstring, Ptr{Cvoid}, UInt32, UInt32, UInt32),
      lpFile, phkResult, samDesired, dwOptions, Reserved)
# lpFile : LPCWSTR -> Cwstring
# phkResult : HKEY* out -> Ptr{Cvoid}
# samDesired : DWORD -> UInt32
# dwOptions : DWORD -> UInt32
# Reserved : DWORD optional -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t RegLoadAppKeyW(
    const uint16_t* lpFile,
    void* phkResult,
    uint32_t samDesired,
    uint32_t dwOptions,
    uint32_t Reserved);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved)
-- lpFile : LPCWSTR
-- phkResult : HKEY* out
-- samDesired : DWORD
-- dwOptions : DWORD
-- Reserved : DWORD optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RegLoadAppKeyW = lib.func('__stdcall', 'RegLoadAppKeyW', 'uint32_t', ['str16', 'void *', 'uint32_t', 'uint32_t', 'uint32_t']);
// RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved)
// lpFile : LPCWSTR -> 'str16'
// phkResult : HKEY* out -> 'void *'
// samDesired : DWORD -> 'uint32_t'
// dwOptions : DWORD -> 'uint32_t'
// Reserved : DWORD optional -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  RegLoadAppKeyW: { parameters: ["buffer", "pointer", "u32", "u32", "u32"], result: "u32" },
});
// lib.symbols.RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved)
// lpFile : LPCWSTR -> "buffer"
// phkResult : HKEY* out -> "pointer"
// samDesired : DWORD -> "u32"
// dwOptions : DWORD -> "u32"
// Reserved : DWORD optional -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t RegLoadAppKeyW(
    const uint16_t* lpFile,
    void* phkResult,
    uint32_t samDesired,
    uint32_t dwOptions,
    uint32_t Reserved);
C, "ADVAPI32.dll");
// $ffi->RegLoadAppKeyW(lpFile, phkResult, samDesired, dwOptions, Reserved);
// lpFile : LPCWSTR
// phkResult : HKEY* out
// samDesired : DWORD
// dwOptions : DWORD
// Reserved : DWORD optional
// 構造体/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 Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
    int RegLoadAppKeyW(
        WString lpFile,   // LPCWSTR
        Pointer phkResult,   // HKEY* out
        int samDesired,   // DWORD
        int dwOptions,   // DWORD
        int Reserved   // DWORD optional
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("advapi32")]
lib LibADVAPI32
  fun RegLoadAppKeyW = RegLoadAppKeyW(
    lpFile : UInt16*,   # LPCWSTR
    phkResult : Void*,   # HKEY* out
    samDesired : UInt32,   # DWORD
    dwOptions : UInt32,   # DWORD
    Reserved : UInt32   # DWORD optional
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef RegLoadAppKeyWNative = Uint32 Function(Pointer<Utf16>, Pointer<Void>, Uint32, Uint32, Uint32);
typedef RegLoadAppKeyWDart = int Function(Pointer<Utf16>, Pointer<Void>, int, int, int);
final RegLoadAppKeyW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<RegLoadAppKeyWNative, RegLoadAppKeyWDart>('RegLoadAppKeyW');
// lpFile : LPCWSTR -> Pointer<Utf16>
// phkResult : HKEY* out -> Pointer<Void>
// samDesired : DWORD -> Uint32
// dwOptions : DWORD -> Uint32
// Reserved : DWORD optional -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function RegLoadAppKeyW(
  lpFile: PWideChar;   // LPCWSTR
  phkResult: Pointer;   // HKEY* out
  samDesired: DWORD;   // DWORD
  dwOptions: DWORD;   // DWORD
  Reserved: DWORD   // DWORD optional
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegLoadAppKeyW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RegLoadAppKeyW"
  c_RegLoadAppKeyW :: CWString -> Ptr () -> Word32 -> Word32 -> Word32 -> IO Word32
-- lpFile : LPCWSTR -> CWString
-- phkResult : HKEY* out -> Ptr ()
-- samDesired : DWORD -> Word32
-- dwOptions : DWORD -> Word32
-- Reserved : DWORD optional -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let regloadappkeyw =
  foreign "RegLoadAppKeyW"
    ((ptr uint16_t) @-> (ptr void) @-> uint32_t @-> uint32_t @-> uint32_t @-> returning uint32_t)
(* lpFile : LPCWSTR -> (ptr uint16_t) *)
(* phkResult : HKEY* out -> (ptr void) *)
(* samDesired : DWORD -> uint32_t *)
(* dwOptions : DWORD -> uint32_t *)
(* Reserved : DWORD optional -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("RegLoadAppKeyW" reg-load-app-key-w :convention :stdcall) :uint32
  (lp-file (:string :encoding :utf-16le))   ; LPCWSTR
  (phk-result :pointer)   ; HKEY* out
  (sam-desired :uint32)   ; DWORD
  (dw-options :uint32)   ; DWORD
  (reserved :uint32))   ; DWORD optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RegLoadAppKeyW = Win32::API::More->new('ADVAPI32',
    'DWORD RegLoadAppKeyW(LPCWSTR lpFile, HANDLE phkResult, DWORD samDesired, DWORD dwOptions, DWORD Reserved)');
# my $ret = $RegLoadAppKeyW->Call($lpFile, $phkResult, $samDesired, $dwOptions, $Reserved);
# lpFile : LPCWSTR -> LPCWSTR
# phkResult : HKEY* out -> HANDLE
# samDesired : DWORD -> DWORD
# dwOptions : DWORD -> DWORD
# Reserved : DWORD optional -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
公式の関連項目
使用する型