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

CryptExportPublicKeyInfo

関数
暗号プロバイダの鍵から公開鍵情報をエクスポートする。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptExportPublicKeyInfo(
    HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey,
    DWORD dwKeySpec,   // optional
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    CERT_PUBLIC_KEY_INFO* pInfo,   // optional
    DWORD* pcbInfo
);

パラメーター

名前方向
hCryptProvOrNCryptKeyHCRYPTPROV_OR_NCRYPT_KEY_HANDLEin
dwKeySpecDWORDinoptional
dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin
pInfoCERT_PUBLIC_KEY_INFO*outoptional
pcbInfoDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL CryptExportPublicKeyInfo(
    HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey,
    DWORD dwKeySpec,   // optional
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    CERT_PUBLIC_KEY_INFO* pInfo,   // optional
    DWORD* pcbInfo
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptExportPublicKeyInfo(
    UIntPtr hCryptProvOrNCryptKey,   // HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
    uint dwKeySpec,   // DWORD optional
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    IntPtr pInfo,   // CERT_PUBLIC_KEY_INFO* optional, out
    ref uint pcbInfo   // DWORD* in/out
);
<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptExportPublicKeyInfo(
    hCryptProvOrNCryptKey As UIntPtr,   ' HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
    dwKeySpec As UInteger,   ' DWORD optional
    dwCertEncodingType As UInteger,   ' CERT_QUERY_ENCODING_TYPE
    pInfo As IntPtr,   ' CERT_PUBLIC_KEY_INFO* optional, out
    ByRef pcbInfo As UInteger   ' DWORD* in/out
) As Boolean
End Function
' hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
' dwKeySpec : DWORD optional
' dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
' pInfo : CERT_PUBLIC_KEY_INFO* optional, out
' pcbInfo : DWORD* in/out
Declare PtrSafe Function CryptExportPublicKeyInfo Lib "crypt32" ( _
    ByVal hCryptProvOrNCryptKey As LongPtr, _
    ByVal dwKeySpec As Long, _
    ByVal dwCertEncodingType As Long, _
    ByVal pInfo As LongPtr, _
    ByRef pcbInfo As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptExportPublicKeyInfo = ctypes.windll.crypt32.CryptExportPublicKeyInfo
CryptExportPublicKeyInfo.restype = wintypes.BOOL
CryptExportPublicKeyInfo.argtypes = [
    ctypes.c_size_t,  # hCryptProvOrNCryptKey : HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
    wintypes.DWORD,  # dwKeySpec : DWORD optional
    wintypes.DWORD,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    ctypes.c_void_p,  # pInfo : CERT_PUBLIC_KEY_INFO* optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcbInfo : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCryptExportPublicKeyInfo = crypt32.NewProc("CryptExportPublicKeyInfo")
)

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