ICertEncodeBitString::Encode method

The Encode method performs Abstract Syntax Notation One (ASN.1) encoding on a given bit string.

Syntax


HRESULT Encode(
  [in]  LONG BitCount,
  [in]  BSTR strBitString,
  [out] BSTR *pbstrBinary
);

Parameters

BitCount [in]

The number of bits in strBitString.

strBitString [in]

The bit string to encode.

pbstrBinary [out]

A pointer to a BSTR that will contain the encoded bit string. When you have finished using this BSTR, call the SysFreeString function to free pbstrBinary.

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 bit string.

Examples


    HRESULT hr = 0;
    ICertEncodeBitString *pBitString = NULL;
    BSTR bstrKU = NULL;     // bstr for Key Usage, to be encoded
    BSTR bstrEncode = NULL; // will accept the encoded bits
    BYTE KeyUsage[2];
    long nLen;

    // Instantiate the CertEncodeBitString object.
    hr = CoCreateInstance(__uuidof(CCertEncodeBitString),
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          __uuidof(ICertEncodeBitString),
                          (void **)&pBitString);
    if (FAILED(hr))
    {
        printf("Failed CoCreateInstance - pBitString [%x]\n", hr);
        // handle error, free resources
    }

    // Build the string to encode.
    // The constants are defined in Wincrypt.h.
    KeyUsage[0] = CERT_DIGITAL_SIGNATURE_KEY_USAGE |
                  CERT_NON_REPUDIATION_KEY_USAGE |
                  CERT_DATA_ENCIPHERMENT_KEY_USAGE;
    KeyUsage[1] = 0;
    bstrKU = SysAllocStringByteLen((const char *)KeyUsage,
                                   sizeof(KeyUsage));
    if (NULL == bstrKU)
    {
        printf("Memory allocation failed for bstrKU.\n");
        // handle error, free resources
    }

    // Calculate the number of bits in the string.
    nLen = SysStringByteLen(bstrKU) * 8;

    // Encode the string.
    hr = pBitString->Encode(nLen, bstrKU, &bstrEncode);
    SysFreeString(bstrKU);
    if (FAILED(hr))
    {
        printf("Failed Encode [%x]\n", hr);
        // handle error, free resources
    }

    // Do something with the encoded string. If you want to add it 
    // to the CertEncodeBitString object, call the Decode method.
    hr = pBitString->Decode(bstrEncode);
    SysFreeString(bstrEncode);
    if (FAILED(hr))
    {
        printf("Failed Decode [%x]\n", hr);
        // handle error, free resources
    }

    nLen = 0;
    hr = pBitString->GetBitCount(&nLen);
    if (FAILED(hr))
    {
        printf("Failed GetBitCount [%x]\n", hr);
        // handle error, free resources
    }

    bstrKU = NULL;
    hr = pBitString->GetBitString(&bstrKU);
    if (FAILED(hr))
    {
        printf("Failed GetBitString [%x]\n", hr);
        // handle error, free resources
    }

    // Loop through and print the bits.
    BYTE by, bit;
    for (LONG i=0; i<nLen; i++)
    {
        by = ((BYTE*)bstrKU)[i/8];
        bit = (by>>(7(i%8))) & 1;
        printf("Bit #%u: %u\n", i, bit);
    }

    SysFreeString(bstrKU);
    pBitString->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_ICertEncodeBitString is defined as 6db525be-1278-11d1-9bd4-00c04fb683fa

See also

ICertEncodeBitString
ICertEncodeBitString::Decode

 

 

Show: