The CertDuplicateCertificateContext function duplicates a certificate context by incrementing the reference count. The reference count is used to keep track of the lifetime of the context.
#include <wincrypt.h>
PCCERT_CONTEXT WINAPI CertDuplicateCertificateContext(
PCCERT_CONTEXT pCertContext // in
);
Currently, a copy is not made of the context, and the returned context is the same as the context that was input.
// handle_error() is a function defined in a separate file.
HANDLE hStoreHandle;
PCCERT_CONTEXT pCertContext;
PCCERT_CONTEXT pDupCertContext;
// Open a certificate store. For details, see CertOpenStore.
if(hStoreHandle = CertOpenStore(
CERT_STORE_PROV_SYSTEM,0,NULL,CERT_SYSTEM_STORE_CURRENT_USER,
L"MY"))
printf("The MY store is open. Continue. \n");
else
handle_error("The store did not open.");
// Get the first certificate in the system store.
if(pCertContext= CertEnumCertificatesInStore(
hStoreHandle,NULL)))
printf("A certificate has been retrieved. Continue. \n");
else
handle_error("Certificate not retrieved. The store may be empty.");
// Close the certificate store. For details, see CertCloseStore.
if(CertCloseStore(hStoreHandle,CERT_CLOSE_STORE_CHECK_FLAG))
// If the function returns TRUE, no certificates remained open.
printf("The store was closed and no certs are still open.\n");
else
// The function returned FALSE. The store is closed, but
// certificates remain open.
printf("Certificates remain open.\n");
// Create a duplicate of the certificate
if(pDupCertContext = CertDuplicateCertificateContext(
pCertContext // The certificate to be duplicated.
))
printf("A duplicate was created. Continue. \n");
else
handle_error("Duplication of the certificate failed.");
// Use the certificate contexts as necessary.
// ....
// Free the duplicate certificate.
if(CertFreeCertificateContext(pDupCertContext))
printf("CertDuplicateCertificateContext succeeded. Continue.\n");
else
handle_error("The duplicate cert could not be freed.");
// Free the original certificate.
if(!(CertFreeCertificateContext(pCertContext)))
handle_error("The original certificate was not freed.");
if(!(CertCloseStore(hStoreHandle,0)))
handle_error("The certificate store was not closed.");
printf("The program terminated normally.\n");
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.