RegSaveKeyA
関数シグネチャ
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR RegSaveKeyA(
HKEY hKey,
LPCSTR lpFile,
const SECURITY_ATTRIBUTES* lpSecurityAttributes // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | HKEY | in | オープンされているレジストリ キーへのハンドル。 このハンドルは RegCreateKeyEx または RegOpenKeyEx 関数によって返されます。または、次のいずれかの 定義済みキー を指定することもできます。 |
| lpFile | LPCSTR | in | 指定したキーとサブキーの保存先となるファイルの名前。ファイルが既に存在する場合、この関数は失敗します。 文字列にパスが含まれていない場合、ローカル キーではこのファイルは呼び出し元プロセスのカレント ディレクトリに作成され、リモート キーでは %systemroot%\system32 ディレクトリに作成されます。新しいファイルにはアーカイブ属性が設定されます。 |
| lpSecurityAttributes | SECURITY_ATTRIBUTES* | inoptional | 新しいファイルのセキュリティ記述子を指定する SECURITY_ATTRIBUTES 構造体へのポインター。lpSecurityAttributes が NULL の場合、ファイルには既定のセキュリティ記述子が設定されます。ファイルの既定のセキュリティ記述子内の ACL は、その親ディレクトリから継承されます。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
指定したキーとそのすべてのサブキーおよび値を、標準形式で新しいファイルに保存します。(ANSI)
戻り値
関数が成功した場合、戻り値は 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 特権が有効になっている必要があります。詳細については、 特殊な特権での実行 を参照してください。
winreg.h ヘッダーは RegSaveKey を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在させて使用すると、不一致が生じ、コンパイル エラーまたは実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則 を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR RegSaveKeyA(
HKEY hKey,
LPCSTR lpFile,
const SECURITY_ATTRIBUTES* lpSecurityAttributes // optional
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint RegSaveKeyA(
IntPtr hKey, // HKEY
[MarshalAs(UnmanagedType.LPStr)] string lpFile, // LPCSTR
IntPtr lpSecurityAttributes // SECURITY_ATTRIBUTES* optional
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function RegSaveKeyA(
hKey As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPStr)> lpFile As String, ' LPCSTR
lpSecurityAttributes As IntPtr ' SECURITY_ATTRIBUTES* optional
) As UInteger
End Function' hKey : HKEY
' lpFile : LPCSTR
' lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
Declare PtrSafe Function RegSaveKeyA Lib "advapi32" ( _
ByVal hKey As LongPtr, _
ByVal lpFile As String, _
ByVal lpSecurityAttributes As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RegSaveKeyA = ctypes.windll.advapi32.RegSaveKeyA
RegSaveKeyA.restype = wintypes.DWORD
RegSaveKeyA.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.LPCSTR, # lpFile : LPCSTR
ctypes.c_void_p, # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
RegSaveKeyA = Fiddle::Function.new(
lib['RegSaveKeyA'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
Fiddle::TYPE_VOIDP, # lpFile : LPCSTR
Fiddle::TYPE_VOIDP, # lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
],
-Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn RegSaveKeyA(
hKey: *mut core::ffi::c_void, // HKEY
lpFile: *const u8, // LPCSTR
lpSecurityAttributes: *const SECURITY_ATTRIBUTES // SECURITY_ATTRIBUTES* optional
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi)]
public static extern uint RegSaveKeyA(IntPtr hKey, [MarshalAs(UnmanagedType.LPStr)] string lpFile, IntPtr lpSecurityAttributes);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegSaveKeyA' -Namespace Win32 -PassThru
# $api::RegSaveKeyA(hKey, lpFile, lpSecurityAttributes)#uselib "ADVAPI32.dll"
#func global RegSaveKeyA "RegSaveKeyA" sptr, sptr, sptr
; RegSaveKeyA hKey, lpFile, varptr(lpSecurityAttributes) ; 戻り値は stat
; hKey : HKEY -> "sptr"
; lpFile : LPCSTR -> "sptr"
; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll" #cfunc global RegSaveKeyA "RegSaveKeyA" sptr, str, var ; res = RegSaveKeyA(hKey, lpFile, lpSecurityAttributes) ; hKey : HKEY -> "sptr" ; lpFile : LPCSTR -> "str" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global RegSaveKeyA "RegSaveKeyA" sptr, str, sptr ; res = RegSaveKeyA(hKey, lpFile, varptr(lpSecurityAttributes)) ; hKey : HKEY -> "sptr" ; lpFile : LPCSTR -> "str" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; WIN32_ERROR RegSaveKeyA(HKEY hKey, LPCSTR lpFile, SECURITY_ATTRIBUTES* lpSecurityAttributes) #uselib "ADVAPI32.dll" #cfunc global RegSaveKeyA "RegSaveKeyA" intptr, str, var ; res = RegSaveKeyA(hKey, lpFile, lpSecurityAttributes) ; hKey : HKEY -> "intptr" ; lpFile : LPCSTR -> "str" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; WIN32_ERROR RegSaveKeyA(HKEY hKey, LPCSTR lpFile, SECURITY_ATTRIBUTES* lpSecurityAttributes) #uselib "ADVAPI32.dll" #cfunc global RegSaveKeyA "RegSaveKeyA" intptr, str, intptr ; res = RegSaveKeyA(hKey, lpFile, varptr(lpSecurityAttributes)) ; hKey : HKEY -> "intptr" ; lpFile : LPCSTR -> "str" ; lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRegSaveKeyA = advapi32.NewProc("RegSaveKeyA")
)
// hKey (HKEY), lpFile (LPCSTR), lpSecurityAttributes (SECURITY_ATTRIBUTES* optional)
r1, _, err := procRegSaveKeyA.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFile))),
uintptr(lpSecurityAttributes),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction RegSaveKeyA(
hKey: THandle; // HKEY
lpFile: PAnsiChar; // LPCSTR
lpSecurityAttributes: Pointer // SECURITY_ATTRIBUTES* optional
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegSaveKeyA';result := DllCall("ADVAPI32\RegSaveKeyA"
, "Ptr", hKey ; HKEY
, "AStr", lpFile ; LPCSTR
, "Ptr", lpSecurityAttributes ; SECURITY_ATTRIBUTES* optional
, "UInt") ; return: WIN32_ERROR●RegSaveKeyA(hKey, lpFile, lpSecurityAttributes) = DLL("ADVAPI32.dll", "dword RegSaveKeyA(void*, char*, void*)")
# 呼び出し: RegSaveKeyA(hKey, lpFile, lpSecurityAttributes)
# hKey : HKEY -> "void*"
# lpFile : LPCSTR -> "char*"
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn RegSaveKeyA(
hKey: ?*anyopaque, // HKEY
lpFile: [*c]const u8, // LPCSTR
lpSecurityAttributes: [*c]SECURITY_ATTRIBUTES // SECURITY_ATTRIBUTES* optional
) callconv(std.os.windows.WINAPI) u32;proc RegSaveKeyA(
hKey: pointer, # HKEY
lpFile: cstring, # LPCSTR
lpSecurityAttributes: ptr SECURITY_ATTRIBUTES # SECURITY_ATTRIBUTES* optional
): uint32 {.importc: "RegSaveKeyA", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
uint RegSaveKeyA(
void* hKey, // HKEY
const(char)* lpFile, // LPCSTR
SECURITY_ATTRIBUTES* lpSecurityAttributes // SECURITY_ATTRIBUTES* optional
);ccall((:RegSaveKeyA, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cstring, Ptr{SECURITY_ATTRIBUTES}),
hKey, lpFile, lpSecurityAttributes)
# hKey : HKEY -> Ptr{Cvoid}
# lpFile : LPCSTR -> Cstring
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Ptr{SECURITY_ATTRIBUTES}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t RegSaveKeyA(
void* hKey,
const char* lpFile,
void* lpSecurityAttributes);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegSaveKeyA(hKey, lpFile, lpSecurityAttributes)
-- hKey : HKEY
-- lpFile : LPCSTR
-- lpSecurityAttributes : SECURITY_ATTRIBUTES* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RegSaveKeyA = lib.func('__stdcall', 'RegSaveKeyA', 'uint32_t', ['void *', 'str', 'void *']);
// RegSaveKeyA(hKey, lpFile, lpSecurityAttributes)
// hKey : HKEY -> 'void *'
// lpFile : LPCSTR -> 'str'
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
RegSaveKeyA: { parameters: ["pointer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.RegSaveKeyA(hKey, lpFile, lpSecurityAttributes)
// hKey : HKEY -> "pointer"
// lpFile : LPCSTR -> "buffer"
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t RegSaveKeyA(
void* hKey,
const char* lpFile,
void* lpSecurityAttributes);
C, "ADVAPI32.dll");
// $ffi->RegSaveKeyA(hKey, lpFile, lpSecurityAttributes);
// hKey : HKEY
// lpFile : LPCSTR
// 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.ASCII_OPTIONS);
int RegSaveKeyA(
Pointer hKey, // HKEY
String lpFile, // LPCSTR
Pointer lpSecurityAttributes // SECURITY_ATTRIBUTES* optional
);
}@[Link("advapi32")]
lib LibADVAPI32
fun RegSaveKeyA = RegSaveKeyA(
hKey : Void*, # HKEY
lpFile : UInt8*, # LPCSTR
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 RegSaveKeyANative = Uint32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Void>);
typedef RegSaveKeyADart = int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Void>);
final RegSaveKeyA = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RegSaveKeyANative, RegSaveKeyADart>('RegSaveKeyA');
// hKey : HKEY -> Pointer<Void>
// lpFile : LPCSTR -> Pointer<Utf8>
// lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegSaveKeyA(
hKey: THandle; // HKEY
lpFile: PAnsiChar; // LPCSTR
lpSecurityAttributes: Pointer // SECURITY_ATTRIBUTES* optional
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegSaveKeyA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegSaveKeyA"
c_RegSaveKeyA :: Ptr () -> CString -> Ptr () -> IO Word32
-- hKey : HKEY -> Ptr ()
-- lpFile : LPCSTR -> CString
-- lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let regsavekeya =
foreign "RegSaveKeyA"
((ptr void) @-> string @-> (ptr void) @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* lpFile : LPCSTR -> string *)
(* 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 ("RegSaveKeyA" reg-save-key-a :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(lp-file :string) ; LPCSTR
(lp-security-attributes :pointer)) ; SECURITY_ATTRIBUTES* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegSaveKeyA = Win32::API::More->new('ADVAPI32',
'DWORD RegSaveKeyA(HANDLE hKey, LPCSTR lpFile, LPVOID lpSecurityAttributes)');
# my $ret = $RegSaveKeyA->Call($hKey, $lpFile, $lpSecurityAttributes);
# hKey : HKEY -> HANDLE
# lpFile : LPCSTR -> LPCSTR
# lpSecurityAttributes : SECURITY_ATTRIBUTES* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f RegSaveKeyW (Unicode版) — 指定キーとサブキーをファイルに保存する。
- f RegSaveKeyExA — 形式を指定して指定キーをファイルに保存する。
- f RegCreateKeyExA — オプションやアクセス権を指定してレジストリキーを作成または開く。
- f RegDeleteKeyA — 指定したサブキーをレジストリから削除する。
- f RegLoadKeyA — ハイブファイルをレジストリのサブキーとして読み込む。
- f RegReplaceKeyA — 指定キーのバックアップ用ファイルを置き換える。
- f RegRestoreKeyA — 保存済みハイブファイルから指定キーを復元する。