Win32 API 日本語リファレンス
ホームSecurity.Cryptography › CryptDuplicateKey

CryptDuplicateKey

関数
暗号鍵ハンドルを複製して状態をコピーする。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// ADVAPI32.dll
#include <windows.h>

BOOL CryptDuplicateKey(
    UINT_PTR hKey,
    DWORD* pdwReserved,   // optional
    DWORD dwFlags,
    UINT_PTR* phKey
);

パラメーター

名前方向
hKeyUINT_PTRin
pdwReservedDWORD*optional
dwFlagsDWORDin
phKeyUINT_PTR*out

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll
#include <windows.h>

BOOL CryptDuplicateKey(
    UINT_PTR hKey,
    DWORD* pdwReserved,   // optional
    DWORD dwFlags,
    UINT_PTR* phKey
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptDuplicateKey(
    UIntPtr hKey,   // UINT_PTR
    IntPtr pdwReserved,   // DWORD* optional
    uint dwFlags,   // DWORD
    out UIntPtr phKey   // UINT_PTR* out
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptDuplicateKey(
    hKey As UIntPtr,   ' UINT_PTR
    pdwReserved As IntPtr,   ' DWORD* optional
    dwFlags As UInteger,   ' DWORD
    <Out> ByRef phKey As UIntPtr   ' UINT_PTR* out
) As Boolean
End Function
' hKey : UINT_PTR
' pdwReserved : DWORD* optional
' dwFlags : DWORD
' phKey : UINT_PTR* out
Declare PtrSafe Function CryptDuplicateKey Lib "advapi32" ( _
    ByVal hKey As LongPtr, _
    ByVal pdwReserved As LongPtr, _
    ByVal dwFlags As Long, _
    ByRef phKey As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptDuplicateKey = ctypes.windll.advapi32.CryptDuplicateKey
CryptDuplicateKey.restype = wintypes.BOOL
CryptDuplicateKey.argtypes = [
    ctypes.c_size_t,  # hKey : UINT_PTR
    ctypes.POINTER(wintypes.DWORD),  # pdwReserved : DWORD* optional
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.POINTER(ctypes.c_size_t),  # phKey : UINT_PTR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CryptDuplicateKey = Fiddle::Function.new(
  lib['CryptDuplicateKey'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hKey : UINT_PTR
    Fiddle::TYPE_VOIDP,  # pdwReserved : DWORD* optional
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_VOIDP,  # phKey : UINT_PTR* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CryptDuplicateKey(
        hKey: usize,  // UINT_PTR
        pdwReserved: *mut u32,  // DWORD* optional
        dwFlags: u32,  // DWORD
        phKey: *mut usize  // UINT_PTR* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool CryptDuplicateKey(UIntPtr hKey, IntPtr pdwReserved, uint dwFlags, out UIntPtr phKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptDuplicateKey' -Namespace Win32 -PassThru
# $api::CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
#uselib "ADVAPI32.dll"
#func global CryptDuplicateKey "CryptDuplicateKey" sptr, sptr, sptr, sptr
; CryptDuplicateKey hKey, varptr(pdwReserved), dwFlags, varptr(phKey)   ; 戻り値は stat
; hKey : UINT_PTR -> "sptr"
; pdwReserved : DWORD* optional -> "sptr"
; dwFlags : DWORD -> "sptr"
; phKey : UINT_PTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CryptDuplicateKey "CryptDuplicateKey" sptr, var, int, var
; res = CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
; hKey : UINT_PTR -> "sptr"
; pdwReserved : DWORD* optional -> "var"
; dwFlags : DWORD -> "int"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptDuplicateKey(UINT_PTR hKey, DWORD* pdwReserved, DWORD dwFlags, UINT_PTR* phKey)
#uselib "ADVAPI32.dll"
#cfunc global CryptDuplicateKey "CryptDuplicateKey" intptr, var, int, var
; res = CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
; hKey : UINT_PTR -> "intptr"
; pdwReserved : DWORD* optional -> "var"
; dwFlags : DWORD -> "int"
; phKey : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptDuplicateKey = advapi32.NewProc("CryptDuplicateKey")
)

// hKey (UINT_PTR), pdwReserved (DWORD* optional), dwFlags (DWORD), phKey (UINT_PTR* out)
r1, _, err := procCryptDuplicateKey.Call(
	uintptr(hKey),
	uintptr(pdwReserved),
	uintptr(dwFlags),
	uintptr(phKey),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptDuplicateKey(
  hKey: NativeUInt;   // UINT_PTR
  pdwReserved: Pointer;   // DWORD* optional
  dwFlags: DWORD;   // DWORD
  phKey: Pointer   // UINT_PTR* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptDuplicateKey';
result := DllCall("ADVAPI32\CryptDuplicateKey"
    , "UPtr", hKey   ; UINT_PTR
    , "Ptr", pdwReserved   ; DWORD* optional
    , "UInt", dwFlags   ; DWORD
    , "Ptr", phKey   ; UINT_PTR* out
    , "Int")   ; return: BOOL
●CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey) = DLL("ADVAPI32.dll", "bool CryptDuplicateKey(int, void*, dword, void*)")
# 呼び出し: CryptDuplicateKey(hKey, pdwReserved, dwFlags, phKey)
# hKey : UINT_PTR -> "int"
# pdwReserved : DWORD* optional -> "void*"
# dwFlags : DWORD -> "dword"
# phKey : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。