Directory Services

Example Code for Reading a Constructed Attribute

The following code example shows a method that can be used to retrieve an attribute value that will work with all attributes types.

[Visual Basic]
Public Function GetAttribute(oObject As IADs, AttributeName As String) As String
	Const E_ADS_PROPERTY_NOT_FOUND = &H8000500D

	On Error Resume Next

	' Attempt to get the attribute value.
	GetAttribute = oObject.Get(AttributeName)
	If (Err.Number = E_ADS_PROPERTY_NOT_FOUND) Then
		' Reset the error number.
		Err.Number = 0
	
		' Use IADs.GetInfoEx to explicitly load the attribute value into the cache.
		oObject.GetInfoEx Array(AttributeName), 0
		If Err.Number = 0 Then
			' Attempt to get the attribute value.
			GetAttribute = oObject.Get(AttributeName)
		End If
	End If
End Function

The following code example shows a method that can be used to retrieve an attribute value that will work with all attributes types.

[C++]
/***************************************************************************

	GetAttribute()

***************************************************************************/

HRESULT GetAttribute(IADs *pads, BSTR bstrAttribute, VARIANT *pvar)
{
	if(!pads || !pvar)
	{
		return E_INVALIDARG;
}

	HRESULT hr;
	CComVariant svar;

	// Attempt to get the attribute with IADs.Get.
	hr = pads->Get(bstrAttribute, &svar);
	if(E_ADS_PROPERTY_NOT_FOUND  == hr)
	{
		// Attempt to get the attribute with IADs.GetInfoEx and then IADs.Get.

		CComVariant svarArray;
		hr = ADsBuildVarArrayStr(&(LPWSTR)bstrAttribute, 1, &svarArray);
		if (SUCCEEDED(hr))
		{
			hr = pads->GetInfoEx(svarArray, 0L);
			if(SUCCEEDED(hr))
			{
				hr = pads->Get(bstrAttribute, &svar);
		}
	}

}

	if(S_OK == hr)
	{
		hr = svar.Detach(pvar);
}

	return hr;
}