This topic has not yet been rated - Rate this topic

ICertEncodeAltName::SetNameEntry method

Applies to: desktop apps only

The SetNameEntry method sets a name at a specified index of the alternate name array.

Before using this method, you must call ICertEncodeAltName::Reset so that the object knows how many elements are in the array.

Syntax


HRESULT SetNameEntry(
  [in]  LONG NameIndex,
  [in]  LONG NameChoice,
  [in]  const BSTR strName
);

Parameters

NameIndex [in]

Zero-based index that specifies the index of the alternate name entry to set.

If the NameChoice parameter is CERT_ALT_NAME_OTHER_NAME, OR (|) the index value with EAN_NAMEOBJECTID (defined as 0x80000000) to set the OID. Otherwise, the binary value is set.

NameChoice [in]

Specifies the name choice. The name choice indicates the type of the alternate name so that it can be used correctly. It must be one of the following values.

ValueMeaning
CERT_ALT_NAME_DIRECTORY_NAME

The name is a directory name.

CERT_ALT_NAME_DNS_NAME

The name is an IA5 string specifying a DNS (Domain Name System) name in the format host.entity.domain.

CERT_ALT_NAME_IP_ADDRESS

The name is an octet string that represents an Internet Protocol address.

CERT_ALT_NAME_REGISTERED_ID

The name is a registered object identifier (OID).

CERT_ALT_NAME_RFC822_NAME

The name is an email address.

CERT_ALT_NAME_URL

The name is an IA5 string that contains a URL in the format Service://HostName/Path.

CERT_ALT_NAME_OTHER_NAME

The name consists of an object identifier (OID) and a binary BLOB.

 

strName [in]

Specifies the alternate name.

Return value

VB

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.

Examples


    HRESULT hr = 0;
    ICertEncodeAltName *pAltName = NULL;
    BSTR bstrEMailAddress = NULL;
    LONG cnt = 0;
    LONG choice = 0;

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

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

    bstrEMailAddress = SysAllocString(L"<EMAILADDRESSHERE>");
    if (NULL == bstrEMailAddress)
    {
        printf("Memory allocation failed for bstrEMailAddress.\n");
        // handle error, free resources
    }

    // Set the name of the first element of the array.
    hr = pAltName->SetNameEntry(0, CERT_ALT_NAME_RFC822_NAME, 
        bstrEMailAddress);
    SysFreeString(bstrEMailAddress);
    bstrEMailAddress = NULL;
    if (FAILED(hr))
    {
        printf("ICertEncodeAltName::SetNameEntry failed: 
            0x%x\n", hr);
        // handle error, free resources
    }

    bstrEMailAddress = SysAllocString(L"<EMAILADDRESSHERE>");
    if (NULL == bstrEMailAddress)
    {
        printf("Memory allocation failed for bstrEMailAddress.\n");
        // handle error, free resources
    }

    // Set the name of the second element of the array.
    hr = pAltName->SetNameEntry(1, CERT_ALT_NAME_RFC822_NAME, 
        bstrEMailAddress);
    SysFreeString(bstrEMailAddress);
    bstrEMailAddress = NULL;
    if (FAILED(hr))
    {
        printf("ICertEncodeAltName::SetNameEntry failed: 
            0x%x\n", hr);
        // handle error, free resources
    }

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

    // Loop through the array and for each element in the array,  
    // print whether the name is an email address and the name itself.
    for (LONG i=0; i<cnt; i++)
    {
        hr = pAltName->GetNameChoice(i, &choice);
        if (FAILED(hr))
        {
            printf("ICertEncodeAltName::GetNameChoice failed: 
                0x%x\n", hr);
            // handle error, free resources
        }
        printf("Alt name(%d) %s an email address.\n", i,
               (CERT_ALT_NAME_RFC822_NAME == choice)
               ? "is" : "is not");

        hr = pAltName->GetName(i, &bstrEMailAddress);
        if (FAILED(hr))
        {
            printf("ICertEncodeAltName::GetName failed: 0x%x\n", hr);
            // handle error, free resources
        }
        printf("Alt name (%d) is %ws\n", i, bstrEMailAddress);
        SysFreeString(bstrEMailAddress);
        bstrEMailAddress = NULL;
    }

    // Encode the names from the CertEncodeAltName object into  
    // a BSTR. You can store the names for later use.
    BSTR bstrEncodedNames = NULL;
    hr = pAltName->Encode(&bstrEncodedNames);
    if (FAILED(hr))
    {
        printf("ICertEncodeAltName::Encode failed: 0x%x\n", hr);
        // handle error, free resources
    }
    pAltName->Release();
    pAltName = NULL;

    // Create a new instance of the CertEncodeAltName object
    // that will contain the decoded names from bstrEncodedNames.
    hr = CoCreateInstance(__uuidof(CCertEncodeAltName),
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          __uuidof(ICertEncodeAltName),
                          (void**)&pAltName);
    if (FAILED(hr))
    {
        printf("CoCreateInstance failed: 0x%x\n", hr);
        // handle error, free resources
    }

    // Decode the names from bstrEncodedNames into the new
    // CertEncodeAltName object. 
    hr = pAltName->Decode(bstrEncodedNames);
    SysFreeString(bstrEncodedNames);
    bstrEncodedNames = NULL;
    if (FAILED(hr))
    {
        printf("ICertEncodeAltName::Decode failed: 0x%x\n", hr);
        // handle error, free resources
    }

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

    // Verify the decode worked. Loop through the array and for 
    // each element in the array, print whether the name is an 
    // email address and the name itself.
	    for (LONG i=0; i<cnt; i++)
    {
        hr = pAltName->GetNameChoice(i, &choice);
        if (FAILED(hr))
        {
            printf("ICertEncodeAltName::GetNameChoice failed: 
                0x%x\n", hr);
            // handle error, free resources
        }
        printf("Alt name(%d) %s an email address.\n", i,
            (CERT_ALT_NAME_RFC822_NAME == choice) ? 
            "is" : "is not");

        hr = pAltName->GetName(i, &bstrEMailAddress);
        if (FAILED(hr))
        {
            printf("ICertEncodeAltName::GetName failed: 0x%x\n", hr);
            // handle error, free resources
        }
        printf("Alt name (%d) is %ws\n", i, bstrEMailAddress);
        SysFreeString(bstrEMailAddress);
        bstrEMailAddress = NULL;
    }

    pAltName->Release();

Requirements

Minimum supported client

None supported

Minimum supported server

Windows 2000 Server

Header

Certenc.h (include Certsrv.h)

Library

Certidl.lib

DLL

Certenc.dll

IID

IID_ICertEncodeAltName is defined as 1c9a8c70-1271-11d1-9bd4-00c04fb683fa

See also

ICertEncodeAltName

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ