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

RegCreateKeyW

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

シグネチャ

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

WIN32_ERROR RegCreateKeyW(
    HKEY hKey,
    LPCWSTR lpSubKey,   // optional
    HKEY* phkResult
);

パラメーター

名前方向説明
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
lpSubKeyLPCWSTRinoptional

この関数が開くまたは作成するキーの名前。このキーは、hKey パラメーターで識別されるキーのサブキーである必要があります。

キー名の詳細については、Structure of the Registry を参照してください。

hKey が定義済みキーのいずれかである場合、lpSubKeyNULL にできます。その場合、phkResult は関数に渡された hKey ハンドルと同じハンドルを受け取ります。

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

戻り値の型: WIN32_ERROR

公式ドキュメント

指定されたレジストリキーを作成します。キーがレジストリに既に存在する場合、この関数はそのキーを開きます。(Unicode)

戻り値

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

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

解説(Remarks)

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

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

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

subkey1\subkey2\subkey3\subkey4

なお、この動作により、パス内の既存のキーのスペルが誤っている場合に、不要なキーが作成される結果になることに注意してください。

メモ

winreg.h ヘッダーは、RegCreateKey を、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 RegCreateKeyW(
    HKEY hKey,
    LPCWSTR lpSubKey,   // optional
    HKEY* phkResult
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegCreateKeyW(
    IntPtr hKey,   // HKEY
    [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey,   // LPCWSTR optional
    IntPtr phkResult   // HKEY* out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegCreateKeyW(
    hKey As IntPtr,   ' HKEY
    <MarshalAs(UnmanagedType.LPWStr)> lpSubKey As String,   ' LPCWSTR optional
    phkResult As IntPtr   ' HKEY* out
) As UInteger
End Function
' hKey : HKEY
' lpSubKey : LPCWSTR optional
' phkResult : HKEY* out
Declare PtrSafe Function RegCreateKeyW Lib "advapi32" ( _
    ByVal hKey As LongPtr, _
    ByVal lpSubKey As LongPtr, _
    ByVal phkResult 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

RegCreateKeyW = ctypes.windll.advapi32.RegCreateKeyW
RegCreateKeyW.restype = wintypes.DWORD
RegCreateKeyW.argtypes = [
    wintypes.HANDLE,  # hKey : HKEY
    wintypes.LPCWSTR,  # lpSubKey : LPCWSTR optional
    ctypes.c_void_p,  # phkResult : HKEY* out
]
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRegCreateKeyW = advapi32.NewProc("RegCreateKeyW")
)

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

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

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

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

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

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

関連項目

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