RegCreateKeyA
関数シグネチャ
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR RegCreateKeyA(
HKEY hKey,
LPCSTR lpSubKey, // optional
HKEY* phkResult
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | HKEY | in | 開かれているレジストリキーへのハンドル。呼び出し元プロセスは、このキーに対する KEY_CREATE_SUB_KEY アクセス権を持っている必要があります。詳細については、 Registry Key Security and Access Rights を参照してください。 キー作成のアクセス権は、ハンドル取得時に指定したアクセスマスクではなく、レジストリキーのセキュリティ記述子に対して確認されます。したがって、hKey が KEY_READ の samDesired で開かれていても、そのセキュリティ記述子で許可されていれば、キーを作成する操作に使用できます。 このハンドルは、 RegCreateKeyEx 関数または RegOpenKeyEx 関数によって返されます。または、次の 定義済みキー のいずれかを指定できます。 |
| lpSubKey | LPCSTR | inoptional | この関数が開くまたは作成するキーの名前。このキーは、hKey パラメーターで識別されるキーのサブキーである必要があります。 キー名の詳細については、Structure of the Registry を参照してください。 hKey が定義済みキーのいずれかである場合、lpSubKey に NULL を指定できます。その場合、phkResult は関数に渡されたものと同じ hKey ハンドルを受け取ります。 |
| phkResult | HKEY* | out | 開かれたまたは作成されたキーへのハンドルを受け取る変数へのポインター。キーが定義済みレジストリキーのいずれでもない場合は、ハンドルの使用を終えた後に RegCloseKey 関数を呼び出してください。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
指定したレジストリキーを作成します。キーが既にレジストリに存在する場合、この関数はそれを開きます。(ANSI)
戻り値
関数が成功した場合、戻り値は ERROR_SUCCESS です。
関数が失敗した場合、戻り値は Winerror.h で定義されている 0 以外のエラーコードです。FORMAT_MESSAGE_FROM_SYSTEM フラグを指定して FormatMessage 関数を使用すると、エラーの一般的な説明を取得できます。
解説(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 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR RegCreateKeyA(
HKEY hKey,
LPCSTR lpSubKey, // optional
HKEY* phkResult
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint RegCreateKeyA(
IntPtr hKey, // HKEY
[MarshalAs(UnmanagedType.LPStr)] string lpSubKey, // LPCSTR optional
IntPtr phkResult // HKEY* out
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function RegCreateKeyA(
hKey As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPStr)> lpSubKey As String, ' LPCSTR optional
phkResult As IntPtr ' HKEY* out
) As UInteger
End Function' hKey : HKEY
' lpSubKey : LPCSTR optional
' phkResult : HKEY* out
Declare PtrSafe Function RegCreateKeyA Lib "advapi32" ( _
ByVal hKey As LongPtr, _
ByVal lpSubKey As String, _
ByVal phkResult As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RegCreateKeyA = ctypes.windll.advapi32.RegCreateKeyA
RegCreateKeyA.restype = wintypes.DWORD
RegCreateKeyA.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.LPCSTR, # lpSubKey : LPCSTR optional
ctypes.c_void_p, # phkResult : HKEY* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
RegCreateKeyA = Fiddle::Function.new(
lib['RegCreateKeyA'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
Fiddle::TYPE_VOIDP, # lpSubKey : LPCSTR optional
Fiddle::TYPE_VOIDP, # phkResult : HKEY* out
],
-Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn RegCreateKeyA(
hKey: *mut core::ffi::c_void, // HKEY
lpSubKey: *const u8, // LPCSTR 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.Ansi)]
public static extern uint RegCreateKeyA(IntPtr hKey, [MarshalAs(UnmanagedType.LPStr)] string lpSubKey, IntPtr phkResult);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegCreateKeyA' -Namespace Win32 -PassThru
# $api::RegCreateKeyA(hKey, lpSubKey, phkResult)#uselib "ADVAPI32.dll"
#func global RegCreateKeyA "RegCreateKeyA" sptr, sptr, sptr
; RegCreateKeyA hKey, lpSubKey, phkResult ; 戻り値は stat
; hKey : HKEY -> "sptr"
; lpSubKey : LPCSTR optional -> "sptr"
; phkResult : HKEY* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global RegCreateKeyA "RegCreateKeyA" sptr, str, sptr
; res = RegCreateKeyA(hKey, lpSubKey, phkResult)
; hKey : HKEY -> "sptr"
; lpSubKey : LPCSTR optional -> "str"
; phkResult : HKEY* out -> "sptr"; WIN32_ERROR RegCreateKeyA(HKEY hKey, LPCSTR lpSubKey, HKEY* phkResult)
#uselib "ADVAPI32.dll"
#cfunc global RegCreateKeyA "RegCreateKeyA" intptr, str, intptr
; res = RegCreateKeyA(hKey, lpSubKey, phkResult)
; hKey : HKEY -> "intptr"
; lpSubKey : LPCSTR optional -> "str"
; phkResult : HKEY* out -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRegCreateKeyA = advapi32.NewProc("RegCreateKeyA")
)
// hKey (HKEY), lpSubKey (LPCSTR optional), phkResult (HKEY* out)
r1, _, err := procRegCreateKeyA.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpSubKey))),
uintptr(phkResult),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction RegCreateKeyA(
hKey: THandle; // HKEY
lpSubKey: PAnsiChar; // LPCSTR optional
phkResult: Pointer // HKEY* out
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegCreateKeyA';result := DllCall("ADVAPI32\RegCreateKeyA"
, "Ptr", hKey ; HKEY
, "AStr", lpSubKey ; LPCSTR optional
, "Ptr", phkResult ; HKEY* out
, "UInt") ; return: WIN32_ERROR●RegCreateKeyA(hKey, lpSubKey, phkResult) = DLL("ADVAPI32.dll", "dword RegCreateKeyA(void*, char*, void*)")
# 呼び出し: RegCreateKeyA(hKey, lpSubKey, phkResult)
# hKey : HKEY -> "void*"
# lpSubKey : LPCSTR optional -> "char*"
# phkResult : HKEY* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn RegCreateKeyA(
hKey: ?*anyopaque, // HKEY
lpSubKey: [*c]const u8, // LPCSTR optional
phkResult: ?*anyopaque // HKEY* out
) callconv(std.os.windows.WINAPI) u32;proc RegCreateKeyA(
hKey: pointer, # HKEY
lpSubKey: cstring, # LPCSTR optional
phkResult: pointer # HKEY* out
): uint32 {.importc: "RegCreateKeyA", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
uint RegCreateKeyA(
void* hKey, // HKEY
const(char)* lpSubKey, // LPCSTR optional
void* phkResult // HKEY* out
);ccall((:RegCreateKeyA, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cstring, Ptr{Cvoid}),
hKey, lpSubKey, phkResult)
# hKey : HKEY -> Ptr{Cvoid}
# lpSubKey : LPCSTR optional -> Cstring
# phkResult : HKEY* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t RegCreateKeyA(
void* hKey,
const char* lpSubKey,
void* phkResult);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegCreateKeyA(hKey, lpSubKey, phkResult)
-- hKey : HKEY
-- lpSubKey : LPCSTR optional
-- phkResult : HKEY* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RegCreateKeyA = lib.func('__stdcall', 'RegCreateKeyA', 'uint32_t', ['void *', 'str', 'void *']);
// RegCreateKeyA(hKey, lpSubKey, phkResult)
// hKey : HKEY -> 'void *'
// lpSubKey : LPCSTR optional -> 'str'
// phkResult : HKEY* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
RegCreateKeyA: { parameters: ["pointer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.RegCreateKeyA(hKey, lpSubKey, phkResult)
// hKey : HKEY -> "pointer"
// lpSubKey : LPCSTR optional -> "buffer"
// phkResult : HKEY* out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t RegCreateKeyA(
void* hKey,
const char* lpSubKey,
void* phkResult);
C, "ADVAPI32.dll");
// $ffi->RegCreateKeyA(hKey, lpSubKey, phkResult);
// hKey : HKEY
// lpSubKey : LPCSTR 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.ASCII_OPTIONS);
int RegCreateKeyA(
Pointer hKey, // HKEY
String lpSubKey, // LPCSTR optional
Pointer phkResult // HKEY* out
);
}@[Link("advapi32")]
lib LibADVAPI32
fun RegCreateKeyA = RegCreateKeyA(
hKey : Void*, # HKEY
lpSubKey : UInt8*, # LPCSTR 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 RegCreateKeyANative = Uint32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Void>);
typedef RegCreateKeyADart = int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Void>);
final RegCreateKeyA = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RegCreateKeyANative, RegCreateKeyADart>('RegCreateKeyA');
// hKey : HKEY -> Pointer<Void>
// lpSubKey : LPCSTR optional -> Pointer<Utf8>
// phkResult : HKEY* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegCreateKeyA(
hKey: THandle; // HKEY
lpSubKey: PAnsiChar; // LPCSTR optional
phkResult: Pointer // HKEY* out
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegCreateKeyA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegCreateKeyA"
c_RegCreateKeyA :: Ptr () -> CString -> Ptr () -> IO Word32
-- hKey : HKEY -> Ptr ()
-- lpSubKey : LPCSTR optional -> CString
-- phkResult : HKEY* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let regcreatekeya =
foreign "RegCreateKeyA"
((ptr void) @-> string @-> (ptr void) @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* lpSubKey : LPCSTR optional -> string *)
(* 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 ("RegCreateKeyA" reg-create-key-a :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(lp-sub-key :string) ; LPCSTR optional
(phk-result :pointer)) ; HKEY* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegCreateKeyA = Win32::API::More->new('ADVAPI32',
'DWORD RegCreateKeyA(HANDLE hKey, LPCSTR lpSubKey, HANDLE phkResult)');
# my $ret = $RegCreateKeyA->Call($hKey, $lpSubKey, $phkResult);
# hKey : HKEY -> HANDLE
# lpSubKey : LPCSTR optional -> LPCSTR
# phkResult : HKEY* out -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f RegCreateKeyW (Unicode版) — 指定したレジストリキーを作成または開く。
- f RegCreateKeyExA — オプションやアクセス権を指定してレジストリキーを作成または開く。
- f RegCloseKey — 指定したレジストリキーのハンドルを閉じる。
- f RegDeleteKeyA — 指定したサブキーをレジストリから削除する。
- f RegOpenKeyExA — アクセス権を指定してレジストリキーを開く。