CertCloseStore
関数シグネチャ
// CRYPT32.dll
#include <windows.h>
BOOL CertCloseStore(
HCERTSTORE hCertStore, // optional
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hCertStore | HCERTSTORE | inoptional | 閉じる対象の証明書ストアのハンドル。 |
| dwFlags | DWORD | in | 通常、このパラメーターには既定値の 0 を使用します。既定では、解放されていないコンテキスト用のメモリを割り当てたままストアを閉じます。この場合、コンテキスト用のメモリが割り当てられたままかどうかの確認は行われません。 フラグを設定すると、ストアを閉じる際に、そのストアのすべての 証明書、証明書失効リスト (CRL)、および 証明書信頼リスト (CTL) のコンテキスト用メモリを強制的に解放できます。また、ストアのすべての証明書、CRL、CTL の各コンテキストが解放されたかどうかを確認するフラグを設定することもできます。次の値が定義されています。 |
戻り値の型: BOOL
公式ドキュメント
証明書ストアのハンドルを閉じ、ストアの参照カウントを減らします。
戻り値
関数が成功した場合、戻り値は TRUE です。
関数が失敗した場合、戻り値は FALSE です。拡張エラー情報を取得するには、 GetLastError を呼び出します。
CERT_CLOSE_STORE_CHECK_FLAG が設定されていない場合、または設定されていてストアに関連付けられたすべてのコンテキストが解放されている場合、戻り値は TRUE です。
CERT_CLOSE_STORE_CHECK_FLAG が設定されており、ストアに関連付けられた 1 つ以上のコンテキスト用のメモリが割り当てられたままになっている場合、戻り値は FALSE です。関数が FALSE を返す場合でも、ストアは常に閉じられます。詳細については「解説」を参照してください。
ストアに関連付けられたコンテキスト用のメモリが割り当てられたままの場合、GetLastError には CRYPT_E_PENDING_CLOSE が設定されます。CERT_CLOSE_STORE_CHECK_FLAG が設定されていない限り、GetLastError が返す既存の値は保持されます。
解説(Remarks)
証明書ストアが開いている間は、そのストアの コンテキスト を取得または複製できます。コンテキスト を取得または複製すると、その参照カウントが増加します。コンテキストを、検索関数や列挙関数に直前のコンテキストとして渡すことによって、または CertFreeCertificateContext、 CertFreeCRLContext、 CertFreeCTLContext を使用して解放すると、その参照カウントが減少します。コンテキストの参照カウントが 0 になると、そのコンテキストに割り当てられていたメモリは自動的に解放されます。コンテキストに割り当てられていたメモリが解放されると、そのコンテキストへのポインターはすべて無効になります。
既定では、参照カウントが 0 より大きいコンテキストを格納するために使用されているメモリは、証明書ストアを閉じても解放されません。それらのコンテキストへの参照は有効なままですが、これによりメモリリークが発生する可能性があります。また、ストアを閉じた後にコンテキストのプロパティに加えた変更は永続化されません。
ストアに関連付けられたすべてのコンテキスト用のメモリを強制的に解放するには、CERT_CLOSE_STORE_FORCE_FLAG を設定します。このフラグを設定すると、ストアに関連付けられたすべてのコンテキスト用のメモリが解放され、ストアに関連付けられた証明書、CRL、CTL の各コンテキストへのポインターはすべて無効になります。このフラグは、ストアを 1 つの関数内で開き、ストアハンドルおよびそのコンテキストのいずれも、呼び出した関数に一度も渡していない場合にのみ設定してください。
ストアに関連付けられたコンテキストの参照カウントの状態は、ストアを閉じる際に CERT_CLOSE_STORE_CHECK_FLAG を使用して確認できます。このフラグを設定し、証明書、CRL、CTL のいずれかのコンテキストが解放されていない場合、関数は FALSE を返し、 GetLastError は CRYPT_E_PENDING_CLOSE を返します。FALSE が返された場合でもストアは閉じられ、アクティブなコンテキストのメモリは解放されないことに注意してください。
ストアを開いた際に CERT_STORE_NO_CRYPT_RELEASE_FLAG が設定されていなかった場合、ストアを閉じると、その CSP ハンドルが解放されます。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// CRYPT32.dll
#include <windows.h>
BOOL CertCloseStore(
HCERTSTORE hCertStore, // optional
DWORD dwFlags
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CertCloseStore(
IntPtr hCertStore, // HCERTSTORE optional
uint dwFlags // DWORD
);<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CertCloseStore(
hCertStore As IntPtr, ' HCERTSTORE optional
dwFlags As UInteger ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hCertStore : HCERTSTORE optional
' dwFlags : DWORD
Declare PtrSafe Function CertCloseStore Lib "crypt32" ( _
ByVal hCertStore 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
CertCloseStore = ctypes.windll.crypt32.CertCloseStore
CertCloseStore.restype = wintypes.BOOL
CertCloseStore.argtypes = [
wintypes.HANDLE, # hCertStore : HCERTSTORE optional
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')
CertCloseStore = Fiddle::Function.new(
lib['CertCloseStore'],
[
Fiddle::TYPE_VOIDP, # hCertStore : HCERTSTORE optional
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
Fiddle::TYPE_INT)#[link(name = "crypt32")]
extern "system" {
fn CertCloseStore(
hCertStore: *mut core::ffi::c_void, // HCERTSTORE optional
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 CertCloseStore(IntPtr hCertStore, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CertCloseStore' -Namespace Win32 -PassThru
# $api::CertCloseStore(hCertStore, dwFlags)#uselib "CRYPT32.dll"
#func global CertCloseStore "CertCloseStore" sptr, sptr
; CertCloseStore hCertStore, dwFlags ; 戻り値は stat
; hCertStore : HCERTSTORE optional -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "CRYPT32.dll"
#cfunc global CertCloseStore "CertCloseStore" sptr, int
; res = CertCloseStore(hCertStore, dwFlags)
; hCertStore : HCERTSTORE optional -> "sptr"
; dwFlags : DWORD -> "int"; BOOL CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
#uselib "CRYPT32.dll"
#cfunc global CertCloseStore "CertCloseStore" intptr, int
; res = CertCloseStore(hCertStore, dwFlags)
; hCertStore : HCERTSTORE optional -> "intptr"
; dwFlags : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
procCertCloseStore = crypt32.NewProc("CertCloseStore")
)
// hCertStore (HCERTSTORE optional), dwFlags (DWORD)
r1, _, err := procCertCloseStore.Call(
uintptr(hCertStore),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CertCloseStore(
hCertStore: THandle; // HCERTSTORE optional
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CertCloseStore';result := DllCall("CRYPT32\CertCloseStore"
, "Ptr", hCertStore ; HCERTSTORE optional
, "UInt", dwFlags ; DWORD
, "Int") ; return: BOOL●CertCloseStore(hCertStore, dwFlags) = DLL("CRYPT32.dll", "bool CertCloseStore(void*, dword)")
# 呼び出し: CertCloseStore(hCertStore, dwFlags)
# hCertStore : HCERTSTORE optional -> "void*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "crypt32" fn CertCloseStore(
hCertStore: ?*anyopaque, // HCERTSTORE optional
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc CertCloseStore(
hCertStore: pointer, # HCERTSTORE optional
dwFlags: uint32 # DWORD
): int32 {.importc: "CertCloseStore", stdcall, dynlib: "CRYPT32.dll".}pragma(lib, "crypt32");
extern(Windows)
int CertCloseStore(
void* hCertStore, // HCERTSTORE optional
uint dwFlags // DWORD
);ccall((:CertCloseStore, "CRYPT32.dll"), stdcall, Int32,
(Ptr{Cvoid}, UInt32),
hCertStore, dwFlags)
# hCertStore : HCERTSTORE optional -> Ptr{Cvoid}
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CertCloseStore(
void* hCertStore,
uint32_t dwFlags);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CertCloseStore(hCertStore, dwFlags)
-- hCertStore : HCERTSTORE optional
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CertCloseStore = lib.func('__stdcall', 'CertCloseStore', 'int32_t', ['void *', 'uint32_t']);
// CertCloseStore(hCertStore, dwFlags)
// hCertStore : HCERTSTORE optional -> 'void *'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("CRYPT32.dll", {
CertCloseStore: { parameters: ["pointer", "u32"], result: "i32" },
});
// lib.symbols.CertCloseStore(hCertStore, dwFlags)
// hCertStore : HCERTSTORE optional -> "pointer"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CertCloseStore(
void* hCertStore,
uint32_t dwFlags);
C, "CRYPT32.dll");
// $ffi->CertCloseStore(hCertStore, dwFlags);
// hCertStore : HCERTSTORE optional
// dwFlags : DWORD
// 構造体/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);
boolean CertCloseStore(
Pointer hCertStore, // HCERTSTORE optional
int dwFlags // DWORD
);
}@[Link("crypt32")]
lib LibCRYPT32
fun CertCloseStore = CertCloseStore(
hCertStore : Void*, # HCERTSTORE optional
dwFlags : UInt32 # DWORD
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CertCloseStoreNative = Int32 Function(Pointer<Void>, Uint32);
typedef CertCloseStoreDart = int Function(Pointer<Void>, int);
final CertCloseStore = DynamicLibrary.open('CRYPT32.dll')
.lookupFunction<CertCloseStoreNative, CertCloseStoreDart>('CertCloseStore');
// hCertStore : HCERTSTORE optional -> Pointer<Void>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CertCloseStore(
hCertStore: THandle; // HCERTSTORE optional
dwFlags: DWORD // DWORD
): BOOL; stdcall;
external 'CRYPT32.dll' name 'CertCloseStore';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CertCloseStore"
c_CertCloseStore :: Ptr () -> Word32 -> IO CInt
-- hCertStore : HCERTSTORE optional -> Ptr ()
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let certclosestore =
foreign "CertCloseStore"
((ptr void) @-> uint32_t @-> returning int32_t)
(* hCertStore : HCERTSTORE optional -> (ptr void) *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)
(cffi:defcfun ("CertCloseStore" cert-close-store :convention :stdcall) :int32
(h-cert-store :pointer) ; HCERTSTORE optional
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CertCloseStore = Win32::API::More->new('CRYPT32',
'BOOL CertCloseStore(HANDLE hCertStore, DWORD dwFlags)');
# my $ret = $CertCloseStore->Call($hCertStore, $dwFlags);
# hCertStore : HCERTSTORE optional -> HANDLE
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f CertDuplicateStore — 証明書ストアのハンドルを複製して参照を増やす。
- f CertFreeCRLContext — CRLコンテキストを解放する。
- f CertFreeCTLContext — CTLコンテキストを解放する。
- f CertFreeCertificateContext — 証明書コンテキストを解放する。
- f CertOpenStore — 指定したプロバイダーで証明書ストアを開く。
- f CryptReleaseContext — 暗号化サービスプロバイダーのコンテキストハンドルを解放する。