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
lpSubKeyLPCWSTRin
ReservedDWORDoptional
lpClassLPWSTRinoptional
dwOptionsREG_OPEN_CREATE_OPTIONSin
samDesiredREG_SAM_FLAGSin
lpSecurityAttributesSECURITY_ATTRIBUTES*inoptional
phkResultHKEY*out
lpdwDispositionREG_CREATE_KEY_DISPOSITION*outoptional

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

// 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, 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, int
; 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 -> "int"
; ※出力/バッファ引数は 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, int
; 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 -> "int"
; ※出力/バッファ引数は 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 版の利用を推奨。