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。 下面是一些可能的错误代码。

说明
E_INVALIDARG
hCertStore 参数中的句柄与 pPrevCertContext 指向的证书上下文中的句柄不同。
CRYPT_E_NOT_FOUND
未找到证书。 如果存储为空或函数到达存储区列表的末尾,则会发生此情况。
ERROR_NO_MORE_FILES
适用于外部存储。 未找到证书。 如果存储为空或函数到达存储区列表的末尾,则会发生此情况。

注解

在后续调用中作为 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
Library Crypt32.lib
DLL Crypt32.dll

另请参阅

CERT_CONTEXT

CertDeleteCertificateFromStore

CertDuplicateCertificateContext

CertFindCRLInStore

CertFindCTLInStore

CertFindCertificateInStore

CertFreeCertificateContext

证书函数