Directory Services

Accessing Attributes with ADSI

The IADs.Get and IADs.GetEx methods are used to retrieve named attribute values. Both methods return a VARIANT value. These methods are available only for directories that support a schema. When accessing objects in a directory without a schema, the IADsPropertyEntry and IADsPropertyValue interfaces must be used to manipulate attribute values.

The IADs.Get and IADs.GetEx methods return a VARIANT which may, or may not, be a VARIANT array depending on the number of values returned by the server. For example, if only one value is returned from the server, regardless of whether it is a single or multi-valued attribute, the method returns a single VARIANT. Conversely, if multiple values are returned, a VARIANT array is returned. If a VARIANT array is returned, the vt member of the VARIANT structure contains the VT_VARIANT/vbVariant and VT_ARRAY/vbArray flags.

The IADs.Get and IADs.GetEx methods can also return a COM object using the IDispatch interface. In this case, the vt member of the VARIANT structure contains the VT_DISPATCH/vbObject flag. To access the COM object, call the QueryInterface method on the IDispatch interface to obtain the desired interface.

Another data type returned by the IADs.Get and IADs.GetEx methods is binary data. In this case, the data is supplied as a contiguous array of bytes and the vt member of the VARIANT structure will contain the VT_UI1/vbByte and VT_ARRAY/vbArray flags.

Note  Microsoft Visual Basic, Scripting Edition only supports VARIANT and VARIANT arrays. Because of this, VBScript cannot be used to read binary property values.

Many ADSI interfaces define interface-specific properties. For example, the IADsComputer interface defines the Location property. These interface-defined properties may contain data that is identical to one of the named attributes, but the properties are specific to the type of object the interface refers to. In languages that support automation, these interface-defined properties can be accessed using the dot notation as shown in the following code example.

Dim oUser as IADs
Dim Path as String
 
' Bind to a specific user object.
set oUser = GetObject("LDAP://CN=Jeff Smith,CN=Users,DC=fabrikam,DC=com")
 
' Get property.
Path = MyUser.ADsPath

In non-automation languages, the property access methods must be used to access the interface-defined properties. For example, the IADsComputer::get_Location method is used to retrieve the IADsComputer.Location property. The following C++ code example demonstrates how to use the property access method in C++ to retrieve the ADsPath of a user.

HRESULT hr;
IADs *pUser; 
 
// Bind to user object.
hr = ADsGetObject(L"LDAP://CN=Jeff Smith,CN=Users,DC=fabrikam,DC=com", 
				IID_IADs, 
				(void**)&pUser);
if(SUCCEEDED(hr)) 
{
	BSTR bstrName;

	// Get property.
	hr = pUser->get_Name(&bstrName);
	if(SUCCEEDED(hr)) 
	{
		wprintf(bstrName);
 
		SysFreeString(bstrName);
}

	pUser->Release();
}

For more information about accessing attributes with ADSI, see: