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

PFXImportCertStore

関数
PFX/PKCS#12 BLOBをインポートして証明書ストアを開く。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

HCERTSTORE PFXImportCertStore(
    CRYPT_INTEGER_BLOB* pPFX,
    LPCWSTR szPassword,
    CRYPT_KEY_FLAGS dwFlags
);

パラメーター

名前方向
pPFXCRYPT_INTEGER_BLOB*in
szPasswordLPCWSTRin
dwFlagsCRYPT_KEY_FLAGSin

戻り値の型: HCERTSTORE

各言語での呼び出し定義

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

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

PFXImportCertStore = ctypes.windll.crypt32.PFXImportCertStore
PFXImportCertStore.restype = ctypes.c_void_p
PFXImportCertStore.argtypes = [
    ctypes.c_void_p,  # pPFX : CRYPT_INTEGER_BLOB*
    wintypes.LPCWSTR,  # szPassword : LPCWSTR
    wintypes.DWORD,  # dwFlags : CRYPT_KEY_FLAGS
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procPFXImportCertStore = crypt32.NewProc("PFXImportCertStore")
)

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