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

RegCopyTreeA

関数
指定キーのサブツリー全体を別のキーへコピーする。
DLLADVAPI32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

WIN32_ERROR RegCopyTreeA(
    HKEY hKeySrc,
    LPCSTR lpSubKey,   // optional
    HKEY hKeyDest
);

パラメーター

名前方向説明
hKeySrcHKEYin

開いているレジストリキーへのハンドル。このキーは KEY_READ アクセス権で開かれている必要があります。詳細については、 Registry Key Security and Access Rights を参照してください。

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

lpSubKeyLPCSTRinoptionalキーの名前。このキーは、hKeySrc パラメーターで指定されたキーのサブキーである必要があります。このパラメーターには NULL を指定することもできます。
hKeyDestHKEYin

宛先キーへのハンドル。呼び出し元プロセスは、そのキーに対する KEY_CREATE_SUB_KEY アクセス権を持っている必要があります。

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

戻り値の型: WIN32_ERROR

公式ドキュメント

指定したレジストリキーを、その値とサブキーとともに、指定した宛先キーへコピーします。(ANSI)

戻り値

関数が成功した場合、戻り値は ERROR_SUCCESS です。

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

解説(Remarks)

この関数は、キーのセキュリティ記述子もコピーします。

この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0600 以降として定義します。詳細については、 Using the Windows Headers を参照してください。

メモ

winreg.h ヘッダーは RegCopyTree をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版が自動的に選択されます。エンコーディングに依存しないエイリアスの使用を、エンコーディングに依存しないわけではないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

WIN32_ERROR RegCopyTreeA(
    HKEY hKeySrc,
    LPCSTR lpSubKey,   // optional
    HKEY hKeyDest
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint RegCopyTreeA(
    IntPtr hKeySrc,   // HKEY
    [MarshalAs(UnmanagedType.LPStr)] string lpSubKey,   // LPCSTR optional
    IntPtr hKeyDest   // HKEY
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function RegCopyTreeA(
    hKeySrc As IntPtr,   ' HKEY
    <MarshalAs(UnmanagedType.LPStr)> lpSubKey As String,   ' LPCSTR optional
    hKeyDest As IntPtr   ' HKEY
) As UInteger
End Function
' hKeySrc : HKEY
' lpSubKey : LPCSTR optional
' hKeyDest : HKEY
Declare PtrSafe Function RegCopyTreeA Lib "advapi32" ( _
    ByVal hKeySrc As LongPtr, _
    ByVal lpSubKey As String, _
    ByVal hKeyDest As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

RegCopyTreeA = ctypes.windll.advapi32.RegCopyTreeA
RegCopyTreeA.restype = wintypes.DWORD
RegCopyTreeA.argtypes = [
    wintypes.HANDLE,  # hKeySrc : HKEY
    wintypes.LPCSTR,  # lpSubKey : LPCSTR optional
    wintypes.HANDLE,  # hKeyDest : HKEY
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
RegCopyTreeA = Fiddle::Function.new(
  lib['RegCopyTreeA'],
  [
    Fiddle::TYPE_VOIDP,  # hKeySrc : HKEY
    Fiddle::TYPE_VOIDP,  # lpSubKey : LPCSTR optional
    Fiddle::TYPE_VOIDP,  # hKeyDest : HKEY
  ],
  -Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn RegCopyTreeA(
        hKeySrc: *mut core::ffi::c_void,  // HKEY
        lpSubKey: *const u8,  // LPCSTR optional
        hKeyDest: *mut core::ffi::c_void  // HKEY
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi)]
public static extern uint RegCopyTreeA(IntPtr hKeySrc, [MarshalAs(UnmanagedType.LPStr)] string lpSubKey, IntPtr hKeyDest);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegCopyTreeA' -Namespace Win32 -PassThru
# $api::RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest)
#uselib "ADVAPI32.dll"
#func global RegCopyTreeA "RegCopyTreeA" sptr, sptr, sptr
; RegCopyTreeA hKeySrc, lpSubKey, hKeyDest   ; 戻り値は stat
; hKeySrc : HKEY -> "sptr"
; lpSubKey : LPCSTR optional -> "sptr"
; hKeyDest : HKEY -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global RegCopyTreeA "RegCopyTreeA" sptr, str, sptr
; res = RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest)
; hKeySrc : HKEY -> "sptr"
; lpSubKey : LPCSTR optional -> "str"
; hKeyDest : HKEY -> "sptr"
; WIN32_ERROR RegCopyTreeA(HKEY hKeySrc, LPCSTR lpSubKey, HKEY hKeyDest)
#uselib "ADVAPI32.dll"
#cfunc global RegCopyTreeA "RegCopyTreeA" intptr, str, intptr
; res = RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest)
; hKeySrc : HKEY -> "intptr"
; lpSubKey : LPCSTR optional -> "str"
; hKeyDest : HKEY -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRegCopyTreeA = advapi32.NewProc("RegCopyTreeA")
)

// hKeySrc (HKEY), lpSubKey (LPCSTR optional), hKeyDest (HKEY)
r1, _, err := procRegCopyTreeA.Call(
	uintptr(hKeySrc),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpSubKey))),
	uintptr(hKeyDest),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function RegCopyTreeA(
  hKeySrc: THandle;   // HKEY
  lpSubKey: PAnsiChar;   // LPCSTR optional
  hKeyDest: THandle   // HKEY
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegCopyTreeA';
result := DllCall("ADVAPI32\RegCopyTreeA"
    , "Ptr", hKeySrc   ; HKEY
    , "AStr", lpSubKey   ; LPCSTR optional
    , "Ptr", hKeyDest   ; HKEY
    , "UInt")   ; return: WIN32_ERROR
●RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest) = DLL("ADVAPI32.dll", "dword RegCopyTreeA(void*, char*, void*)")
# 呼び出し: RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest)
# hKeySrc : HKEY -> "void*"
# lpSubKey : LPCSTR optional -> "char*"
# hKeyDest : HKEY -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "advapi32" fn RegCopyTreeA(
    hKeySrc: ?*anyopaque, // HKEY
    lpSubKey: [*c]const u8, // LPCSTR optional
    hKeyDest: ?*anyopaque // HKEY
) callconv(std.os.windows.WINAPI) u32;
proc RegCopyTreeA(
    hKeySrc: pointer,  # HKEY
    lpSubKey: cstring,  # LPCSTR optional
    hKeyDest: pointer  # HKEY
): uint32 {.importc: "RegCopyTreeA", stdcall, dynlib: "ADVAPI32.dll".}
pragma(lib, "advapi32");
extern(Windows)
uint RegCopyTreeA(
    void* hKeySrc,   // HKEY
    const(char)* lpSubKey,   // LPCSTR optional
    void* hKeyDest   // HKEY
);
ccall((:RegCopyTreeA, "ADVAPI32.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, Cstring, Ptr{Cvoid}),
      hKeySrc, lpSubKey, hKeyDest)
# hKeySrc : HKEY -> Ptr{Cvoid}
# lpSubKey : LPCSTR optional -> Cstring
# hKeyDest : HKEY -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t RegCopyTreeA(
    void* hKeySrc,
    const char* lpSubKey,
    void* hKeyDest);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest)
-- hKeySrc : HKEY
-- lpSubKey : LPCSTR optional
-- hKeyDest : HKEY
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RegCopyTreeA = lib.func('__stdcall', 'RegCopyTreeA', 'uint32_t', ['void *', 'str', 'void *']);
// RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest)
// hKeySrc : HKEY -> 'void *'
// lpSubKey : LPCSTR optional -> 'str'
// hKeyDest : HKEY -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  RegCopyTreeA: { parameters: ["pointer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest)
// hKeySrc : HKEY -> "pointer"
// lpSubKey : LPCSTR optional -> "buffer"
// hKeyDest : HKEY -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t RegCopyTreeA(
    void* hKeySrc,
    const char* lpSubKey,
    void* hKeyDest);
C, "ADVAPI32.dll");
// $ffi->RegCopyTreeA(hKeySrc, lpSubKey, hKeyDest);
// hKeySrc : HKEY
// lpSubKey : LPCSTR optional
// hKeyDest : HKEY
// 構造体/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 RegCopyTreeA(
        Pointer hKeySrc,   // HKEY
        String lpSubKey,   // LPCSTR optional
        Pointer hKeyDest   // HKEY
    );
}
@[Link("advapi32")]
lib LibADVAPI32
  fun RegCopyTreeA = RegCopyTreeA(
    hKeySrc : Void*,   # HKEY
    lpSubKey : UInt8*,   # LPCSTR optional
    hKeyDest : Void*   # HKEY
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef RegCopyTreeANative = Uint32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Void>);
typedef RegCopyTreeADart = int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Void>);
final RegCopyTreeA = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<RegCopyTreeANative, RegCopyTreeADart>('RegCopyTreeA');
// hKeySrc : HKEY -> Pointer<Void>
// lpSubKey : LPCSTR optional -> Pointer<Utf8>
// hKeyDest : HKEY -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function RegCopyTreeA(
  hKeySrc: THandle;   // HKEY
  lpSubKey: PAnsiChar;   // LPCSTR optional
  hKeyDest: THandle   // HKEY
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegCopyTreeA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RegCopyTreeA"
  c_RegCopyTreeA :: Ptr () -> CString -> Ptr () -> IO Word32
-- hKeySrc : HKEY -> Ptr ()
-- lpSubKey : LPCSTR optional -> CString
-- hKeyDest : HKEY -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let regcopytreea =
  foreign "RegCopyTreeA"
    ((ptr void) @-> string @-> (ptr void) @-> returning uint32_t)
(* hKeySrc : HKEY -> (ptr void) *)
(* lpSubKey : LPCSTR optional -> string *)
(* hKeyDest : HKEY -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("RegCopyTreeA" reg-copy-tree-a :convention :stdcall) :uint32
  (h-key-src :pointer)   ; HKEY
  (lp-sub-key :string)   ; LPCSTR optional
  (h-key-dest :pointer))   ; HKEY
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RegCopyTreeA = Win32::API::More->new('ADVAPI32',
    'DWORD RegCopyTreeA(HANDLE hKeySrc, LPCSTR lpSubKey, HANDLE hKeyDest)');
# my $ret = $RegCopyTreeA->Call($hKeySrc, $lpSubKey, $hKeyDest);
# hKeySrc : HKEY -> HANDLE
# lpSubKey : LPCSTR optional -> LPCSTR
# hKeyDest : HKEY -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
使用する型