Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Binding to the Parent of an Object

The following C++ code fragment contains a function that binds to the parent of an object:

HRESULT GetParentObject(IADs *pObject, //Pointer the object whose parent to bind to.
                        IADs **ppParent //Return a pointer to the parent object.
                        )
{
    if ((!pObject)||(!ppParent))
        return E_INVALIDARG;
 
    HRESULT hr = E_FAIL;
    BSTR bstr;
    hr = pObject->get_Parent(&bstr);
    if (SUCCEEDED(hr))
    {
        //Bind to the parent container.
        *ppParent = NULL;
        hr = ADsOpenObject(bstr,
             NULL,
             NULL,
             ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
             IID_IADs,
             (void**)ppParent);
        if(FAILED(hr))
        {
            if (!(*ppParent))
            {
              (*ppParent)->Release();
              (*ppParent) = NULL;
            }
        }
    }
    FreeADsStr(bstr);
    return hr;
}