RegCopyTreeW
関数シグネチャ
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR RegCopyTreeW(
HKEY hKeySrc,
LPCWSTR lpSubKey, // optional
HKEY hKeyDest
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKeySrc | HKEY | in | 開いているレジストリキーのハンドル。このキーは KEY_READ アクセス権で開かれている必要があります。詳細については、 Registry Key Security and Access Rights を参照してください。 このハンドルは RegCreateKeyEx 関数または RegOpenKeyEx 関数によって返されます。あるいは、定義済みキーのいずれかを指定できます。 |
| lpSubKey | LPCWSTR | inoptional | キーの名前。このキーは、hKeySrc パラメーターで識別されるキーのサブキーである必要があります。このパラメーターには NULL を指定することもできます。 |
| hKeyDest | HKEY | in | 宛先キーのハンドル。呼び出し元プロセスは、そのキーに対する KEY_CREATE_SUB_KEY アクセス権を持っている必要があります。 このハンドルは RegCreateKeyEx 関数または RegOpenKeyEx 関数によって返されます。あるいは、定義済みキーのいずれかを指定できます。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
指定したレジストリキーを、その値およびサブキーとともに、指定した宛先キーへコピーします。(Unicode)
戻り値
関数が成功した場合、戻り値は 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 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR RegCopyTreeW(
HKEY hKeySrc,
LPCWSTR lpSubKey, // optional
HKEY hKeyDest
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegCopyTreeW(
IntPtr hKeySrc, // HKEY
[MarshalAs(UnmanagedType.LPWStr)] string lpSubKey, // LPCWSTR optional
IntPtr hKeyDest // HKEY
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegCopyTreeW(
hKeySrc As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPWStr)> lpSubKey As String, ' LPCWSTR optional
hKeyDest As IntPtr ' HKEY
) As UInteger
End Function' hKeySrc : HKEY
' lpSubKey : LPCWSTR optional
' hKeyDest : HKEY
Declare PtrSafe Function RegCopyTreeW Lib "advapi32" ( _
ByVal hKeySrc As LongPtr, _
ByVal lpSubKey As LongPtr, _
ByVal hKeyDest 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
RegCopyTreeW = ctypes.windll.advapi32.RegCopyTreeW
RegCopyTreeW.restype = wintypes.DWORD
RegCopyTreeW.argtypes = [
wintypes.HANDLE, # hKeySrc : HKEY
wintypes.LPCWSTR, # lpSubKey : LPCWSTR optional
wintypes.HANDLE, # hKeyDest : HKEY
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
RegCopyTreeW = Fiddle::Function.new(
lib['RegCopyTreeW'],
[
Fiddle::TYPE_VOIDP, # hKeySrc : HKEY
Fiddle::TYPE_VOIDP, # lpSubKey : LPCWSTR optional
Fiddle::TYPE_VOIDP, # hKeyDest : HKEY
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "advapi32")]
extern "system" {
fn RegCopyTreeW(
hKeySrc: *mut core::ffi::c_void, // HKEY
lpSubKey: *const u16, // LPCWSTR 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.Unicode)]
public static extern uint RegCopyTreeW(IntPtr hKeySrc, [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey, IntPtr hKeyDest);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegCopyTreeW' -Namespace Win32 -PassThru
# $api::RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest)#uselib "ADVAPI32.dll"
#func global RegCopyTreeW "RegCopyTreeW" wptr, wptr, wptr
; RegCopyTreeW hKeySrc, lpSubKey, hKeyDest ; 戻り値は stat
; hKeySrc : HKEY -> "wptr"
; lpSubKey : LPCWSTR optional -> "wptr"
; hKeyDest : HKEY -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global RegCopyTreeW "RegCopyTreeW" sptr, wstr, sptr
; res = RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest)
; hKeySrc : HKEY -> "sptr"
; lpSubKey : LPCWSTR optional -> "wstr"
; hKeyDest : HKEY -> "sptr"; WIN32_ERROR RegCopyTreeW(HKEY hKeySrc, LPCWSTR lpSubKey, HKEY hKeyDest)
#uselib "ADVAPI32.dll"
#cfunc global RegCopyTreeW "RegCopyTreeW" intptr, wstr, intptr
; res = RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest)
; hKeySrc : HKEY -> "intptr"
; lpSubKey : LPCWSTR optional -> "wstr"
; hKeyDest : HKEY -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRegCopyTreeW = advapi32.NewProc("RegCopyTreeW")
)
// hKeySrc (HKEY), lpSubKey (LPCWSTR optional), hKeyDest (HKEY)
r1, _, err := procRegCopyTreeW.Call(
uintptr(hKeySrc),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpSubKey))),
uintptr(hKeyDest),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction RegCopyTreeW(
hKeySrc: THandle; // HKEY
lpSubKey: PWideChar; // LPCWSTR optional
hKeyDest: THandle // HKEY
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegCopyTreeW';result := DllCall("ADVAPI32\RegCopyTreeW"
, "Ptr", hKeySrc ; HKEY
, "WStr", lpSubKey ; LPCWSTR optional
, "Ptr", hKeyDest ; HKEY
, "UInt") ; return: WIN32_ERROR●RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest) = DLL("ADVAPI32.dll", "dword RegCopyTreeW(void*, char*, void*)")
# 呼び出し: RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest)
# hKeySrc : HKEY -> "void*"
# lpSubKey : LPCWSTR optional -> "char*"
# hKeyDest : HKEY -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "advapi32" fn RegCopyTreeW(
hKeySrc: ?*anyopaque, // HKEY
lpSubKey: [*c]const u16, // LPCWSTR optional
hKeyDest: ?*anyopaque // HKEY
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc RegCopyTreeW(
hKeySrc: pointer, # HKEY
lpSubKey: WideCString, # LPCWSTR optional
hKeyDest: pointer # HKEY
): uint32 {.importc: "RegCopyTreeW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "advapi32");
extern(Windows)
uint RegCopyTreeW(
void* hKeySrc, // HKEY
const(wchar)* lpSubKey, // LPCWSTR optional
void* hKeyDest // HKEY
);ccall((:RegCopyTreeW, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cwstring, Ptr{Cvoid}),
hKeySrc, lpSubKey, hKeyDest)
# hKeySrc : HKEY -> Ptr{Cvoid}
# lpSubKey : LPCWSTR optional -> Cwstring
# hKeyDest : HKEY -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t RegCopyTreeW(
void* hKeySrc,
const uint16_t* lpSubKey,
void* hKeyDest);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest)
-- hKeySrc : HKEY
-- lpSubKey : LPCWSTR optional
-- hKeyDest : HKEY
-- 構造体/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 RegCopyTreeW = lib.func('__stdcall', 'RegCopyTreeW', 'uint32_t', ['void *', 'str16', 'void *']);
// RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest)
// hKeySrc : HKEY -> 'void *'
// lpSubKey : LPCWSTR optional -> 'str16'
// hKeyDest : HKEY -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
RegCopyTreeW: { parameters: ["pointer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest)
// hKeySrc : HKEY -> "pointer"
// lpSubKey : LPCWSTR optional -> "buffer"
// hKeyDest : HKEY -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t RegCopyTreeW(
void* hKeySrc,
const uint16_t* lpSubKey,
void* hKeyDest);
C, "ADVAPI32.dll");
// $ffi->RegCopyTreeW(hKeySrc, lpSubKey, hKeyDest);
// hKeySrc : HKEY
// lpSubKey : LPCWSTR 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.UNICODE_OPTIONS);
int RegCopyTreeW(
Pointer hKeySrc, // HKEY
WString lpSubKey, // LPCWSTR optional
Pointer hKeyDest // HKEY
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("advapi32")]
lib LibADVAPI32
fun RegCopyTreeW = RegCopyTreeW(
hKeySrc : Void*, # HKEY
lpSubKey : UInt16*, # LPCWSTR 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 RegCopyTreeWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Void>);
typedef RegCopyTreeWDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Void>);
final RegCopyTreeW = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RegCopyTreeWNative, RegCopyTreeWDart>('RegCopyTreeW');
// hKeySrc : HKEY -> Pointer<Void>
// lpSubKey : LPCWSTR optional -> Pointer<Utf16>
// hKeyDest : HKEY -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegCopyTreeW(
hKeySrc: THandle; // HKEY
lpSubKey: PWideChar; // LPCWSTR optional
hKeyDest: THandle // HKEY
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegCopyTreeW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegCopyTreeW"
c_RegCopyTreeW :: Ptr () -> CWString -> Ptr () -> IO Word32
-- hKeySrc : HKEY -> Ptr ()
-- lpSubKey : LPCWSTR optional -> CWString
-- hKeyDest : HKEY -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let regcopytreew =
foreign "RegCopyTreeW"
((ptr void) @-> (ptr uint16_t) @-> (ptr void) @-> returning uint32_t)
(* hKeySrc : HKEY -> (ptr void) *)
(* lpSubKey : LPCWSTR optional -> (ptr uint16_t) *)
(* 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 ("RegCopyTreeW" reg-copy-tree-w :convention :stdcall) :uint32
(h-key-src :pointer) ; HKEY
(lp-sub-key (:string :encoding :utf-16le)) ; LPCWSTR optional
(h-key-dest :pointer)) ; HKEY
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegCopyTreeW = Win32::API::More->new('ADVAPI32',
'DWORD RegCopyTreeW(HANDLE hKeySrc, LPCWSTR lpSubKey, HANDLE hKeyDest)');
# my $ret = $RegCopyTreeW->Call($hKeySrc, $lpSubKey, $hKeyDest);
# hKeySrc : HKEY -> HANDLE
# lpSubKey : LPCWSTR optional -> LPCWSTR
# hKeyDest : HKEY -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f RegCopyTreeA (ANSI版) — 指定キーのサブツリー全体を別のキーへコピーする。