Platform SDK: Active Directory, ADSI, and Directory Services

Retrieving the objectClass Property

The objectClass property contains the class of which the object is an instance, as well as all classes from which that class is derived. For example, the user class inherits from top, person, and organizationalPerson; therefore, the objectClass property contains the names of those classes, as well as user. So, how do you find out what class the object is an instance of? The objectClass property is the only property with multiple values that has ordered values — the first value is the top of the class inheritance tree, which is the top class, and the last value is the most derived class, which is the class that the object is an instance of.

The following function takes a pointer to a column containing an objectClass property and returns the instantiated objectClass of the object:

HRESULT GetClass(ADS_SEARCH_COLUMN *pcol, LPOLESTR *ppClass)
{
  if (!pcol)
    return E_POINTER;
 
  HRESULT hr = E_FAIL;
  if (ppClass)
  {
    LPOLESTR szClass = new OLECHAR[MAX_PATH];
    wcscpy(szClass, L"");
    if ( _wcsicmp(pcol->pszAttrName,L"objectClass") == 0 )
    {
      for (DWORD x = 0; x< pcol->dwNumValues; x++)
      {
        wcscpy(szClass, pcol->pADsValues[x].CaseIgnoreString);
      }
    }
    if (0==wcscmp(L"", szClass))
    {
      hr = E_FAIL;
    }
    else
    {
      //Allocate memory for string.
      //Caller must free using CoTaskMemFree.
      *ppClass = (OLECHAR *)CoTaskMemAlloc (
                             sizeof(OLECHAR)*(wcslen(szClass)+1));
      if (*ppClass)
      {
        wcscpy(*ppClass, szClass);
        hr = S_OK;
      }
      else
      hr=E_FAIL;
    }
  }
  return hr;
}