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

PFXExportCertStore

関数
証明書ストアをパスワード保護したPFX BLOBにエクスポートする。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL PFXExportCertStore(
    HCERTSTORE hStore,
    CRYPT_INTEGER_BLOB* pPFX,
    LPCWSTR szPassword,
    DWORD dwFlags
);

パラメーター

名前方向
hStoreHCERTSTOREin
pPFXCRYPT_INTEGER_BLOB*inout
szPasswordLPCWSTRin
dwFlagsDWORDin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL PFXExportCertStore(
    HCERTSTORE hStore,
    CRYPT_INTEGER_BLOB* pPFX,
    LPCWSTR szPassword,
    DWORD dwFlags
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool PFXExportCertStore(
    IntPtr hStore,   // HCERTSTORE
    IntPtr pPFX,   // CRYPT_INTEGER_BLOB* in/out
    [MarshalAs(UnmanagedType.LPWStr)] string szPassword,   // LPCWSTR
    uint dwFlags   // DWORD
);
<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function PFXExportCertStore(
    hStore As IntPtr,   ' HCERTSTORE
    pPFX As IntPtr,   ' CRYPT_INTEGER_BLOB* in/out
    <MarshalAs(UnmanagedType.LPWStr)> szPassword As String,   ' LPCWSTR
    dwFlags As UInteger   ' DWORD
) As Boolean
End Function
' hStore : HCERTSTORE
' pPFX : CRYPT_INTEGER_BLOB* in/out
' szPassword : LPCWSTR
' dwFlags : DWORD
Declare PtrSafe Function PFXExportCertStore Lib "crypt32" ( _
    ByVal hStore As LongPtr, _
    ByVal pPFX As LongPtr, _
    ByVal szPassword As LongPtr, _
    ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PFXExportCertStore = ctypes.windll.crypt32.PFXExportCertStore
PFXExportCertStore.restype = wintypes.BOOL
PFXExportCertStore.argtypes = [
    wintypes.HANDLE,  # hStore : HCERTSTORE
    ctypes.c_void_p,  # pPFX : CRYPT_INTEGER_BLOB* in/out
    wintypes.LPCWSTR,  # szPassword : LPCWSTR
    wintypes.DWORD,  # dwFlags : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CRYPT32.dll')
PFXExportCertStore = Fiddle::Function.new(
  lib['PFXExportCertStore'],
  [
    Fiddle::TYPE_VOIDP,  # hStore : HCERTSTORE
    Fiddle::TYPE_VOIDP,  # pPFX : CRYPT_INTEGER_BLOB* in/out
    Fiddle::TYPE_VOIDP,  # szPassword : LPCWSTR
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn PFXExportCertStore(
        hStore: *mut core::ffi::c_void,  // HCERTSTORE
        pPFX: *mut CRYPT_INTEGER_BLOB,  // CRYPT_INTEGER_BLOB* in/out
        szPassword: *const u16,  // LPCWSTR
        dwFlags: u32  // DWORD
    ) -> 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 PFXExportCertStore(IntPtr hStore, IntPtr pPFX, [MarshalAs(UnmanagedType.LPWStr)] string szPassword, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_PFXExportCertStore' -Namespace Win32 -PassThru
# $api::PFXExportCertStore(hStore, pPFX, szPassword, dwFlags)
#uselib "CRYPT32.dll"
#func global PFXExportCertStore "PFXExportCertStore" sptr, sptr, sptr, sptr
; PFXExportCertStore hStore, varptr(pPFX), szPassword, dwFlags   ; 戻り値は stat
; hStore : HCERTSTORE -> "sptr"
; pPFX : CRYPT_INTEGER_BLOB* in/out -> "sptr"
; szPassword : LPCWSTR -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global PFXExportCertStore "PFXExportCertStore" sptr, var, wstr, int
; res = PFXExportCertStore(hStore, pPFX, szPassword, dwFlags)
; hStore : HCERTSTORE -> "sptr"
; pPFX : CRYPT_INTEGER_BLOB* in/out -> "var"
; szPassword : LPCWSTR -> "wstr"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL PFXExportCertStore(HCERTSTORE hStore, CRYPT_INTEGER_BLOB* pPFX, LPCWSTR szPassword, DWORD dwFlags)
#uselib "CRYPT32.dll"
#cfunc global PFXExportCertStore "PFXExportCertStore" intptr, var, wstr, int
; res = PFXExportCertStore(hStore, pPFX, szPassword, dwFlags)
; hStore : HCERTSTORE -> "intptr"
; pPFX : CRYPT_INTEGER_BLOB* in/out -> "var"
; szPassword : LPCWSTR -> "wstr"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procPFXExportCertStore = crypt32.NewProc("PFXExportCertStore")
)

// hStore (HCERTSTORE), pPFX (CRYPT_INTEGER_BLOB* in/out), szPassword (LPCWSTR), dwFlags (DWORD)
r1, _, err := procPFXExportCertStore.Call(
	uintptr(hStore),
	uintptr(pPFX),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szPassword))),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function PFXExportCertStore(
  hStore: THandle;   // HCERTSTORE
  pPFX: Pointer;   // CRYPT_INTEGER_BLOB* in/out
  szPassword: PWideChar;   // LPCWSTR
  dwFlags: DWORD   // DWORD
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'PFXExportCertStore';
result := DllCall("CRYPT32\PFXExportCertStore"
    , "Ptr", hStore   ; HCERTSTORE
    , "Ptr", pPFX   ; CRYPT_INTEGER_BLOB* in/out
    , "WStr", szPassword   ; LPCWSTR
    , "UInt", dwFlags   ; DWORD
    , "Int")   ; return: BOOL
●PFXExportCertStore(hStore, pPFX, szPassword, dwFlags) = DLL("CRYPT32.dll", "bool PFXExportCertStore(void*, void*, char*, dword)")
# 呼び出し: PFXExportCertStore(hStore, pPFX, szPassword, dwFlags)
# hStore : HCERTSTORE -> "void*"
# pPFX : CRYPT_INTEGER_BLOB* in/out -> "void*"
# szPassword : LPCWSTR -> "char*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。