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

RegCreateKeyTransactedW

関数
トランザクション処理でレジストリキーを作成または開く。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

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

WIN32_ERROR RegCreateKeyTransactedW(
    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
    HANDLE hTransaction,
    void* pExtendedParemeter   // optional
);

パラメーター

名前方向説明
hKeyHKEYin

開いているレジストリキーへのハンドル。呼び出し元プロセスは、このキーに対する KEY_CREATE_SUB_KEY アクセス権を持っている必要があります。詳細については、 レジストリキーのセキュリティとアクセス権を参照してください。

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

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

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
lpSubKeyLPCWSTRin

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

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

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

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

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

意味
REG_OPTION_BACKUP_RESTORE
0x00000004L
このフラグが設定されている場合、関数は samDesired パラメーターを無視し、キーのバックアップまたは復元に必要なアクセス権でキーを開こうとします。呼び出し元スレッドで SE_BACKUP_NAME 特権が有効になっている場合、キーは ACCESS_SYSTEM_SECURITY および KEY_READ アクセス権で開かれます。呼び出し元スレッドで SE_RESTORE_NAME 特権が有効になっている場合、キーは ACCESS_SYSTEM_SECURITY および KEY_WRITE アクセス権で開かれます。両方の特権が有効になっている場合、キーは両方の特権を組み合わせたアクセス権を持ちます。詳細については、 特別な特権での実行を参照してください。
REG_OPTION_NON_VOLATILE
0x00000000L
このキーは揮発性ではありません。これが既定値です。情報はファイルに保存され、システムの再起動後も保持されます。 RegSaveKey 関数は、揮発性でないキーを保存します。
REG_OPTION_VOLATILE
0x00000001L
関数によって作成されるすべてのキーは揮発性です。情報はメモリに保存され、対応するレジストリハイブがアンロードされると保持されません。HKEY_LOCAL_MACHINE の場合、これはシステムのシャットダウン時に発生します。 RegLoadKey 関数によって読み込まれたレジストリキーの場合、これは対応する RegUnLoadKey が実行されたときに発生します。 RegSaveKey 関数は揮発性キーを保存しません。このフラグは、既に存在するキーに対しては無視されます。
samDesiredREG_SAM_FLAGSin作成するキーのアクセス権を指定するマスク。詳細については、 レジストリキーのセキュリティとアクセス権を参照してください。
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 の場合、処理結果の情報は返されません。

hTransactionHANDLEinアクティブなトランザクションへのハンドル。このハンドルは CreateTransaction 関数によって返されます。
pExtendedParemetervoid*optionalこのパラメーターは予約されており、NULL にする必要があります。

戻り値の型: WIN32_ERROR

公式ドキュメント

指定したレジストリキーを作成し、トランザクションに関連付けます。(Unicode)

戻り値

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

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

解説(Remarks)

この関数を使用してキーを作成すると、そのキーに対する以降の操作はトランザクション処理されます。トランザクションがコミットされる前にキーに対して非トランザクションの操作が実行されると、トランザクションはロールバックされます。トランザクションがコミットまたはロールバックされた後、追加の操作をトランザクション処理するには、アクティブなトランザクションハンドルを指定して RegCreateKeyTransacted または RegOpenKeyTransacted でキーを開き直す必要があります。トランザクションの詳細については、カーネルトランザクションマネージャーを参照してください。

このキーのサブキーに対する以降の操作は、自動的にはトランザクション処理されない点に注意してください。したがって、RegDeleteKeyEx はトランザクション削除操作を実行しません。代わりに、RegDeleteKeyTransacted 関数を使用してトランザクション削除操作を実行してください。

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

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

subkey1\subkey2\subkey3\subkey4

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

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

メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 RegCreateKeyTransactedW(
    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
    HANDLE hTransaction,
    void* pExtendedParemeter   // optional
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegCreateKeyTransactedW(
    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
    IntPtr hTransaction,   // HANDLE
    IntPtr pExtendedParemeter   // void* optional
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegCreateKeyTransactedW(
    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
    hTransaction As IntPtr,   ' HANDLE
    pExtendedParemeter As IntPtr   ' void* optional
) 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
' hTransaction : HANDLE
' pExtendedParemeter : void* optional
Declare PtrSafe Function RegCreateKeyTransactedW 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, _
    ByVal hTransaction As LongPtr, _
    ByVal pExtendedParemeter 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

RegCreateKeyTransactedW = ctypes.windll.advapi32.RegCreateKeyTransactedW
RegCreateKeyTransactedW.restype = wintypes.DWORD
RegCreateKeyTransactedW.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
    wintypes.HANDLE,  # hTransaction : HANDLE
    ctypes.POINTER(None),  # pExtendedParemeter : void* optional
]
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRegCreateKeyTransactedW = advapi32.NewProc("RegCreateKeyTransactedW")
)

// 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), hTransaction (HANDLE), pExtendedParemeter (void* optional)
r1, _, err := procRegCreateKeyTransactedW.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),
	uintptr(hTransaction),
	uintptr(pExtendedParemeter),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function RegCreateKeyTransactedW(
  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
  hTransaction: THandle;   // HANDLE
  pExtendedParemeter: Pointer   // void* optional
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegCreateKeyTransactedW';
result := DllCall("ADVAPI32\RegCreateKeyTransactedW"
    , "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
    , "Ptr", hTransaction   ; HANDLE
    , "Ptr", pExtendedParemeter   ; void* optional
    , "UInt")   ; return: WIN32_ERROR
●RegCreateKeyTransactedW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition, hTransaction, pExtendedParemeter) = DLL("ADVAPI32.dll", "dword RegCreateKeyTransactedW(void*, char*, dword, char*, dword, dword, void*, void*, void*, void*, void*)")
# 呼び出し: RegCreateKeyTransactedW(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition, hTransaction, pExtendedParemeter)
# 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*"
# hTransaction : HANDLE -> "void*"
# pExtendedParemeter : void* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef RegCreateKeyTransactedWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Uint32, Pointer<Utf16>, Uint32, Uint32, Pointer<Void>, Pointer<Void>, Pointer<Uint32>, Pointer<Void>, Pointer<Void>);
typedef RegCreateKeyTransactedWDart = int Function(Pointer<Void>, Pointer<Utf16>, int, Pointer<Utf16>, int, int, Pointer<Void>, Pointer<Void>, Pointer<Uint32>, Pointer<Void>, Pointer<Void>);
final RegCreateKeyTransactedW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<RegCreateKeyTransactedWNative, RegCreateKeyTransactedWDart>('RegCreateKeyTransactedW');
// 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>
// hTransaction : HANDLE -> Pointer<Void>
// pExtendedParemeter : void* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function RegCreateKeyTransactedW(
  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
  hTransaction: THandle;   // HANDLE
  pExtendedParemeter: Pointer   // void* optional
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegCreateKeyTransactedW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RegCreateKeyTransactedW"
  c_RegCreateKeyTransactedW :: Ptr () -> CWString -> Word32 -> CWString -> Word32 -> Word32 -> Ptr () -> Ptr () -> Ptr Word32 -> Ptr () -> Ptr () -> 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
-- hTransaction : HANDLE -> Ptr ()
-- pExtendedParemeter : void* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let regcreatekeytransactedw =
  foreign "RegCreateKeyTransactedW"
    ((ptr void) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> uint32_t @-> (ptr void) @-> (ptr void) @-> (ptr uint32_t) @-> (ptr void) @-> (ptr void) @-> 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) *)
(* hTransaction : HANDLE -> (ptr void) *)
(* pExtendedParemeter : void* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("RegCreateKeyTransactedW" reg-create-key-transacted-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
  (h-transaction :pointer)   ; HANDLE
  (p-extended-paremeter :pointer))   ; void* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RegCreateKeyTransactedW = Win32::API::More->new('ADVAPI32',
    'DWORD RegCreateKeyTransactedW(HANDLE hKey, LPCWSTR lpSubKey, DWORD Reserved, LPCWSTR lpClass, DWORD dwOptions, DWORD samDesired, LPVOID lpSecurityAttributes, HANDLE phkResult, LPVOID lpdwDisposition, HANDLE hTransaction, LPVOID pExtendedParemeter)');
# my $ret = $RegCreateKeyTransactedW->Call($hKey, $lpSubKey, $Reserved, $lpClass, $dwOptions, $samDesired, $lpSecurityAttributes, $phkResult, $lpdwDisposition, $hTransaction, $pExtendedParemeter);
# 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
# hTransaction : HANDLE -> HANDLE
# pExtendedParemeter : void* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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