CertAddStoreToCollection 函式 (wincrypt.h)

CertAddStoreToCollection 函式會將同層級證書存儲新增至集合證書存儲。 將證書存儲新增至集合存放區時,可以使用使用集合存放區之尋找或列舉函式呼叫來擷取所有憑證、 證書吊銷清單 (CRL) ,以及憑證 信任清單 (C) CL。

語法

BOOL CertAddStoreToCollection(
  [in]           HCERTSTORE hCollectionStore,
  [in, optional] HCERTSTORE hSiblingStore,
  [in]           DWORD      dwUpdateFlags,
  [in]           DWORD      dwPriority
);

參數

[in] hCollectionStore

證書存儲的句柄。

[in, optional] hSiblingStore

要加入至集合存放區的同層級存放區句柄。 如需詳細資訊,請參閱<備註>。

[in] dwUpdateFlags

指出是否可以將 憑證、CRL 和 CCL 新增至集合存放區的新同層級存放區成員。 若要啟用新增功能,請將 dwUpdateFlag 設定為 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG。 若要停用新增專案,請將 dwUpdateFlag 設定為零。

[in] dwPriority

設定集合中新存放區的優先順序級,其中零是最低優先順序。 如果為此參數傳遞零,則指定的存放區會附加為集合中的最後一個存放區。 集合中存放區的優先順序層級會決定列舉存放區的順序,以及在嘗試擷取憑證、CRL 或 CTL 時,存放區的搜尋順序。 優先順序層級也會決定要加入新憑證、CRL 或 CTL 之集合的存放區。 如需詳細資訊,請參閱<備註>。

傳回值

如果函式成功,函式會傳回非零,並將新的存放區新增至存放區的集合。

如果函式失敗,它會傳回零,而且未新增存放區。

備註

集合存放區與單一存放區具有相同 的 HCERTSTORE 句柄;因此,幾乎所有套用至任何 證書存儲 的函式也適用於任何集合存放區。 列舉和搜尋程式會跨越集合存放區中的所有存放區;不過,新增存放區連結的 CertAddCertificateLinkToStore 之類的函式無法與集合存放區搭配使用。

將憑證、CRL 或 CTL 新增至集合存放區時,會優先搜尋集合中的同層級存放區清單,以尋找允許新增的第一個存放區。 如果在 CertAddStoreToCollection 呼叫中設定CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG,則會啟用新增。 使用任何新增元素至存放區的函式時,如果允許新增的存放區不會傳回成功,則加法函式會繼續下一個存放區,而不會提供通知。

當使用 CERT_CLOSE_STORE_FORCE_FLAG 的 CertCloseStore 關閉集合存放區及其同層級存放區時,必須在其同層級存放區之前關閉集合存放區。 如果未使用CERT_CLOSE_STORE_FORCE_FLAG,則可以依任何順序關閉商店。

範例

下列範例示範如何將同層級證書存儲新增至集合證書存儲。 如需完整範例,包括此範例的完整內容,請參閱 範例 C 程式:集合和同層級證書存儲作業

//-------------------------------------------------------------------
// Declare and initialize variables.

HCERTSTORE  hCollectionStore = NULL;     // The collection store 
                                         // handle
HCERTSTORE  hMemoryStore = NULL;         // A memory store handle
LPCSTR pszFileName = "TestStor.sto";     // Output file name
LPWSTR pswzFirstCert = L"Full Test Cert";// Subject of the first
                                         // certificate
LPWSTR pswzSecondCert = L"Microsoft";    // Subject of the second 
                                         // certificate
//-------------------------------------------------------------------
// Open a collection certificate store.

if(hCollectionStore = CertOpenStore(
    CERT_STORE_PROV_COLLECTION, // A collection store
    0,                          // Encoding type; not used with a
                                // collection store
    NULL,                       // Use the default provider
    0,                          // No flags
    NULL))                      // Not needed

{
    printf("The collection store was opened. \n");
}
else
{
    printf( "There was an error while opening the "
        "collection store! \n");
    exit(1);
}
//-------------------------------------------------------------------
// Open a new certificate store in memory.

if(hMemoryStore = CertOpenStore(
    CERT_STORE_PROV_MEMORY,    // A memory store
    0,                         // Encoding type; not used with a
                               // memory store
    NULL,                      // Use the default provider
    0,                         // No flags
    NULL))                     // Not needed
{
    printf("The memory store was opened. \n");
}
else
{
    printf( "There was an error while opening the memory store! \n");
    exit(1);
}
//-------------------------------------------------------------------
// Add the memory store as a sibling to the collection store. 
// All certificates in the memory store and any new certificate
// added to the memory store will also be available in the 
// collection
// store.

if(CertAddStoreToCollection(
    hCollectionStore,
    hMemoryStore,
    CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG,  // New certificates can be
                                          // added to the sibling
                                          // store.
    1))                                   // The sibling store's 
                                          // priority.
                                          // Because this is the 
                                          // store with the highest
                                          // priority, certificates
                                          // added to the collection
                                          // store will actually be
                                          // stored in this store.
{
    printf("The memory store was added to the collection store.\n");
}
else
{
    printf("The memory store was not added to the "
        "collection store.\n");
    exit(1);
}


//-------------------------------------------------------------------
// The store handles must be closed.

if(CertCloseStore(hCollectionStore,
                  0))
{
    printf("The collection store was closed. \n");
}
else
{
    printf("There was an error while closing the "
        "collection store! \n");
}

if(CertCloseStore(hMemoryStore, 0))
{
    printf("The memory store was closed. \n");
}
else
{
    printf("There was an error while closing the memory store! \n");
}

規格需求

需求
最低支援的用戶端 Windows XP [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2003 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 wincrypt.h
程式庫 Crypt32.lib
Dll Crypt32.dll

另請參閱

CertRemoveStoreFromCollection

證書存儲函式