The CertAddCertificateContextToStore function adds a certificate context to the certificate store.
#include <wincrypt.h>
BOOL WINAPI CertAddCertificateContextToStore(
HCERTSTORE hCertStore, // in
PCCERT_CONTEXT pCertContext, // in
DWORD dwAddDisposition, // in
PCCERT_CONTEXT *ppStoreContext // out, optional
);
If a matching certificate or a link to a matching certificate does not exist, a new certificate is added.
If a matching certificate or a link to a matching certificate is not found in the store, a new certificate is added to the store.
ppStoreContext can be NULL, indicating that the caller does not want a copy of the added certificate. If a copy is made, it must be freed by using CertFreeCertificateContext.
TRUE if the function succeeded. FALSE if the function failed.
GetLastError returns the reason for any failures using the following error codes:
Errors from the called functions, CertAddEncodedCertificateToStore and CertSetCertificateContextProperty may be propagated to this function.
The certificate context is not duplicated using CertDuplicateCertContext. Instead, the function creates a new copy of the context and adds it to the store.
In addition to the encoded certificate, the function also copies the context's properties, with the exception of the CERT_KEY_PROV_HANDLE_PROP_ID and CERT_KEY_CONTEXT_PROP_ID properties.
// **********************************************************
// Declare variables.
HCERTSTORE hLinkStoreHandle;
HCERTSTORE hOriginalStoreHandle;
PCCERT_CONTEXT DesiredCert = NULL;
//************************************************************
// Open a memory store where a new certificate will be added.
// For details, see CertOpenStore.
hLinkStoreHandle = CertOpenStore(CERT_STORE_PROV_MEMORY,0,NULL,0,NULL);
//*************************************************************
// Open a system store where certificates can be found
// to add to the memory store.
// For details, see CertOpenStore.
hOriginalStoreHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM,0,NULL,
CERT_SYSTEM_STORE_CURRENT_USER,L"MY");
//***************************************************************
// Get the first certificate in the system store.
DesiredCert=CertEnumCertificatesInStore(hOriginalStoreHandle,DesiredCert);
//***************************************************************
// Add a certificate to hLinkStoreHandle store.
if(CertAddCertificateContextToStore(
hLinkStoreHandle, // The store handle.
DesiredCert, // A pointer to a Cert.
CERT_STORE_ADD_USE_EXISTING, // If a matching cert exists,
// use it.
NULL // Do not make any extra copy of
// the certificate.
))
printf("A certificate has been added to the store. \n");
else
printf("A certificate was not added to the store. \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.
CertAddEncodedCertificateToStore, CertSetCertificateContextProperty