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

RegCreateKeyExW

関数
オプションやアクセス権を指定してレジストリキーを作成または開く。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR RegCreateKeyExW(
    HKEY hKey,
    LPCWSTR lpSubKey,
    DWORD Reserved,   // optional
    LPWSTR lpClass,   // optional
    REG_OPEN_CREATE_OPTIONS dwOptions,
    REG_SAM_FLAGS samDesired,
    const SECURITY_ATTRIBUTES* lpSecurityAttributes,   // optional
    HKEY* phkResult,
    REG_CREATE_KEY_DISPOSITION* lpdwDisposition   // optional
);

パラメーター

名前方向説明
hKeyHKEYin

開いているレジストリキーへのハンドル。呼び出し側プロセスは、そのキーに対する KEY_CREATE_SUB_KEY アクセス権を持っている必要があります。詳細については、 Registry Key Security and Access Rights を参照してください。

キー作成のアクセスは、ハンドル取得時に指定したアクセスマスクではなく、レジストリキーのセキュリティ記述子に対して確認されます。したがって、hKeyKEY_READsamDesired で開かれていても、セキュリティ記述子で許可されていれば、レジストリを変更する操作に使用できます。

このハンドルは、 RegCreateKeyEx 関数または RegOpenKeyEx 関数によって返されます。または、次の 事前定義キー のいずれかを指定できます。

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
lpSubKeyLPCWSTRin

この関数が開く、または作成するサブキーの名前。指定するサブキーは、hKey パラメーターで識別されるキーのサブキーである必要があります。レジストリツリー内で最大 32 階層の深さまで指定できます。キー名の詳細については、Structure of the Registry を参照してください。

lpSubKey が空の文字列へのポインターである場合、phkResulthKey で指定されたキーへの新しいハンドルを受け取ります。

このパラメーターを NULL にすることはできません。

ReservedDWORDoptionalこのパラメーターは予約されており、ゼロにする必要があります。
lpClassLPWSTRinoptionalこのキーのユーザー定義クラス型。このパラメーターは無視される場合があります。このパラメーターは NULL にできます。
dwOptionsREG_OPEN_CREATE_OPTIONSin

このパラメーターには、次の値のいずれかを指定できます。

意味
REG_OPTION_BACKUP_RESTORE
0x00000004L
このフラグが設定されている場合、関数は samDesired パラメーターを無視し、キーのバックアップまたは復元に必要なアクセス権でキーを開こうとします。呼び出し側スレッドが SE_BACKUP_NAME 特権を有効にしている場合、キーは ACCESS_SYSTEM_SECURITY および KEY_READ アクセス権で開かれます。呼び出し側スレッドが SE_RESTORE_NAME 特権を有効にしている場合、Windows Vista 以降では、キーは ACCESS_SYSTEM_SECURITY、DELETE、および KEY_WRITE アクセス権で開かれます。両方の特権が有効になっている場合、キーは両方の特権を組み合わせたアクセス権を持ちます。詳細については、 Running with Special Privileges を参照してください。
REG_OPTION_CREATE_LINK
0x00000002L
注意 レジストリのシンボリックリンクは、どうしても必要な場合のアプリケーション互換性のためにのみ使用してください。
このキーはシンボリックリンクです。ターゲットパスは、キーの L"SymbolicLinkValue" 値に割り当てられます。ターゲットパスは絶対レジストリパスである必要があります。
REG_OPTION_NON_VOLATILE
0x00000000L
このキーは揮発性ではありません。これが既定です。情報はファイルに保存され、システムの再起動後も保持されます。 RegSaveKey 関数は、揮発性でないキーを保存します。
REG_OPTION_VOLATILE
0x00000001L
この関数によって作成されるすべてのキーは揮発性です。情報はメモリに保存され、対応するレジストリハイブがアンロードされると保持されません。HKEY_LOCAL_MACHINE の場合、これはシステムが完全なシャットダウンを開始したときにのみ発生します。 RegLoadKey 関数によって読み込まれたレジストリキーの場合、これは対応する RegUnLoadKey が実行されたときに発生します。 RegSaveKey 関数は揮発性キーを保存しません。このフラグは、既に存在するキーに対しては無視されます。
注意 ユーザーが選択したシャットダウンでは、 高速スタートアップシャットダウンがシステムの既定の動作です。
samDesiredREG_SAM_FLAGSin作成するキーのアクセス権を指定するマスク。詳細については、 Registry Key Security and Access Rights を参照してください。
lpSecurityAttributesSECURITY_ATTRIBUTES*inoptional

返されるハンドルを子プロセスが継承できるかどうかを決定する SECURITY_ATTRIBUTES 構造体へのポインター。lpSecurityAttributesNULL の場合、ハンドルは継承できません。

構造体の lpSecurityDescriptor メンバーは、新しいキーのセキュリティ記述子を指定します。lpSecurityAttributesNULL の場合、キーには既定のセキュリティ記述子が設定されます。キーの既定のセキュリティ記述子内の ACL は、その直接の親キーから継承されます。

phkResultHKEY*out開かれた、または作成されたキーへのハンドルを受け取る変数へのポインター。キーが事前定義レジストリキーのいずれでもない場合は、ハンドルの使用を終えた後に RegCloseKey 関数を呼び出してください。
lpdwDispositionREG_CREATE_KEY_DISPOSITION*outoptional

次のいずれかの処理結果(disposition)の値を受け取る変数へのポインター。

意味
REG_CREATED_NEW_KEY
0x00000001L
キーは存在せず、新たに作成されました。
REG_OPENED_EXISTING_KEY
0x00000002L
キーは存在しており、変更されることなく単に開かれました。

lpdwDispositionNULL の場合、処理結果情報は返されません。

戻り値の型: WIN32_ERROR

公式ドキュメント

指定したレジストリキーを作成します。キーが既に存在する場合、この関数はそのキーを開きます。なお、キー名は大文字と小文字を区別しません。(Unicode)

戻り値

関数が成功すると、戻り値は ERROR_SUCCESS です。

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

解説(Remarks)

RegCreateKeyEx 関数が作成するキーには値がありません。アプリケーションは、 RegSetValueEx 関数を使用してキーの値を設定できます。

RegCreateKeyEx 関数は、指定したパス内に存在しないすべてのキーを作成します。アプリケーションはこの動作を利用して、複数のキーを一度に作成できます。たとえば、アプリケーションは lpSubKey パラメーターに次の形式の文字列を指定することで、先行する 3 つのサブキーと同時に 4 階層下のサブキーを作成できます。

subkey1\subkey2\subkey3\subkey4

なお、この動作のため、パス内の既存のキーのスペルが間違っていると、意図しないキーが作成される結果になります。

アプリケーションは、HKEY_USERS または HKEY_LOCAL_MACHINE の直接の子であるキーを作成することはできません。アプリケーションは、HKEY_USERS または HKEY_LOCAL_MACHINE ツリーの下位レベルにサブキーを作成できます。

サービスまたはアプリケーションが異なるユーザーを偽装する場合、この関数を HKEY_CURRENT_USER とともに使用しないでください。代わりに、RegOpenCurrentUser 関数を呼び出してください。

なお、特定のレジストリキーにアクセスする操作はリダイレクトされます。詳細については、Registry Virtualization および 32-bit and 64-bit Application Data in the Registry を参照してください。

メモ

winreg.h ヘッダーは、RegCreateKeyEx を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスと、エンコーディング非依存でないコードを混在して使用すると、不整合が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、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 RegCreateKeyExW(
    HKEY hKey,
    LPCWSTR lpSubKey,
    DWORD Reserved,   // optional
    LPWSTR lpClass,   // optional
    REG_OPEN_CREATE_OPTIONS dwOptions,
    REG_SAM_FLAGS samDesired,
    const SECURITY_ATTRIBUTES* lpSecurityAttributes,   // optional
    HKEY* phkResult,
    REG_CREATE_KEY_DISPOSITION* lpdwDisposition   // optional
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegCreateKeyExW(
    IntPtr hKey,   // HKEY
    [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey,   // LPCWSTR
    uint Reserved,   // DWORD optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpClass,   // LPWSTR optional
    uint dwOptions,   // REG_OPEN_CREATE_OPTIONS
    uint samDesired,   // REG_SAM_FLAGS
    IntPtr lpSecurityAttributes,   // SECURITY_ATTRIBUTES* optional
    IntPtr phkResult,   // HKEY* out
    IntPtr lpdwDisposition   // REG_CREATE_KEY_DISPOSITION* optional, out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegCreateKeyExW(
    hKey As IntPtr,   ' HKEY
    <MarshalAs(UnmanagedType.LPWStr)> lpSubKey As String,   ' LPCWSTR
    Reserved As UInteger,   ' DWORD optional
    <MarshalAs(UnmanagedType.LPWStr)> lpClass As String,   ' LPWSTR optional
    dwOptions As UInteger,   ' REG_OPEN_CREATE_OPTIONS
    samDesired As UInteger,   ' REG_SAM_FLAGS
    lpSecurityAttributes As IntPtr,   ' SECURITY_ATTRIBUTES* optional
    phkResult As IntPtr,   ' HKEY* out
    lpdwDisposition As IntPtr   ' REG_CREATE_KEY_DISPOSITION* optional, out
) As UInteger
End Function
' hKey : HKEY
' lpSubKey : LPCWSTR
' Reserved : DWORD optional
' lpClass : LPWSTR optional
' dwOptions : REG_OPEN_CREATE_OPTIONS
' samDesired : REG_SAM_FLAGS
' lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
' phkResult : HKEY* out
' lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out
Declare PtrSafe Function RegCreateKeyExW Lib "advapi32" ( _
    ByVal hKey As LongPtr, _
    ByVal lpSubKey As LongPtr, _
    ByVal Reserved As Long, _
    ByVal lpClass As LongPtr, _
    ByVal dwOptions As Long, _
    ByVal samDesired As Long, _
    ByVal lpSecurityAttributes As LongPtr, _
    ByVal phkResult As LongPtr, _
    ByVal lpdwDisposition As LongPtr) 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

RegCreateKeyExW = ctypes.windll.advapi32.RegCreateKeyExW
RegCreateKeyExW.restype = wintypes.DWORD
RegCreateKeyExW.argtypes = [
    wintypes.HANDLE,  # hKey : HKEY
    wintypes.LPCWSTR,  # lpSubKey : LPCWSTR
    wintypes.DWORD,  # Reserved : DWORD optional
    wintypes.LPCWSTR,  # lpClass : LPWSTR optional
    wintypes.DWORD,  # dwOptions : REG_OPEN_CREATE_OPTIONS
    wintypes.DWORD,  # samDesired : REG_SAM_FLAGS
    ctypes.c_void_p,  # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
    ctypes.c_void_p,  # phkResult : HKEY* out
    ctypes.c_void_p,  # lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
RegCreateKeyExW = Fiddle::Function.new(
  lib['RegCreateKeyExW'],
  [
    Fiddle::TYPE_VOIDP,  # hKey : HKEY
    Fiddle::TYPE_VOIDP,  # lpSubKey : LPCWSTR
    -Fiddle::TYPE_INT,  # Reserved : DWORD optional
    Fiddle::TYPE_VOIDP,  # lpClass : LPWSTR optional
    -Fiddle::TYPE_INT,  # dwOptions : REG_OPEN_CREATE_OPTIONS
    -Fiddle::TYPE_INT,  # samDesired : REG_SAM_FLAGS
    Fiddle::TYPE_VOIDP,  # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
    Fiddle::TYPE_VOIDP,  # phkResult : HKEY* out
    Fiddle::TYPE_VOIDP,  # lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn RegCreateKeyExW(
        hKey: *mut core::ffi::c_void,  // HKEY
        lpSubKey: *const u16,  // LPCWSTR
        Reserved: u32,  // DWORD optional
        lpClass: *mut u16,  // LPWSTR optional
        dwOptions: u32,  // REG_OPEN_CREATE_OPTIONS
        samDesired: u32,  // REG_SAM_FLAGS
        lpSecurityAttributes: *const SECURITY_ATTRIBUTES,  // SECURITY_ATTRIBUTES* optional
        phkResult: *mut *mut core::ffi::c_void,  // HKEY* out
        lpdwDisposition: *mut u32  // REG_CREATE_KEY_DISPOSITION* optional, out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode)]
public static extern uint RegCreateKeyExW(IntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey, uint Reserved, [MarshalAs(UnmanagedType.LPWStr)] string lpClass, uint dwOptions, uint samDesired, IntPtr lpSecurityAttributes, IntPtr phkResult, IntPtr lpdwDisposition);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegCreateKeyExW' -Namespace Win32 -PassThru
# $api::RegCreateKeyExW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
#uselib "ADVAPI32.dll"
#func global RegCreateKeyExW "RegCreateKeyExW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; RegCreateKeyExW hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, varptr(lpSecurityAttributes), phkResult, varptr(lpdwDisposition)   ; 戻り値は stat
; hKey : HKEY -> "wptr"
; lpSubKey : LPCWSTR -> "wptr"
; Reserved : DWORD optional -> "wptr"
; lpClass : LPWSTR optional -> "wptr"
; dwOptions : REG_OPEN_CREATE_OPTIONS -> "wptr"
; samDesired : REG_SAM_FLAGS -> "wptr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "wptr"
; phkResult : HKEY* out -> "wptr"
; lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global RegCreateKeyExW "RegCreateKeyExW" sptr, wstr, int, wstr, int, int, var, sptr, var
; res = RegCreateKeyExW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
; hKey : HKEY -> "sptr"
; lpSubKey : LPCWSTR -> "wstr"
; Reserved : DWORD optional -> "int"
; lpClass : LPWSTR optional -> "wstr"
; dwOptions : REG_OPEN_CREATE_OPTIONS -> "int"
; samDesired : REG_SAM_FLAGS -> "int"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; phkResult : HKEY* out -> "sptr"
; lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR RegCreateKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD Reserved, LPWSTR lpClass, REG_OPEN_CREATE_OPTIONS dwOptions, REG_SAM_FLAGS samDesired, SECURITY_ATTRIBUTES* lpSecurityAttributes, HKEY* phkResult, REG_CREATE_KEY_DISPOSITION* lpdwDisposition)
#uselib "ADVAPI32.dll"
#cfunc global RegCreateKeyExW "RegCreateKeyExW" intptr, wstr, int, wstr, int, int, var, intptr, var
; res = RegCreateKeyExW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
; hKey : HKEY -> "intptr"
; lpSubKey : LPCWSTR -> "wstr"
; Reserved : DWORD optional -> "int"
; lpClass : LPWSTR optional -> "wstr"
; dwOptions : REG_OPEN_CREATE_OPTIONS -> "int"
; samDesired : REG_SAM_FLAGS -> "int"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; phkResult : HKEY* out -> "intptr"
; lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRegCreateKeyExW = advapi32.NewProc("RegCreateKeyExW")
)

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

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

typedef RegCreateKeyExWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Uint32, Pointer<Utf16>, Uint32, Uint32, Pointer<Void>, Pointer<Void>, Pointer<Uint32>);
typedef RegCreateKeyExWDart = int Function(Pointer<Void>, Pointer<Utf16>, int, Pointer<Utf16>, int, int, Pointer<Void>, Pointer<Void>, Pointer<Uint32>);
final RegCreateKeyExW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<RegCreateKeyExWNative, RegCreateKeyExWDart>('RegCreateKeyExW');
// hKey : HKEY -> Pointer<Void>
// lpSubKey : LPCWSTR -> Pointer<Utf16>
// Reserved : DWORD optional -> Uint32
// lpClass : LPWSTR optional -> Pointer<Utf16>
// dwOptions : REG_OPEN_CREATE_OPTIONS -> Uint32
// samDesired : REG_SAM_FLAGS -> Uint32
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Pointer<Void>
// phkResult : HKEY* out -> Pointer<Void>
// lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function RegCreateKeyExW(
  hKey: THandle;   // HKEY
  lpSubKey: PWideChar;   // LPCWSTR
  Reserved: DWORD;   // DWORD optional
  lpClass: PWideChar;   // LPWSTR optional
  dwOptions: DWORD;   // REG_OPEN_CREATE_OPTIONS
  samDesired: DWORD;   // REG_SAM_FLAGS
  lpSecurityAttributes: Pointer;   // SECURITY_ATTRIBUTES* optional
  phkResult: Pointer;   // HKEY* out
  lpdwDisposition: Pointer   // REG_CREATE_KEY_DISPOSITION* optional, out
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegCreateKeyExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RegCreateKeyExW"
  c_RegCreateKeyExW :: Ptr () -> CWString -> Word32 -> CWString -> Word32 -> Word32 -> Ptr () -> Ptr () -> Ptr Word32 -> IO Word32
-- hKey : HKEY -> Ptr ()
-- lpSubKey : LPCWSTR -> CWString
-- Reserved : DWORD optional -> Word32
-- lpClass : LPWSTR optional -> CWString
-- dwOptions : REG_OPEN_CREATE_OPTIONS -> Word32
-- samDesired : REG_SAM_FLAGS -> Word32
-- lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Ptr ()
-- phkResult : HKEY* out -> Ptr ()
-- lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let regcreatekeyexw =
  foreign "RegCreateKeyExW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> uint32_t @-> (ptr void) @-> (ptr void) @-> (ptr uint32_t) @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* lpSubKey : LPCWSTR -> (ptr uint16_t) *)
(* Reserved : DWORD optional -> uint32_t *)
(* lpClass : LPWSTR optional -> (ptr uint16_t) *)
(* dwOptions : REG_OPEN_CREATE_OPTIONS -> uint32_t *)
(* samDesired : REG_SAM_FLAGS -> uint32_t *)
(* lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> (ptr void) *)
(* phkResult : HKEY* out -> (ptr void) *)
(* lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("RegCreateKeyExW" reg-create-key-ex-w :convention :stdcall) :uint32
  (h-key :pointer)   ; HKEY
  (lp-sub-key (:string :encoding :utf-16le))   ; LPCWSTR
  (reserved :uint32)   ; DWORD optional
  (lp-class (:string :encoding :utf-16le))   ; LPWSTR optional
  (dw-options :uint32)   ; REG_OPEN_CREATE_OPTIONS
  (sam-desired :uint32)   ; REG_SAM_FLAGS
  (lp-security-attributes :pointer)   ; SECURITY_ATTRIBUTES* optional
  (phk-result :pointer)   ; HKEY* out
  (lpdw-disposition :pointer))   ; REG_CREATE_KEY_DISPOSITION* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RegCreateKeyExW = Win32::API::More->new('ADVAPI32',
    'DWORD RegCreateKeyExW(HANDLE hKey, LPCWSTR lpSubKey, DWORD Reserved, LPCWSTR lpClass, DWORD dwOptions, DWORD samDesired, LPVOID lpSecurityAttributes, HANDLE phkResult, LPVOID lpdwDisposition)');
# my $ret = $RegCreateKeyExW->Call($hKey, $lpSubKey, $Reserved, $lpClass, $dwOptions, $samDesired, $lpSecurityAttributes, $phkResult, $lpdwDisposition);
# hKey : HKEY -> HANDLE
# lpSubKey : LPCWSTR -> LPCWSTR
# Reserved : DWORD optional -> DWORD
# lpClass : LPWSTR optional -> LPCWSTR
# dwOptions : REG_OPEN_CREATE_OPTIONS -> DWORD
# samDesired : REG_SAM_FLAGS -> DWORD
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> LPVOID
# phkResult : HKEY* out -> HANDLE
# lpdwDisposition : REG_CREATE_KEY_DISPOSITION* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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