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

CertFindCTLInStore

関数
指定条件に合致するCTLをストアから検索する。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

CTL_CONTEXT* CertFindCTLInStore(
    HCERTSTORE hCertStore,
    DWORD dwMsgAndCertEncodingType,
    DWORD dwFindFlags,
    CERT_FIND_TYPE dwFindType,
    const void* pvFindPara,   // optional
    CTL_CONTEXT* pPrevCtlContext   // optional
);

パラメーター

名前方向説明
hCertStoreHCERTSTOREin検索対象となる証明書ストアのハンドル。
dwMsgAndCertEncodingTypeDWORDin

CTL で使用されるエンコードの種類を指定します。次の例のように、証明書のエンコードの種類とメッセージのエンコードの種類の両方を、ビットごとの OR 演算で組み合わせて指定することも常に可能です。

X509_ASN_ENCODING | PKCS_7_ASN_ENCODING 現在定義されているエンコードの種類は次のとおりです。

このパラメーターは、dwFindType パラメーターが CTL_FIND_USAGE に設定されている場合にのみ使用されます。

dwFindFlagsDWORDindwFindTypeCTL_FIND_USAGE に設定されている場合に指定できます。詳細については、後述の CTL_FIND_USAGE のコメントを参照してください。
dwFindTypeCERT_FIND_TYPEin

実行する検索の種類を指定します。検索の種類によって、pvFindPara のデータ型、内容、および使用方法が決まります。このパラメーターには、次のいずれかの値を指定できます。

意味
CTL_FIND_ANY
pvFindPara のデータ型: NULL

任意の CTL が一致します。

CTL_FIND_SHA1_HASH
pvFindPara のデータ型: CRYPT_HASH_BLOB

CRYPT_HASH_BLOB 構造体内のハッシュに一致するハッシュを持つ CTL が検索されます。

CTL_FIND_MD5_HASH
pvFindPara のデータ型: CRYPT_HASH_BLOB

CRYPT_HASH_BLOB 構造体内のハッシュに一致するハッシュを持つ CTL が検索されます。

CTL_FIND_USAGE
pvFindPara のデータ型: CTL_FIND_USAGE_PARA

CTL_FIND_USAGE_PARA 構造体内の使用法識別子、リスト識別子、または署名者に一致する使用法識別子、リスト識別子、または署名者を持つ任意の CTL が検索されます。

cUsageIdentifier メンバーが SubjectUsage のサイズである場合、任意の CTL が一致します。

ListIdentifier メンバーの cbData メンバーが 0 の場合、任意のリスト識別子が一致します。ListIdentifiercbData メンバーが CTL_FIND_NO_LIST_ID_CBDATA の場合、リスト識別子を持たない CTL のみが一致します。

CTL_FIND_USAGE_PARA 構造体内の pSigner メンバーが NULL の場合、任意の CTL 署名者が一致し、pSignerCERT_INFO 構造体内の Issuer メンバーと SerialNumber メンバーのみが使用されます。pSigner が CTL_FIND_NO_SIGNER_PTR の場合、署名者を持たない CTL のみが一致します。

CTL_FIND_SAME_USAGE_FLAG
pvFindPara のデータ型: CTL_FIND_USAGE_PARA

まったく同じ使用法識別子を持つ CTL のみが一致します。追加の使用法識別子を持つ CTL は一致しません。たとえば、CTL_FIND_USAGE_PARA 構造体に "1.2.3" のみが指定されている場合、一致するには CTL が "1.2.3" のみを含み、追加の使用法識別子を含んでいてはなりません。

CTL_FIND_EXISTING
pvFindPara のデータ型: PCCTL_CONTEXT

CTL_CONTEXT と完全に一致する次の CRL を検索します。

CTL_FIND_SUBJECT
pvFindPara のデータ型: CTL_FIND_SUBJECT_PARA

指定されたサブジェクトを持つ CTL が検索されます。CertFindSubjectInCTL を呼び出して、CTL 内のサブジェクトのエントリへのポインターを取得できます。CTL_FIND_SUBJECT_PARA 内の pUsagePara メンバーを設定すると、上記の CTL_FIND_USAGE で説明した一致処理を有効にできます (省略可能)。

pvFindParavoid*inoptionaldwFindType パラメーターに関連付けられた検索値へのポインター。
pPrevCtlContextCTL_CONTEXT*inoptionalこの関数によって最後に返された CTL_CONTEXT へのポインター。ストア内の最初の CTL を取得するには、NULL にする必要があります。後続の CTL は、pPrevCtlContext を以前の関数呼び出しで返された CTL_CONTEXT へのポインターに設定することで取得します。検索条件を満たさない証明書、または CertDeleteCTLFromStore によって以前に削除された証明書はスキップされます。この関数は、このパラメーターが NULL 以外の値である場合に参照される CTL_CONTEXT を解放します。

戻り値の型: CTL_CONTEXT*

公式ドキュメント

dwFindType とそれに関連付けられた pvFindPara で指定された検索条件に一致する、最初または次の証明書信頼リスト (CTL) コンテキストを検索します。

戻り値

関数が成功した場合、戻り値は読み取り専用の CTL コンテキストへのポインターです。

拡張エラー情報を取得するには、 GetLastError を呼び出します。発生する可能性のあるエラーコードを次に示します。

戻りコード 説明
CRYPT_E_NOT_FOUND
ストア内に CTL が見つからなかった、検索条件に一致する CTL が見つからなかった、または関数がストアのリストの末尾に達したかのいずれかです。
E_INVALIDARG
hCertStore パラメーターのハンドルが pPrevCtlContext パラメーターが指す CTL コンテキスト内のハンドルと一致しない、または dwFindType パラメーターに無効な値が指定されました。

解説(Remarks)

返されたポインターは、この関数への後続の呼び出しで pPrevCtlContext として渡されると解放されます。それ以外の場合は、 CertFreeCTLContext を呼び出してポインターを解放する必要があります。関数に渡された NULL 以外の pPrevCtlContext は、関数がエラーを生成した場合でも、常に CertFreeCTLContext の呼び出しによって解放されます。

CertDuplicateCTLContext を呼び出して、返されたコンテキストの複製を作成できます。返された CTL コンテキストは、 CertAddCTLContextToStore を使用して別の証明書ストアに追加できます。また、 CertAddCTLLinkToStore を使用して、その CTL コンテキストへのリンクを非コレクションストアに追加できます。検索条件に一致する CTL が見つからない場合は、NULL が返されます。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

CTL_CONTEXT* CertFindCTLInStore(
    HCERTSTORE hCertStore,
    DWORD dwMsgAndCertEncodingType,
    DWORD dwFindFlags,
    CERT_FIND_TYPE dwFindType,
    const void* pvFindPara,   // optional
    CTL_CONTEXT* pPrevCtlContext   // optional
);
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CertFindCTLInStore(
    IntPtr hCertStore,   // HCERTSTORE
    uint dwMsgAndCertEncodingType,   // DWORD
    uint dwFindFlags,   // DWORD
    uint dwFindType,   // CERT_FIND_TYPE
    IntPtr pvFindPara,   // void* optional
    IntPtr pPrevCtlContext   // CTL_CONTEXT* optional
);
<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CertFindCTLInStore(
    hCertStore As IntPtr,   ' HCERTSTORE
    dwMsgAndCertEncodingType As UInteger,   ' DWORD
    dwFindFlags As UInteger,   ' DWORD
    dwFindType As UInteger,   ' CERT_FIND_TYPE
    pvFindPara As IntPtr,   ' void* optional
    pPrevCtlContext As IntPtr   ' CTL_CONTEXT* optional
) As IntPtr
End Function
' hCertStore : HCERTSTORE
' dwMsgAndCertEncodingType : DWORD
' dwFindFlags : DWORD
' dwFindType : CERT_FIND_TYPE
' pvFindPara : void* optional
' pPrevCtlContext : CTL_CONTEXT* optional
Declare PtrSafe Function CertFindCTLInStore Lib "crypt32" ( _
    ByVal hCertStore As LongPtr, _
    ByVal dwMsgAndCertEncodingType As Long, _
    ByVal dwFindFlags As Long, _
    ByVal dwFindType As Long, _
    ByVal pvFindPara As LongPtr, _
    ByVal pPrevCtlContext As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CertFindCTLInStore = ctypes.windll.crypt32.CertFindCTLInStore
CertFindCTLInStore.restype = ctypes.c_void_p
CertFindCTLInStore.argtypes = [
    wintypes.HANDLE,  # hCertStore : HCERTSTORE
    wintypes.DWORD,  # dwMsgAndCertEncodingType : DWORD
    wintypes.DWORD,  # dwFindFlags : DWORD
    wintypes.DWORD,  # dwFindType : CERT_FIND_TYPE
    ctypes.POINTER(None),  # pvFindPara : void* optional
    ctypes.c_void_p,  # pPrevCtlContext : CTL_CONTEXT* optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCertFindCTLInStore = crypt32.NewProc("CertFindCTLInStore")
)

// hCertStore (HCERTSTORE), dwMsgAndCertEncodingType (DWORD), dwFindFlags (DWORD), dwFindType (CERT_FIND_TYPE), pvFindPara (void* optional), pPrevCtlContext (CTL_CONTEXT* optional)
r1, _, err := procCertFindCTLInStore.Call(
	uintptr(hCertStore),
	uintptr(dwMsgAndCertEncodingType),
	uintptr(dwFindFlags),
	uintptr(dwFindType),
	uintptr(pvFindPara),
	uintptr(pPrevCtlContext),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // CTL_CONTEXT*
function CertFindCTLInStore(
  hCertStore: THandle;   // HCERTSTORE
  dwMsgAndCertEncodingType: DWORD;   // DWORD
  dwFindFlags: DWORD;   // DWORD
  dwFindType: DWORD;   // CERT_FIND_TYPE
  pvFindPara: Pointer;   // void* optional
  pPrevCtlContext: Pointer   // CTL_CONTEXT* optional
): Pointer; stdcall;
  external 'CRYPT32.dll' name 'CertFindCTLInStore';
result := DllCall("CRYPT32\CertFindCTLInStore"
    , "Ptr", hCertStore   ; HCERTSTORE
    , "UInt", dwMsgAndCertEncodingType   ; DWORD
    , "UInt", dwFindFlags   ; DWORD
    , "UInt", dwFindType   ; CERT_FIND_TYPE
    , "Ptr", pvFindPara   ; void* optional
    , "Ptr", pPrevCtlContext   ; CTL_CONTEXT* optional
    , "Ptr")   ; return: CTL_CONTEXT*
●CertFindCTLInStore(hCertStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext) = DLL("CRYPT32.dll", "void* CertFindCTLInStore(void*, dword, dword, dword, void*, void*)")
# 呼び出し: CertFindCTLInStore(hCertStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext)
# hCertStore : HCERTSTORE -> "void*"
# dwMsgAndCertEncodingType : DWORD -> "dword"
# dwFindFlags : DWORD -> "dword"
# dwFindType : CERT_FIND_TYPE -> "dword"
# pvFindPara : void* optional -> "void*"
# pPrevCtlContext : CTL_CONTEXT* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "crypt32" fn CertFindCTLInStore(
    hCertStore: ?*anyopaque, // HCERTSTORE
    dwMsgAndCertEncodingType: u32, // DWORD
    dwFindFlags: u32, // DWORD
    dwFindType: u32, // CERT_FIND_TYPE
    pvFindPara: ?*anyopaque, // void* optional
    pPrevCtlContext: [*c]CTL_CONTEXT // CTL_CONTEXT* optional
) callconv(std.os.windows.WINAPI) [*c]CTL_CONTEXT;
proc CertFindCTLInStore(
    hCertStore: pointer,  # HCERTSTORE
    dwMsgAndCertEncodingType: uint32,  # DWORD
    dwFindFlags: uint32,  # DWORD
    dwFindType: uint32,  # CERT_FIND_TYPE
    pvFindPara: pointer,  # void* optional
    pPrevCtlContext: ptr CTL_CONTEXT  # CTL_CONTEXT* optional
): ptr CTL_CONTEXT {.importc: "CertFindCTLInStore", stdcall, dynlib: "CRYPT32.dll".}
pragma(lib, "crypt32");
extern(Windows)
CTL_CONTEXT* CertFindCTLInStore(
    void* hCertStore,   // HCERTSTORE
    uint dwMsgAndCertEncodingType,   // DWORD
    uint dwFindFlags,   // DWORD
    uint dwFindType,   // CERT_FIND_TYPE
    void* pvFindPara,   // void* optional
    CTL_CONTEXT* pPrevCtlContext   // CTL_CONTEXT* optional
);
ccall((:CertFindCTLInStore, "CRYPT32.dll"), stdcall, Ptr{CTL_CONTEXT},
      (Ptr{Cvoid}, UInt32, UInt32, UInt32, Ptr{Cvoid}, Ptr{CTL_CONTEXT}),
      hCertStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext)
# hCertStore : HCERTSTORE -> Ptr{Cvoid}
# dwMsgAndCertEncodingType : DWORD -> UInt32
# dwFindFlags : DWORD -> UInt32
# dwFindType : CERT_FIND_TYPE -> UInt32
# pvFindPara : void* optional -> Ptr{Cvoid}
# pPrevCtlContext : CTL_CONTEXT* optional -> Ptr{CTL_CONTEXT}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* CertFindCTLInStore(
    void* hCertStore,
    uint32_t dwMsgAndCertEncodingType,
    uint32_t dwFindFlags,
    uint32_t dwFindType,
    void* pvFindPara,
    void* pPrevCtlContext);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CertFindCTLInStore(hCertStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext)
-- hCertStore : HCERTSTORE
-- dwMsgAndCertEncodingType : DWORD
-- dwFindFlags : DWORD
-- dwFindType : CERT_FIND_TYPE
-- pvFindPara : void* optional
-- pPrevCtlContext : CTL_CONTEXT* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CertFindCTLInStore = lib.func('__stdcall', 'CertFindCTLInStore', 'void *', ['void *', 'uint32_t', 'uint32_t', 'uint32_t', 'void *', 'void *']);
// CertFindCTLInStore(hCertStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext)
// hCertStore : HCERTSTORE -> 'void *'
// dwMsgAndCertEncodingType : DWORD -> 'uint32_t'
// dwFindFlags : DWORD -> 'uint32_t'
// dwFindType : CERT_FIND_TYPE -> 'uint32_t'
// pvFindPara : void* optional -> 'void *'
// pPrevCtlContext : CTL_CONTEXT* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("CRYPT32.dll", {
  CertFindCTLInStore: { parameters: ["pointer", "u32", "u32", "u32", "pointer", "pointer"], result: "pointer" },
});
// lib.symbols.CertFindCTLInStore(hCertStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext)
// hCertStore : HCERTSTORE -> "pointer"
// dwMsgAndCertEncodingType : DWORD -> "u32"
// dwFindFlags : DWORD -> "u32"
// dwFindType : CERT_FIND_TYPE -> "u32"
// pvFindPara : void* optional -> "pointer"
// pPrevCtlContext : CTL_CONTEXT* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* CertFindCTLInStore(
    void* hCertStore,
    uint32_t dwMsgAndCertEncodingType,
    uint32_t dwFindFlags,
    uint32_t dwFindType,
    void* pvFindPara,
    void* pPrevCtlContext);
C, "CRYPT32.dll");
// $ffi->CertFindCTLInStore(hCertStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCtlContext);
// hCertStore : HCERTSTORE
// dwMsgAndCertEncodingType : DWORD
// dwFindFlags : DWORD
// dwFindType : CERT_FIND_TYPE
// pvFindPara : void* optional
// pPrevCtlContext : CTL_CONTEXT* optional
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Crypt32 extends StdCallLibrary {
    Crypt32 INSTANCE = Native.load("crypt32", Crypt32.class);
    Pointer CertFindCTLInStore(
        Pointer hCertStore,   // HCERTSTORE
        int dwMsgAndCertEncodingType,   // DWORD
        int dwFindFlags,   // DWORD
        int dwFindType,   // CERT_FIND_TYPE
        Pointer pvFindPara,   // void* optional
        Pointer pPrevCtlContext   // CTL_CONTEXT* optional
    );
}
@[Link("crypt32")]
lib LibCRYPT32
  fun CertFindCTLInStore = CertFindCTLInStore(
    hCertStore : Void*,   # HCERTSTORE
    dwMsgAndCertEncodingType : UInt32,   # DWORD
    dwFindFlags : UInt32,   # DWORD
    dwFindType : UInt32,   # CERT_FIND_TYPE
    pvFindPara : Void*,   # void* optional
    pPrevCtlContext : CTL_CONTEXT*   # CTL_CONTEXT* optional
  ) : CTL_CONTEXT*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CertFindCTLInStoreNative = Pointer<Void> Function(Pointer<Void>, Uint32, Uint32, Uint32, Pointer<Void>, Pointer<Void>);
typedef CertFindCTLInStoreDart = Pointer<Void> Function(Pointer<Void>, int, int, int, Pointer<Void>, Pointer<Void>);
final CertFindCTLInStore = DynamicLibrary.open('CRYPT32.dll')
    .lookupFunction<CertFindCTLInStoreNative, CertFindCTLInStoreDart>('CertFindCTLInStore');
// hCertStore : HCERTSTORE -> Pointer<Void>
// dwMsgAndCertEncodingType : DWORD -> Uint32
// dwFindFlags : DWORD -> Uint32
// dwFindType : CERT_FIND_TYPE -> Uint32
// pvFindPara : void* optional -> Pointer<Void>
// pPrevCtlContext : CTL_CONTEXT* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CertFindCTLInStore(
  hCertStore: THandle;   // HCERTSTORE
  dwMsgAndCertEncodingType: DWORD;   // DWORD
  dwFindFlags: DWORD;   // DWORD
  dwFindType: DWORD;   // CERT_FIND_TYPE
  pvFindPara: Pointer;   // void* optional
  pPrevCtlContext: Pointer   // CTL_CONTEXT* optional
): Pointer; stdcall;
  external 'CRYPT32.dll' name 'CertFindCTLInStore';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CertFindCTLInStore"
  c_CertFindCTLInStore :: Ptr () -> Word32 -> Word32 -> Word32 -> Ptr () -> Ptr () -> IO (Ptr ())
-- hCertStore : HCERTSTORE -> Ptr ()
-- dwMsgAndCertEncodingType : DWORD -> Word32
-- dwFindFlags : DWORD -> Word32
-- dwFindType : CERT_FIND_TYPE -> Word32
-- pvFindPara : void* optional -> Ptr ()
-- pPrevCtlContext : CTL_CONTEXT* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let certfindctlinstore =
  foreign "CertFindCTLInStore"
    ((ptr void) @-> uint32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> (ptr void) @-> returning (ptr void))
(* hCertStore : HCERTSTORE -> (ptr void) *)
(* dwMsgAndCertEncodingType : DWORD -> uint32_t *)
(* dwFindFlags : DWORD -> uint32_t *)
(* dwFindType : CERT_FIND_TYPE -> uint32_t *)
(* pvFindPara : void* optional -> (ptr void) *)
(* pPrevCtlContext : CTL_CONTEXT* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)

(cffi:defcfun ("CertFindCTLInStore" cert-find-ctlin-store :convention :stdcall) :pointer
  (h-cert-store :pointer)   ; HCERTSTORE
  (dw-msg-and-cert-encoding-type :uint32)   ; DWORD
  (dw-find-flags :uint32)   ; DWORD
  (dw-find-type :uint32)   ; CERT_FIND_TYPE
  (pv-find-para :pointer)   ; void* optional
  (p-prev-ctl-context :pointer))   ; CTL_CONTEXT* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CertFindCTLInStore = Win32::API::More->new('CRYPT32',
    'LPVOID CertFindCTLInStore(HANDLE hCertStore, DWORD dwMsgAndCertEncodingType, DWORD dwFindFlags, DWORD dwFindType, LPVOID pvFindPara, LPVOID pPrevCtlContext)');
# my $ret = $CertFindCTLInStore->Call($hCertStore, $dwMsgAndCertEncodingType, $dwFindFlags, $dwFindType, $pvFindPara, $pPrevCtlContext);
# hCertStore : HCERTSTORE -> HANDLE
# dwMsgAndCertEncodingType : DWORD -> DWORD
# dwFindFlags : DWORD -> DWORD
# dwFindType : CERT_FIND_TYPE -> DWORD
# pvFindPara : void* optional -> LPVOID
# pPrevCtlContext : CTL_CONTEXT* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型