RegReplaceKeyW
関数シグネチャ
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR RegReplaceKeyW(
HKEY hKey,
LPCWSTR lpSubKey, // optional
LPCWSTR lpNewFile,
LPCWSTR lpOldFile
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | HKEY | in | オープンされたレジストリキーへのハンドル。このハンドルは RegCreateKeyEx または RegOpenKeyEx 関数によって返されます。あるいは、次のいずれかの 定義済みキー を指定することもできます。 HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS |
| lpSubKey | LPCWSTR | inoptional | サブキーと値を置き換える対象となるレジストリキーの名前。キーが存在する場合、それは hKey パラメーターで識別されるキーのサブキーである必要があります。サブキーが存在しない場合は作成されます。このパラメーターには NULL を指定できます。 指定したサブキーがハイブのルートでない場合、 RegReplaceKey はハイブのツリー構造をたどってハイブのルートに到達するまで上方向に進み、そのハイブの内容を lpNewFile で指定されたデータファイルの内容で置き換えます。 詳細については、 Registry Element Size Limits を参照してください。 |
| lpNewFile | LPCWSTR | in | レジストリ情報を含むファイルの名前。このファイルは通常、 RegSaveKey 関数を使用して作成されます。 |
| lpOldFile | LPCWSTR | in | 置き換えられるレジストリ情報のバックアップコピーを受け取るファイルの名前。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
レジストリキーとそのすべてのサブキーのバッキングファイルを別のファイルに置き換えます。これにより、次回システム起動時に、そのキーとサブキーは新しいファイルに格納された値を持つようになります。(Unicode)
戻り値
関数が成功した場合、戻り値は ERROR_SUCCESS です。
関数が失敗した場合、戻り値は Winerror.h で定義されている 0 以外のエラーコードです。 FormatMessage 関数を FORMAT_MESSAGE_FROM_SYSTEM フラグとともに使用すると、エラーの一般的な説明を取得できます。
解説(Remarks)
レジストリハイブファイルには 2 種類の異なる形式があります。現在のオペレーティングシステムで作成されたレジストリハイブは、通常、それより前のオペレーティングシステムでは読み込めません。
lpNewFile パラメーターで指定されたファイルは、システムが再起動されるまで開いたままになります。
hKey が RegConnectRegistry によって返されたハンドルである場合、lpNewFile および lpOldFile で指定されたパスはリモートコンピューターからの相対パスになります。
呼び出し元のプロセスは、レジストリが存在するコンピューター上で SE_RESTORE_NAME および SE_BACKUP_NAME 特権を持っている必要があります。詳細については、 Running with Special Privileges を参照してください。
winreg.h ヘッダーは RegReplaceKey を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不整合が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR RegReplaceKeyW(
HKEY hKey,
LPCWSTR lpSubKey, // optional
LPCWSTR lpNewFile,
LPCWSTR lpOldFile
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegReplaceKeyW(
IntPtr hKey, // HKEY
[MarshalAs(UnmanagedType.LPWStr)] string lpSubKey, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string lpNewFile, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string lpOldFile // LPCWSTR
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegReplaceKeyW(
hKey As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPWStr)> lpSubKey As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpNewFile As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> lpOldFile As String ' LPCWSTR
) As UInteger
End Function' hKey : HKEY
' lpSubKey : LPCWSTR optional
' lpNewFile : LPCWSTR
' lpOldFile : LPCWSTR
Declare PtrSafe Function RegReplaceKeyW Lib "advapi32" ( _
ByVal hKey As LongPtr, _
ByVal lpSubKey As LongPtr, _
ByVal lpNewFile As LongPtr, _
ByVal lpOldFile 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
RegReplaceKeyW = ctypes.windll.advapi32.RegReplaceKeyW
RegReplaceKeyW.restype = wintypes.DWORD
RegReplaceKeyW.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.LPCWSTR, # lpSubKey : LPCWSTR optional
wintypes.LPCWSTR, # lpNewFile : LPCWSTR
wintypes.LPCWSTR, # lpOldFile : LPCWSTR
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
RegReplaceKeyW = Fiddle::Function.new(
lib['RegReplaceKeyW'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
Fiddle::TYPE_VOIDP, # lpSubKey : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpNewFile : LPCWSTR
Fiddle::TYPE_VOIDP, # lpOldFile : LPCWSTR
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "advapi32")]
extern "system" {
fn RegReplaceKeyW(
hKey: *mut core::ffi::c_void, // HKEY
lpSubKey: *const u16, // LPCWSTR optional
lpNewFile: *const u16, // LPCWSTR
lpOldFile: *const u16 // LPCWSTR
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode)]
public static extern uint RegReplaceKeyW(IntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFile, [MarshalAs(UnmanagedType.LPWStr)] string lpOldFile);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegReplaceKeyW' -Namespace Win32 -PassThru
# $api::RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile)#uselib "ADVAPI32.dll"
#func global RegReplaceKeyW "RegReplaceKeyW" wptr, wptr, wptr, wptr
; RegReplaceKeyW hKey, lpSubKey, lpNewFile, lpOldFile ; 戻り値は stat
; hKey : HKEY -> "wptr"
; lpSubKey : LPCWSTR optional -> "wptr"
; lpNewFile : LPCWSTR -> "wptr"
; lpOldFile : LPCWSTR -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global RegReplaceKeyW "RegReplaceKeyW" sptr, wstr, wstr, wstr
; res = RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile)
; hKey : HKEY -> "sptr"
; lpSubKey : LPCWSTR optional -> "wstr"
; lpNewFile : LPCWSTR -> "wstr"
; lpOldFile : LPCWSTR -> "wstr"; WIN32_ERROR RegReplaceKeyW(HKEY hKey, LPCWSTR lpSubKey, LPCWSTR lpNewFile, LPCWSTR lpOldFile)
#uselib "ADVAPI32.dll"
#cfunc global RegReplaceKeyW "RegReplaceKeyW" intptr, wstr, wstr, wstr
; res = RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile)
; hKey : HKEY -> "intptr"
; lpSubKey : LPCWSTR optional -> "wstr"
; lpNewFile : LPCWSTR -> "wstr"
; lpOldFile : LPCWSTR -> "wstr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRegReplaceKeyW = advapi32.NewProc("RegReplaceKeyW")
)
// hKey (HKEY), lpSubKey (LPCWSTR optional), lpNewFile (LPCWSTR), lpOldFile (LPCWSTR)
r1, _, err := procRegReplaceKeyW.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpSubKey))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpNewFile))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpOldFile))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction RegReplaceKeyW(
hKey: THandle; // HKEY
lpSubKey: PWideChar; // LPCWSTR optional
lpNewFile: PWideChar; // LPCWSTR
lpOldFile: PWideChar // LPCWSTR
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegReplaceKeyW';result := DllCall("ADVAPI32\RegReplaceKeyW"
, "Ptr", hKey ; HKEY
, "WStr", lpSubKey ; LPCWSTR optional
, "WStr", lpNewFile ; LPCWSTR
, "WStr", lpOldFile ; LPCWSTR
, "UInt") ; return: WIN32_ERROR●RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile) = DLL("ADVAPI32.dll", "dword RegReplaceKeyW(void*, char*, char*, char*)")
# 呼び出し: RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile)
# hKey : HKEY -> "void*"
# lpSubKey : LPCWSTR optional -> "char*"
# lpNewFile : LPCWSTR -> "char*"
# lpOldFile : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "advapi32" fn RegReplaceKeyW(
hKey: ?*anyopaque, // HKEY
lpSubKey: [*c]const u16, // LPCWSTR optional
lpNewFile: [*c]const u16, // LPCWSTR
lpOldFile: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc RegReplaceKeyW(
hKey: pointer, # HKEY
lpSubKey: WideCString, # LPCWSTR optional
lpNewFile: WideCString, # LPCWSTR
lpOldFile: WideCString # LPCWSTR
): uint32 {.importc: "RegReplaceKeyW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "advapi32");
extern(Windows)
uint RegReplaceKeyW(
void* hKey, // HKEY
const(wchar)* lpSubKey, // LPCWSTR optional
const(wchar)* lpNewFile, // LPCWSTR
const(wchar)* lpOldFile // LPCWSTR
);ccall((:RegReplaceKeyW, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cwstring, Cwstring, Cwstring),
hKey, lpSubKey, lpNewFile, lpOldFile)
# hKey : HKEY -> Ptr{Cvoid}
# lpSubKey : LPCWSTR optional -> Cwstring
# lpNewFile : LPCWSTR -> Cwstring
# lpOldFile : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t RegReplaceKeyW(
void* hKey,
const uint16_t* lpSubKey,
const uint16_t* lpNewFile,
const uint16_t* lpOldFile);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile)
-- hKey : HKEY
-- lpSubKey : LPCWSTR optional
-- lpNewFile : LPCWSTR
-- lpOldFile : LPCWSTR
-- 構造体/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 RegReplaceKeyW = lib.func('__stdcall', 'RegReplaceKeyW', 'uint32_t', ['void *', 'str16', 'str16', 'str16']);
// RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile)
// hKey : HKEY -> 'void *'
// lpSubKey : LPCWSTR optional -> 'str16'
// lpNewFile : LPCWSTR -> 'str16'
// lpOldFile : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
RegReplaceKeyW: { parameters: ["pointer", "buffer", "buffer", "buffer"], result: "u32" },
});
// lib.symbols.RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile)
// hKey : HKEY -> "pointer"
// lpSubKey : LPCWSTR optional -> "buffer"
// lpNewFile : LPCWSTR -> "buffer"
// lpOldFile : LPCWSTR -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t RegReplaceKeyW(
void* hKey,
const uint16_t* lpSubKey,
const uint16_t* lpNewFile,
const uint16_t* lpOldFile);
C, "ADVAPI32.dll");
// $ffi->RegReplaceKeyW(hKey, lpSubKey, lpNewFile, lpOldFile);
// hKey : HKEY
// lpSubKey : LPCWSTR optional
// lpNewFile : LPCWSTR
// lpOldFile : LPCWSTR
// 構造体/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 RegReplaceKeyW(
Pointer hKey, // HKEY
WString lpSubKey, // LPCWSTR optional
WString lpNewFile, // LPCWSTR
WString lpOldFile // LPCWSTR
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("advapi32")]
lib LibADVAPI32
fun RegReplaceKeyW = RegReplaceKeyW(
hKey : Void*, # HKEY
lpSubKey : UInt16*, # LPCWSTR optional
lpNewFile : UInt16*, # LPCWSTR
lpOldFile : UInt16* # LPCWSTR
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef RegReplaceKeyWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
typedef RegReplaceKeyWDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>);
final RegReplaceKeyW = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RegReplaceKeyWNative, RegReplaceKeyWDart>('RegReplaceKeyW');
// hKey : HKEY -> Pointer<Void>
// lpSubKey : LPCWSTR optional -> Pointer<Utf16>
// lpNewFile : LPCWSTR -> Pointer<Utf16>
// lpOldFile : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegReplaceKeyW(
hKey: THandle; // HKEY
lpSubKey: PWideChar; // LPCWSTR optional
lpNewFile: PWideChar; // LPCWSTR
lpOldFile: PWideChar // LPCWSTR
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegReplaceKeyW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegReplaceKeyW"
c_RegReplaceKeyW :: Ptr () -> CWString -> CWString -> CWString -> IO Word32
-- hKey : HKEY -> Ptr ()
-- lpSubKey : LPCWSTR optional -> CWString
-- lpNewFile : LPCWSTR -> CWString
-- lpOldFile : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let regreplacekeyw =
foreign "RegReplaceKeyW"
((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* lpSubKey : LPCWSTR optional -> (ptr uint16_t) *)
(* lpNewFile : LPCWSTR -> (ptr uint16_t) *)
(* lpOldFile : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("RegReplaceKeyW" reg-replace-key-w :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(lp-sub-key (:string :encoding :utf-16le)) ; LPCWSTR optional
(lp-new-file (:string :encoding :utf-16le)) ; LPCWSTR
(lp-old-file (:string :encoding :utf-16le))) ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegReplaceKeyW = Win32::API::More->new('ADVAPI32',
'DWORD RegReplaceKeyW(HANDLE hKey, LPCWSTR lpSubKey, LPCWSTR lpNewFile, LPCWSTR lpOldFile)');
# my $ret = $RegReplaceKeyW->Call($hKey, $lpSubKey, $lpNewFile, $lpOldFile);
# hKey : HKEY -> HANDLE
# lpSubKey : LPCWSTR optional -> LPCWSTR
# lpNewFile : LPCWSTR -> LPCWSTR
# lpOldFile : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f RegReplaceKeyA (ANSI版) — 指定キーのバックアップ用ファイルを置き換える。
- f RegConnectRegistryW — リモートコンピューターの定義済みレジストリキーに接続する。
- f RegDeleteKeyW — 指定したサブキーをレジストリから削除する。
- f RegLoadKeyW — ハイブファイルをレジストリのサブキーとして読み込む。
- f RegRestoreKeyW — 保存済みハイブファイルから指定キーを復元する。
- f RegSaveKeyW — 指定キーとサブキーをファイルに保存する。