ICertEncodeStringArray::Encode method

The Encode method returns an ASN.1-encoded string of the string array stored in this object.

Use the Decode method to decode the encoded string into an CertEncodeStringArray object.

Before using this method, you must call the Reset to size the array and the SetValue method to set each string in the array.

Syntax


HRESULT Encode(
  [out] BSTR *pbstrBinary
);

Parameters

pbstrBinary [out]

A pointer to a BSTR that will contain the encoded string array. When you have finished using the BSTR, free it by calling the SysFreeString function.

Return value

C++

If the method succeeds, the method returns S_OK.

If the method fails, it returns an HRESULT value that indicates the error. For a list of common error codes, see Common HRESULT Values.

VB

The return value is the ASN.1-encoded string array.

Examples


    HRESULT hr = 0;
    ICertEncodeStringArray *pStringArray = NULL;
    WCHAR *paStringValues[] = { L"Hello, world.",
                                L"Have a great day." };
    BSTR bstr = NULL;
    LONG stringType = 0;
    LONG Count = 0;
    LONG i = 0;

    hr = CoInitialize(NULL);
    hr = CoCreateInstance(__uuidof(CCertEncodeStringArray),
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          __uuidof(ICertEncodeStringArray), 
                          (void**)&pStringArray);
    if (FAILED(hr))
    {
        printf("CoCreateInstance failed: 0x%x\n", hr);
        // handle error, free resources
    }

    // Specify the size of the array in the CertEncodeStringArray 
    // object and the type of strings the array will contain.
    hr = pStringArray->Reset(sizeof(paStringValues)/sizeof(WCHAR*), 
        CERT_RDN_UNICODE_STRING);
    if (FAILED(hr))
    {
        printf("ICertEncodeStringArray::Reset failed: 0x%x\n", hr);
        // handle error, free resources
    }

    hr = pStringArray->GetCount(&Count);
    if (FAILED(hr))
    {
        printf("ICertEncodeStringArray::GetCount "
            "failed: 0x%x\n", hr);
        // handle error, free resources
    }

    // Use the values from the hard-coded paStringValues  
    // array to load the object's array.
    for (i=0; i<Count; i++)
    {
        bstr = SysAllocString(paStringValues[i]);
        if (bstr)
        {
            hr = pStringArray->SetValue(i, bstr);
            if (FAILED(hr))
            {
                printf("ICertEncodeStringArray::SetValue "
                    "failed: 0x%x\n", hr);
                // handle error, free resources
            }
            SysFreeString(bstr);
            bstr = NULL;
        }
        else
        {
            printf("Memory allocation error for bstr.\n");
            // handle error, free resources
        }
    }

    hr = pStringArray->GetStringType(&stringType);
    if (FAILED(hr))
    {
        printf("ICertEncodeStringArray::GetStringType "
            "failed: 0x%x\n", hr);
        // handle error, free resources
    }
    if (CERT_RDN_UNICODE_STRING == stringType)
        printf("Array contains strings of type "
            "CERT_RDN_UNICODE_STRING.\n");
    else
        printf("Array contains strings of unexpected type "
            "(type = %ld).\n", stringType);

    // Loop through and print each element of the array.
    printf("Array before encoding...\n");
    for (i=0; i<Count; i++)
    {
        hr = pStringArray->GetValue(i, &bstr);
        if (FAILED(hr))
        {
            printf("ICertEncodeStringArray::GetValue "
                "failed: 0x%x\n", hr);
            // handle error, free resources
        }
        printf("string[%ld] %ws\n", i, bstr);
        SysFreeString(bstr);
        bstr = NULL;
    }

    // Encode the array into a BSTR and store the data for use later.
    hr = pStringArray->Encode(&bstr);
    if (FAILED(hr))
    {
        printf("ICertEncodeStringArray::Encode failed: 0x%x\n", hr);
        // handle error, free resources
    }
    pStringArray->Release();
    pStringArray = NULL;

    // Create a new instance of the CertEncodeStringArray object 
    // that will contain the decoded array.
    hr = CoCreateInstance(__uuidof(CCertEncodeStringArray), 
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          __uuidof(ICertEncodeStringArray), 
                          (void**)&pStringArray);
    if (FAILED(hr))
    {
        printf("CoCreateInstance failed: 0x%x\n", hr);
        // handle error, free resources
    }

    // Decode the array into the new CertEncodeStringArray object.
    hr = pStringArray->Decode(bstr);
    if (FAILED(hr))
    {
        printf("ICertEncodeStringArray::Decode failed: 0x%x\n", hr);
        // handle error, free resources
    }
    SysFreeString(bstr);
    bstr = NULL;

    hr = pStringArray->GetStringType(&stringType);
    if (FAILED(hr))
    {
        printf("ICertEncodeStringArray::GetStringType "
            "failed: 0x%x\n", hr);
        // handle error, free resources
    }
    if (CERT_RDN_UNICODE_STRING == stringType)
        printf("\nArray contains strings of type "
            "CERT_RDN_UNICODE_STRING.\n");
    else
        printf("Array contains strings of unexpected type "
            "(type = %ld).\n", stringType);

    hr = pStringArray->GetCount(&Count);
    if (FAILED(hr))
    {
        printf("ICertEncodeStringArray::GetCount "
            "failed: 0x%x\n", hr);
        // handle error, free resources
    }

    // Loop through and print each element of the array.
    printf("Array after decoding...\n");
    for (i=0; i<Count; i++)
    {
        hr = pStringArray->GetValue(i, &bstr);
        if (FAILED(hr))
        {
            printf("ICertEncodeStringArray::GetValue "
                "failed: 0x%x\n", hr);
            // handle error, free resources
        }
        printf("string[%ld] %ws\n", i, bstr);
        SysFreeString(bstr);
        bstr = NULL;
    }

    pStringArray->Release();
    CoUninitialize();



Requirements

Minimum supported client

None supported

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

Certenc.h (include Certsrv.h)

Library

Certidl.lib

DLL

Certenc.dll

IID

IID_ICertEncodeStringArray is defined as 12a88820-7494-11d0-8816-0a0c903b83cc

See also

ICertEncodeStringArray
ICertEncodeStringArray::Decode
ICertEncodeStringArray::Reset
ICertEncodeStringArray::SetValue

 

 

Show: