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