Platform SDK: Active Directory, ADSI, and Directory Services

IADsPathname Interface

The IADsPathname interface is used to parse and modify various elements of an ADsPath. It also converts ADsPaths between various display formats.

This example extracts and returns just the server name from a valid ADsPath for display to the user in a maintenance utility.

HRESULT GetServerName(LPWSTR adsPath, LPWSTR *adsServer)
{
    HRESULT hr = S_OK;
    IADsPathname *pIADsPathname = NULL;
 
    // Create the IADsPathname object.
    hr = CoCreateInstance(CLSID_Pathname,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IADsPathname,
                          (void**) &pIADsPathname);
    if (FAILED(hr))
    {
        if (pIADsPathname)
            pIADsPathname->Release();
        return (hr);
    }
 
    // Set the pathname.
    hr = pIADsPathname->Set(adsPath, ADS_SETTYPE_FULL);
    if (SUCCEEDED(hr))
        // Extract and return the server name.
        hr = pIADsPathname->Retrieve(ADS_FORMAT_SERVER, adsServer);
 
    // Clean up.
    pIADsPathname->Release();
    return (hr);
}

This example helps to initialize a newly created ADSI object by setting the object's Distinguished Name property from its own ADsPath. Note that the calling routine is responsible for committing any changes to the underlying directory store by invoking the SetInfo method.

HRESULT SetDistinguishedName(IADs *pIADs)
{
    HRESULT hr = S_OK;
    LPWSTR bsADsPath, bsADsWDNPath;
    IADsPathname *pIADsPathname = NULL;
 
    // Get the ADsPath for this object.
    hr = pIADs->get_ADsPath(&bsADsPath);
    if (FAILED(hr))
        return (hr);
 
    // Create the IADsPathname object.
    hr = CoCreateInstance(CLSID_Pathname,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IADsPathname,
                          (void**) &pIADsPathname);
    if (FAILED(hr))
    {
        if (pIADsPathname)
            pIADsPathname->Release();
        return (hr);
    }
     // Set the pathname.
    hr = pIADsPathname->Set(bsADsPath, ADS_SETTYPE_FULL);
    if (SUCCEEDED(hr))
    {
        // Convert the path to Distinguished Name format.
        hr = pIADsPathname->Retrieve(ADS_FORMAT_WINDWOS_DN,
                                     &bsWDNPath);
 
        if (SUCCEEDED(hr))
        {
            // Set the distinguished name property.
            VARIANT var;
            VariantInit(&var);
            V_BSTR(&var) = bsWDNPath;
            V_VT(&var) = VT_BSTR;
            hr = pIADs->Put(L"distinguishedName", var);
            VariantClear(&var);
        }
    }
 
    // Clean up.
    pIADsPathname->Release();
    return (hr);
 
}