異なる場所でのストアの使用

[CAPICOM は、Windows Server 2008、Windows Vista、および Windows XP のオペレーティング システムで使用できる 32 ビットのみのコンポーネントです。 代わりに、.NET Frameworkを使用してセキュリティ機能を実装します。 詳細については、「 CAPICOM の使用に代わる方法」を参照してください。

次の例では、 Store オブジェクトを操作する側面を示します。 CAPICOM_MEMORY_STORE、CAPICOM_CURRENT_USER_STORE、CAPICOM_LOCAL_MACHINE_STOREの各場所に店舗を開く方法が表示されます。

この例では、開いているストアから証明書をエクスポートし、エクスポートされた証明書をファイルに書き込み、それらを読み取り戻して別のストアにインポートする方法を示します。 新しくインポートされた証明書が列挙され、表示されます。

CAPICOM エラーでは、 Err.Number の負の 10 進値が返されます。 詳細については、「 CAPICOM_ERROR_CODE」を参照してください。 Err.Number の正の 10 進数の値については、「Winerror.h」を参照してください。

Sub OpenStores()

    On Error GoTo ErrorHandler

    'Open Memory, current user, and local machine stores
    Dim MemoryStore As New Store
    Dim CurrentUserStore As New Store
    Dim LocalMachineStore As New Store
    Dim NumCerts As Integer

    MemoryStore.Open(CAPICOM_MEMORY_STORE, "MemStore", _
        CAPICOM_STORE_OPEN_READ_WRITE)
    CurrentUserStore.Open(CAPICOM_CURRENT_USER_STORE, _
        CAPICOM_MY_STORE, CAPICOM_STORE_OPEN_READ_ONLY)
    LocalMachineStore.Open(CAPICOM_LOCAL_MACHINE_STORE, _
        CAPICOM_CA_STORE, CAPICOM_STORE_OPEN_READ_ONLY)

    ' Check the number of certificates in the stores.
    NumCerts = MemoryStore.Certificates.Count
    MsgBox("There are " & NumCerts & _
        " certificates in the memory store. ")
    NumCerts = CurrentUserStore.Certificates.Count
    MsgBox("There are " & NumCerts & _
        " certificates in the current user store. ")
    NumCerts = LocalMachineStore.Certificates.Count
    MsgBox("There are " & NumCerts & _
        " certificates in the local machine store. ")


    '  Export the certificates in the current user MY store
    '  to a file.
    Dim ExportString As String
    ExportString = CurrentUserStore.Export
Open "Exportcerts.txt" For Output As #1
Write #1, ExportString
Close #1

    ' Read the store the file.
    Dim importString As String
Open "exportcerts.txt" For Input As #2
Input #2, importString
Close #2
    MemoryStore.Import(importString)

    ' Check the number of certificates in the memory store after 
    ' import()
    NumCerts = MemoryStore.Certificates.Count
    MsgBox("There are " & NumCerts & _
        " certificates now in the memory store. ")

    ' Enumerate the certificates in the memory store and display each
    ' certificate
    Dim i As Long
    For i = 1 To NumCerts
        MemoryStore.Certificates.Item(i).Display()
    Next i

    ' Release the store objects
    MemoryStore = Nothing
    CurrentUserStore = Nothing
    LocalMachineStore = Nothing
    Exit Sub
ErrorHandler:
    If Err.Number > 0 Then
        MsgBox("Visual Basic error found: " & Err.Description)
    Else
        MsgBox("CAPICOM error found : " & Err.Number)
    End If
End Sub