使用 IDirectoryObject 接口修改属性

除了 IADs::PutIADs::PutEx 之外,还可以使用 IDirectoryObject::SetObjectAttributes 方法来修改属性值。 要使用此方法,必须为每个要修改的属性填写 ADS_ATTR_INFO 结构。

使用 IDirectoryObject::SetObjectAttributes 方法可以修改单值和多值属性。 此函数提供与 IADs::PutEx 方法类似的操作控制,如清除、追加、删除和更新。 控件常量包括:

指定 ADS_ATTR_UPDATE 将触发可能会占用大量资源的服务器端操作。 例如,启动操作以更新组成员身份的长列表。 一般情况下,除非修改涉及目录中的少量属性,否则请避免使用此操作。 若要修改组成员身份的长列表,更有效的方法是从基础目录读取列表,进行修改,然后将更新后的列表存储回目录。

注意

与具有 IADs::SetInfoIADs::PutIADs::PutEx 一样,在 Active Directory 中,属性更改要么被完全提交,要么被丢弃。 如果不允许进行一项或多项修改并因此而无法执行,则对属性的所有修改都不会提交到目录中。

 

示例

以下代码示例展示了如何使用 IDirectoryObject::SetObjectAttributes 方法修改单值和多值属性。

HRESULT hr;
LPCWSTR pwszADsPath = L"LDAP://CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=com";
IDirectoryObject *pDirObject = NULL;

// Bind to the object.
hr = ADsGetObject(pwszADsPath, IID_IDirectoryObject, (void**)&pDirObject);
if(SUCCEEDED(hr))
{ 
    ADSVALUE adsvFaxNumber;
    ADSVALUE rgadsvOtherTelephones[2];
     
    // Set the new FAX number.
    adsvFaxNumber.dwType = ADSTYPE_CASE_IGNORE_STRING; 
    adsvFaxNumber.CaseIgnoreString = L"425-707-9790";

    // Set the first telephone number.
    rgadsvOtherTelephones[0].dwType = ADSTYPE_CASE_IGNORE_STRING;
    rgadsvOtherTelephones[0].CaseIgnoreString = L"425-707-9791";

    // Set the second telephone number.
    rgadsvOtherTelephones[1].dwType = ADSTYPE_CASE_IGNORE_STRING;
    rgadsvOtherTelephones[1].CaseIgnoreString = L"425-707-9792";

    ADS_ATTR_INFO attrInfo[2];

    // Setup the facsimileTelephoneNumber attribute data.
    attrInfo[0].pszAttrName = L"facsimileTelephoneNumber";
    attrInfo[0].dwControlCode = ADS_ATTR_UPDATE;
    attrInfo[0].dwADsType = adsvFaxNumber.dwType;
    attrInfo[0].pADsValues = &adsvFaxNumber;
    attrInfo[0].dwNumValues = 1;

    // Setup the otherTelephone attribute data.
    attrInfo[1].pszAttrName = L"otherTelephone";
    attrInfo[1].dwControlCode = ADS_ATTR_UPDATE;
    attrInfo[1].dwADsType = rgadsvOtherTelephones[0].dwType;
    attrInfo[1].pADsValues = rgadsvOtherTelephones;
    attrInfo[1].dwNumValues = sizeof(rgadsvOtherTelephones)/sizeof(ADSVALUE);

    DWORD dwReturn;
 
    // Set the new attribute values.
    hr = pDirObject->SetObjectAttributes(attrInfo, 
        sizeof(attrInfo)/sizeof(ADS_ATTR_INFO), 
        &dwReturn);

    pDirObject->Release();
}