다음을 통해 공유


다른 위치에 저장소 사용

[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