大网数据广告活动
域与目录服务

Accessing Attributes With the IDirectoryObject Interface

· 52CE 我爱测

The IDirectoryObject interface provides a client application written in C and C++ with direct access to directory service objects.

The IDirectoryObject interface provides a client application written in C and C++ with direct access to directory service objects. The interface enables access by means of a direct network protocol, rather than through the ADSI attribute cache. In place of the properties supported by the IADs interface, IDirectoryObject provides methods that support a critical subset of an object's maintenance methods and provide access to its attributes. With IDirectoryObject, a client can get or set any number of object attributes with one method call. Unlike the corresponding Automation methods, which are batched, those of IDirectoryObject are executed when called. Because methods on this interface do not require creating an instance of an Automation directory object, the performance overhead is small.

Clients written in languages such as C and C++ should use the methods of the IDirectoryObject interface to optimize performance and take advantage of native directory service interfaces. Automation clients cannot use IDirectoryObject. Instead, they should use the IADs interface.

The IDirectoryObject::GetObjectAttributes method retrieves attributes with both single and multiple values. This method takes a list of requested attributes and returns an ADS_ATTR_INFO structure. ADSI allocates this structure; the caller must free this memory when it is no longer required using the FreeADsMem function.

The order of returned attribute values is not necessarily the same as the order in which the attributes were requested. Therefore, it is necessary to compare the attribute names returned from ADSI.

Note:
The ADS_ATTR_INFO structure does not return all of the attributes requested. Only those attributes that contain values are part of the returned structure.

The number of attributes returned is determined by the dwNumberAttributes parameter passed to the IDirectoryObject::GetObjectAttributes method.

The following code example binds to an object and uses the IDirectoryObject::GetObjectAttributes method to retrieve attributes of the object.

HRESULT hr;
IDirectoryObject *pDirObject;

CoInitialize(NULL);

hr = ADsGetObject(
       L"LDAP://CN=Jeff Smith,OU=Users,DC=Fabrikam,DC=com",
       IID_IDirectoryObject, 
       (void**)&pDirObject);

if(SUCCEEDED(hr))
{
  ADS_ATTR_INFO *pAttrInfo = NULL;
  LPWSTR pAttrNames[] = {L"cn", L"title", L"otherTelephone"};
  DWORD dwNumAttr = sizeof(pAttrNames)/sizeof(LPWSTR);
  DWORD dwReturn;

  //////////////////////////////////////////////////
  // Get attribute values requested.
  // Be aware that the order is not necessarily the 
  // same as requested using pAttrNames.
  //////////////////////////////////////////////////
  hr = pDirObject->GetObjectAttributes(pAttrNames, 
                                       dwNumAttr, 
                                       &pAttrInfo, 
                                       &dwReturn);
     
  if(SUCCEEDED(hr))
  {
    for(DWORD idx = 0; idx < dwReturn; idx++)
      {
        if(_wcsicmp(pAttrInfo[idx].pszAttrName, L"cn") == 0)
        {
          if(pAttrInfo[idx].dwADsType == ADSTYPE_CASE_IGNORE_STRING)
          {
            wprintf(L"Common Name: %s\n", 
                    pAttrInfo[idx].pADsValues[0].CaseIgnoreString);
          }
        }
        else if(_wcsicmp(pAttrInfo[idx].pszAttrName, L"title") == 0)
        {
          if(pAttrInfo->dwADsType == ADSTYPE_CASE_IGNORE_STRING)
          {
            wprintf(L"Title: %s\n", 
                    pAttrInfo[idx].pADsValues[0].CaseIgnoreString);
          }
        }
        else if(_wcsicmp(pAttrInfo[idx].pszAttrName, 
                L"otherTelephone") == 0)
        {  
          // Print the multi-valued property, "Other Telephones".
          if(pAttrInfo[idx].dwADsType == ADSTYPE_CASE_IGNORE_STRING)
        {
          wprintf(L"Other Telephones:");
          for(DWORD val = 0; val < pAttrInfo[idx].dwNumValues; val++) 
          {
            wprintf(L"  %s\n", 
                    pAttrInfo[idx].pADsValues[val].CaseIgnoreString);
          }
        }
      }
    }

    FreeADsMem(pAttrInfo);
  }
     
  pDirObject->Release();
}

CoUninitialize();

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。