CertEnumCertificatesInStore 函式 (wincrypt.h)
CertEnumCertificatesInStore 函式會擷取證書存儲中的第一個或下一個憑證。 在迴圈中使用,此函式可以依序擷取證書存儲中的所有憑證。
語法
PCCERT_CONTEXT CertEnumCertificatesInStore(
[in] HCERTSTORE hCertStore,
[in] PCCERT_CONTEXT pPrevCertContext
);
參數
[in] hCertStore
證書存儲的句柄。
[in] pPrevCertContext
指向上一個憑證內容之CERT_CONTEXT的指標。
此參數必須是 NULL ,才能開始列舉,並取得存放區中的第一個憑證。 後續憑證的列舉方式是將 pPrevCertContext 設定為先前呼叫函式所傳回的指標。 此函式會釋放此參數的非 NULL 值所參考的CERT_CONTEXT。
對於 邏輯存放區,包括集合存放區,此函式傳回的 pCertContext 複本無法用來開始列舉的新子序列,因為重複的憑證會失去初始列舉 狀態。 列舉會略過 先前由 CertDeleteCertificateFromStore 刪除的任何憑證。
傳回值
如果函式成功,函式會傳回存放區中下一 個CERT_CONTEXT 的指標。 如果存放區中沒有任何憑證存在,函式會傳回 NULL。
如需擴充的錯誤資訊,請呼叫 GetLastError。 接下來有一些可能的錯誤碼。
值 | Description |
---|---|
|
hCertStore 參數中的句柄與 pPrevCertContext 所指向之憑證內容中的句柄不同。 |
|
找不到憑證。 如果存放區是空的,或函式到達存放區清單的結尾,就會發生這種情況。 |
|
適用於外部存放區。 找不到憑證。 如果存放區是空的,或函式到達存放區清單的結尾,就會發生這種情況。 |
備註
在後續呼叫上當做 pPrevCertContext 參數傳遞時,會釋放傳回的指標。 否則,必須呼叫 CertFreeCertificateContext 來釋放指標。 傳遞至 CertEnumCertificatesInStore 的非 NULLpPrevCertContext 一律會釋出,即使發生錯誤也一樣。
呼叫 CertDuplicateCertificateContext,即可建立目前列舉憑證的重複專案。
範例
下列範例會列出證書存儲中的憑證內容。 如需使用此函式的另一個範例,請參閱 範例 C 程式:從證書存儲刪除憑證。
#include <windows.h>
#include <stdio.h>
#include <Wincrypt.h>
#pragma comment(lib, "crypt32.lib")
//--------------------------------------------------------------------
// Declare and initialize variables.
HANDLE hStoreHandle = NULL;
PCCERT_CONTEXT pCertContext = NULL;
char * pszStoreName = "CA";
//--------------------------------------------------------------------
// Open a system certificate store.
if (hStoreHandle = CertOpenSystemStore(
NULL,
pszStoreName))
{
printf("The %s store has been opened. \n", pszStoreName);
}
else
{
printf("The store was not opened.\n");
exit(1);
}
//-------------------------------------------------------------------
// Find the certificates in the system store.
while(pCertContext= CertEnumCertificatesInStore(
hStoreHandle,
pCertContext)) // on the first call to the function,
// this parameter is NULL
// on all subsequent calls,
// this parameter is the last pointer
// returned by the function
{
//----------------------------------------------------------------
// Do whatever is needed for a current certificate.
// ...
} // End of while.
//--------------------------------------------------------------------
// Clean up.
if (!CertCloseStore(
hStoreHandle,
0))
{
printf("Failed CertCloseStore\n");
exit(1);
}
規格需求
需求 | 值 |
---|---|
最低支援的用戶端 | Windows XP [傳統型應用程式 |UWP 應用程式] |
最低支援的伺服器 | Windows Server 2003 [傳統型應用程式 |UWP 應用程式] |
目標平台 | Windows |
標頭 | wincrypt.h |
程式庫 | Crypt32.lib |
Dll | Crypt32.dll |
另請參閱
CertDeleteCertificateFromStore
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應