CertEnumCertificatesInStore

The CertEnumCertificatesInStore function enumerates all the certificate contexts in the store.

#include <wincrypt.h>
PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(
  HCERTSTORE hCertStore               // in
  PCCERT_CONTEXT pPrevCertContext     // in
);
 

Parameters

hCertStore
Handle to the certificate store.
pPrevCertContext
Pointer to the previous certificate context found. For more details, see the Remarks section.

Return Values

The function returns a pointer to the next CERT_CONTEXT in the store. If no more certificates exist in the store, it returns NULL.

GetLastError may be called to indicate the reason for any failure. This function uses the following error codes:

Error code Description
E_INVALIDARG The hCertStore argument is not the same as the hCertStore of the certificate context pointed to by pPrevCertContext.
CRYPT_E_NOT_FOUND No certificates were found. This happens if the store is empty or if the function reached the end of the store's list.

Remarks

The pPrevCertContext parameter must be NULL to get the first certificate in the store. Successive certificates are enumerated by setting pPrevCertContext to the pointer returned by a previous call to the function. The enumeration skips any CTLs previously deleted by CertDeleteCertificateFromStore.

The returned pointer is freed when passed as the pPrevCertContext on a subsequent call. Otherwise, the pointer must be freed by calling CertFreeCertificateContext. A pPrevCertContext that is not NULL is always freed by this function even for an error.

A duplicate of the currently enumerated certificate can be made by calling CertDuplicateCertificateContext.

Example

// List the certificates in a store using CertEnumCertificatesInStore
HCERTSTORE             hCertStore;
PCCERT_CONTEXT          pCertContext = NULL;
int                    i=0;
//
// Open a file based certificate store. 
// For details, see CertOpenStore.
//
if (hCertStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,
0,
NULL,
CERT_SYSTEM_STORE_CURRENT_USER,
L"MY"))
printf("The file based store is open. Continue.\n");
else
handle_error("The store did not open.");
// In a loop, retrieve each of the certificates in the store.
while(pCertContext= CertEnumCertificatesInStore(
   hCertStore,        // The certificate store being enumerated.
   pCertContext))     // The previous certificate. Must be NULL
                      // for the first call to the function.
printf("%d Another certificate found.\n",++i);
//
// At the end of the loop, print a completion message.
printf("\n\nAll certificates in the store have been enumerated.\n");
 

QuickInfo

  Windows NT: Requires version 4.0 SP3 or later. Available also in IE 3.02 and later.
  Windows: Requires Windows 95 OSR2 or later.
  Windows CE: Unsupported.
  Header: Declared in wincrypt.h.
  Import Library: Use crypt32.lib.

See Also

CertFindCertificateInStore, CertFindCRLInStore, CertFindCTLInStore