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

CryptExportKey

関数
暗号鍵を保護されたキーBLOB形式でエクスポートする。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptExportKey(
    UINT_PTR hKey,
    UINT_PTR hExpKey,
    DWORD dwBlobType,
    CRYPT_KEY_FLAGS dwFlags,
    BYTE* pbData,   // optional
    DWORD* pdwDataLen
);

パラメーター

名前方向
hKeyUINT_PTRin
hExpKeyUINT_PTRin
dwBlobTypeDWORDin
dwFlagsCRYPT_KEY_FLAGSin
pbDataBYTE*outoptional
pdwDataLenDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

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

CryptExportKey = ctypes.windll.advapi32.CryptExportKey
CryptExportKey.restype = wintypes.BOOL
CryptExportKey.argtypes = [
    ctypes.c_size_t,  # hKey : UINT_PTR
    ctypes.c_size_t,  # hExpKey : UINT_PTR
    wintypes.DWORD,  # dwBlobType : DWORD
    wintypes.DWORD,  # dwFlags : CRYPT_KEY_FLAGS
    ctypes.POINTER(ctypes.c_ubyte),  # pbData : BYTE* optional, out
    ctypes.POINTER(wintypes.DWORD),  # pdwDataLen : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CryptExportKey = Fiddle::Function.new(
  lib['CryptExportKey'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hKey : UINT_PTR
    Fiddle::TYPE_UINTPTR_T,  # hExpKey : UINT_PTR
    -Fiddle::TYPE_INT,  # dwBlobType : DWORD
    -Fiddle::TYPE_INT,  # dwFlags : CRYPT_KEY_FLAGS
    Fiddle::TYPE_VOIDP,  # pbData : BYTE* optional, out
    Fiddle::TYPE_VOIDP,  # pdwDataLen : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CryptExportKey(
        hKey: usize,  // UINT_PTR
        hExpKey: usize,  // UINT_PTR
        dwBlobType: u32,  // DWORD
        dwFlags: u32,  // CRYPT_KEY_FLAGS
        pbData: *mut u8,  // BYTE* optional, out
        pdwDataLen: *mut u32  // DWORD* in/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 CryptExportKey(UIntPtr hKey, UIntPtr hExpKey, uint dwBlobType, uint dwFlags, IntPtr pbData, ref uint pdwDataLen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptExportKey' -Namespace Win32 -PassThru
# $api::CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen)
#uselib "ADVAPI32.dll"
#func global CryptExportKey "CryptExportKey" sptr, sptr, sptr, sptr, sptr, sptr
; CryptExportKey hKey, hExpKey, dwBlobType, dwFlags, varptr(pbData), varptr(pdwDataLen)   ; 戻り値は stat
; hKey : UINT_PTR -> "sptr"
; hExpKey : UINT_PTR -> "sptr"
; dwBlobType : DWORD -> "sptr"
; dwFlags : CRYPT_KEY_FLAGS -> "sptr"
; pbData : BYTE* optional, out -> "sptr"
; pdwDataLen : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CryptExportKey "CryptExportKey" sptr, sptr, int, int, var, var
; res = CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen)
; hKey : UINT_PTR -> "sptr"
; hExpKey : UINT_PTR -> "sptr"
; dwBlobType : DWORD -> "int"
; dwFlags : CRYPT_KEY_FLAGS -> "int"
; pbData : BYTE* optional, out -> "var"
; pdwDataLen : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptExportKey(UINT_PTR hKey, UINT_PTR hExpKey, DWORD dwBlobType, CRYPT_KEY_FLAGS dwFlags, BYTE* pbData, DWORD* pdwDataLen)
#uselib "ADVAPI32.dll"
#cfunc global CryptExportKey "CryptExportKey" intptr, intptr, int, int, var, var
; res = CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen)
; hKey : UINT_PTR -> "intptr"
; hExpKey : UINT_PTR -> "intptr"
; dwBlobType : DWORD -> "int"
; dwFlags : CRYPT_KEY_FLAGS -> "int"
; pbData : BYTE* optional, out -> "var"
; pdwDataLen : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptExportKey = advapi32.NewProc("CryptExportKey")
)

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