Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ICertEncodeDateArray::Encode method

The Encode method returns an Abstract Syntax Notation One (ASN.1)-encoded string of the date array stored in this object.

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

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

Syntax


HRESULT Encode(
  [out] BSTR *pbstrBinary
);

Parameters

pbstrBinary [out]

A pointer to a BSTR that will contain the encoded Date 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 that represents the DATE array.

Examples


    HRESULT hr = 0;
    ICertEncodeDateArray *pDateArray = NULL;
    DATE date = 0;
    SYSTEMTIME st;

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

    // Specify an array size of 2 for the CertEncodeDateArray object.
    hr = pDateArray->Reset(2);
    if (FAILED(hr))
    {
        printf("ICertEncodeDateArray::Reset failed: 0x%x\n", hr);
        // handle error, free resources
    }

    memset(&st, 0, sizeof(SYSTEMTIME));
    st.wYear = 2005;
    st.wMonth = 1;
    st.wDay = 1;
    st.wHour = 12;
    if (!SystemTimeToVariantTime(&st, &date))
    {
        printf("SystemTimeToVariantTime failed.\n");
        // handle error, free resources
    }

    // Set the date of the first element of the array.
    hr = pDateArray->SetValue(0, date);
    if (FAILED(hr))
    {
        printf("ICertEncodeDateArray::SetValue failed: 0x%x\n", hr);
        // handle error, free resources
    }

    memset(&st, 0, sizeof(SYSTEMTIME));
    st.wYear = 2002;
    st.wMonth = 7;
    st.wDay = 29;
    st.wHour = 17;
    if (!SystemTimeToVariantTime(&st, &date))
    {
        printf("SystemTimeToVariantTime failed.\n");
        // handle error, free resources
    }

    // Set the date of the second element of the array.
    hr = pDateArray->SetValue(1, date);
    if (FAILED(hr))
    {
        printf("ICertEncodeDateArray::SetValue failed: 0x%x\n", hr);
        // handle error, free resources
    }

    LONG cnt = 0;
    TCHAR szDate[30];

    // Retrieve the number of elements in the array.
    hr = pDateArray->GetCount(&cnt);
    if (FAILED(hr))
    {
        printf("ICertEncodeDateArray::GetCount failed: 0x%x\n", hr);
        // handle error, free resources
    }

    // Loop through and print the date in each element of the array.
    for (LONG i=0; i<cnt; i++)
    {
        hr = pDateArray->GetValue(i, &date);
        if (FAILED(hr))
        {
            printf("ICertEncodeDateArray::GetValue "
                "failed: 0x%x\n", hr);
            // handle error, free resources
        }
        if (!VariantTimeToSystemTime(date, &st))
        {
            // handle error, free resources
        }
        else
        {
            if (!GetDateFormat(NULL, 0, &st, "ddd',' MMM dd yy",  
                szDate, sizeof(szDate)))
            {
                // handle error, free resources
            }
            printf("date[%d] is %s.\n", i, szDate);
        }
    }

    // Encode the date array into a BSTR. 
    // You can store the names for use later.
    BSTR bstrEncoded = NULL;
    hr = pDateArray->Encode(&bstrEncoded);
    pDateArray->Release();
    pDateArray = NULL;
    if (FAILED(hr))
    {
        printf("ICertEncodeDateArray::Encode failed: 0x%x\n", hr);
        // handle error, free resources
    }

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

    // Decode the encoded date array into the new 
    // CertEncodeDateArray object.
    hr = pDateArray->Decode(bstrEncoded);
    SysFreeString(bstrEncoded);
    bstrEncoded = NULL;
    if (FAILED(hr))
    {
        printf("ICertEncodeDateArray::Decode failed: 0x%x\n", hr);
        // handle error, free resources
    }

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

    // Loop through and print the date in each element of the array.
    for (LONG i=0; i<cnt; i++)
    {
        hr = pDateArray->GetValue(i, &date);
        if (FAILED(hr))
        {
            printf("ICertEncodeDateArray::GetValue "
                "failed: 0x%x\n", hr);
            // handle error, free resources
        }
        if (!VariantTimeToSystemTime(date, &st))
        {
            // handle error, free resources
        }
        else
        {
            if (!GetDateFormat(NULL, 0, &st, "ddd',' MMM dd yy", 
                szDate, sizeof(szDate)))
            {
                // handle error, free resources
            }
            printf("date[%d] is %s.\n", i, szDate);
        }
    }

    pDateArray->Release();

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_ICertEncodeDateArray is defined as 2f9469a0-a470-11d0-8821-00a0c903b83c

See also

ICertEncodeDateArray
ICertEncodeDateArray::Decode
ICertEncodeDateArray::Reset
ICertEncodeDateArray::SetValue

 

 

Community Additions

ADD
Show:
© 2015 Microsoft