VC 安装证书

root  : 受信任的根证书颁发机构
trust 受信任的发布者
ca    中级证书颁发机构
my 个人证书
 
 
下面是导入root证书的,其它的改一下即可
 
BOOL ImportCACert(ATL::CString & m_pathCA)
{
  HCERTSTORE pfxStore = 0;
  HCERTSTORE myStore = 0;
  HCERTSTORE hFileStore = 0;
  HANDLE hsection = 0;
  void* pfx = NULL;
  HANDLE hfile = INVALID_HANDLE_VALUE;
  PCCERT_CONTEXT pctx = NULL;
  // Get path of the CA certificate from the edit box
  // Open it...
  hfile = CreateFile(m_pathCA, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
 
  // FOR WINDOWS 98 .... 
  // hfile = CreateFile(m_pathCA, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  if (INVALID_HANDLE_VALUE == hfile)
  {
    MessageBox(NULL,_T("Certificate not found. Check that the path indicated is correct."),_T("information"), MB_ICONERROR);
    return 0;
  }
  hsection = CreateFileMapping(hfile, 0, PAGE_READONLY, 0, 0, 0);
  if (!hsection)
  {
    //AfxMessageBox("Error in 'CreateFileMapping'", MB_ICONERROR);
    FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore);   
    return 0;
  }
  pfx = MapViewOfFile(hsection, FILE_MAP_READ, 0, 0, 0);
  if (!pfx)
  {
    //AfxMessageBox("Error in 'MapViewOfFile'", MB_ICONERROR);
    FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
    return 0;
  }
  int nFilesize=GetFileSize(hfile,0);
  pctx = CertCreateCertificateCon text(MY_ENCODING_TYPE, (BYTE*)pfx,nFilesize );
  if(pctx == NULL)
  {
    //AfxMessageBox("Error in 'CertCreateCertificateCon text'", MB_ICONERROR);
    FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
    return 0;
  }
  // we open the store for the CA
  hFileStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_STORE_OPEN_EXISTING_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root" );
   
  if (!hFileStore)
  {
    //AfxMessageBox("Error in 'CertOpenStore'", MB_ICONERROR);
    FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
    return 0;
  }
  if(!CertAddCertificateContex tToStore(hFileStore, pctx, CERT_STORE_ADD_NEW, 0))
  {
   
    DWORD err = GetLastError();
    if (CRYPT_E_EXISTS == err)
    {
      if(MessageBox(NULL,_T("An equivalent previous personal certificate already exists. Overwrite ? (Yes/No)"),_T("Prompt"), MB_YESNO) == IDYES)
      {
        if (!CertAddCertificateContex tToStore(hFileStore, pctx, CERT_STORE_ADD_REPLACE_EXISTING, 0))
        {
        //  AfxMessageBox("Error in 'CertAddCertificateContex tToStore'", MB_ICONERROR);
          FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
          return 0;
        }
      }
    }
    else
    {
      //AfxMessageBox("Error in 'CertAddCertificateContex tToStore'", MB_ICONERROR);
      FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
      return 0;
    }
  }
  return 1;
}

你可能感兴趣的:(VC 安装证书)