Win32 API 日本語リファレンス
ホームUI.Shell › SHCopyKeyA

SHCopyKeyA

関数
レジストリキーのサブキーと値を別のキーへ複製する。
DLLSHLWAPI.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR SHCopyKeyA(
    HKEY hkeySrc,
    LPCSTR pszSrcSubKey,   // optional
    HKEY hkeyDest,
    DWORD fReserved   // optional
);

パラメーター

名前方向説明
hkeySrcHKEYinソースキーへのハンドル(例: HKEY_CURRENT_USER)。
pszSrcSubKeyLPCSTRinoptionalサブキーと値をコピーする対象のサブキー。
hkeyDestHKEYin宛先キー。
fReservedDWORDoptional予約済み。0 を指定する必要があります。

戻り値の型: WIN32_ERROR

公式ドキュメント

ソースサブキーのサブキーと値を、宛先キーへ再帰的にコピーします。SHCopyKey はキーのセキュリティ属性をコピーしません。(ANSI)

戻り値

型: LSTATUS

成功した場合は ERROR_SUCCESS を返します。それ以外の場合は、Winerror.h で定義されている 0 以外のエラーコードのいずれかを返します。エラーの一般的な説明を取得するには、FormatMessageFORMAT_MESSAGE_FROM_SYSTEM フラグとともに使用してください。

解説(Remarks)

重要 この関数は、コピーするキーや値のセキュリティ属性を複製しません。代わりに、宛先キーのすべてのセキュリティ属性は既定の属性になります。
メモ

shlwapi.h ヘッダーは、SHCopyKey を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規約を参照してください。

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

各言語での呼び出し定義

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

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

SHCopyKeyA = ctypes.windll.shlwapi.SHCopyKeyA
SHCopyKeyA.restype = wintypes.DWORD
SHCopyKeyA.argtypes = [
    wintypes.HANDLE,  # hkeySrc : HKEY
    wintypes.LPCSTR,  # pszSrcSubKey : LPCSTR optional
    wintypes.HANDLE,  # hkeyDest : HKEY
    wintypes.DWORD,  # fReserved : DWORD optional
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procSHCopyKeyA = shlwapi.NewProc("SHCopyKeyA")
)

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

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

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

foreign import stdcall safe "SHCopyKeyA"
  c_SHCopyKeyA :: Ptr () -> CString -> Ptr () -> Word32 -> IO Word32
-- hkeySrc : HKEY -> Ptr ()
-- pszSrcSubKey : LPCSTR optional -> CString
-- hkeyDest : HKEY -> Ptr ()
-- fReserved : DWORD optional -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shcopykeya =
  foreign "SHCopyKeyA"
    ((ptr void) @-> string @-> (ptr void) @-> uint32_t @-> returning uint32_t)
(* hkeySrc : HKEY -> (ptr void) *)
(* pszSrcSubKey : LPCSTR optional -> string *)
(* hkeyDest : HKEY -> (ptr void) *)
(* fReserved : DWORD optional -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)

(cffi:defcfun ("SHCopyKeyA" shcopy-key-a :convention :stdcall) :uint32
  (hkey-src :pointer)   ; HKEY
  (psz-src-sub-key :string)   ; LPCSTR optional
  (hkey-dest :pointer)   ; HKEY
  (f-reserved :uint32))   ; DWORD optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHCopyKeyA = Win32::API::More->new('SHLWAPI',
    'DWORD SHCopyKeyA(HANDLE hkeySrc, LPCSTR pszSrcSubKey, HANDLE hkeyDest, DWORD fReserved)');
# my $ret = $SHCopyKeyA->Call($hkeySrc, $pszSrcSubKey, $hkeyDest, $fReserved);
# hkeySrc : HKEY -> HANDLE
# pszSrcSubKey : LPCSTR optional -> LPCSTR
# hkeyDest : HKEY -> HANDLE
# fReserved : DWORD optional -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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