使用 GetInfoEx 进行优化
IADs.GetInfoEx 方法用于将特定属性值从基础目录服务加载到本地缓存中。 此方法仅将指定的属性值加载到本地缓存中。 IADs.GetInfo 方法用于将所有属性值载入本地缓存中。
IADs.GetInfoEx 方法从基础目录存储中获取 Active Directory 对象属性的特定当前值,并刷新缓存值。
但是,如果属性缓存中已经存在某个值,那么在调用 IADs.Get 或 IADs.GetEx 方法时,如果不首先调用该属性的 IADs.GetInfoEx 方法,则将检索缓存中的值,而不是基础目录中的最新值。 如果本地缓存已被修改,但这些值尚未通过调用 IADs.SetInfo 方法提交到基础目录服务,则可能导致更新的属性值被覆盖。 避免缓存问题的建议方法是,在调用 IADs.GetInfo 之前始终通过调用 IADs.SetInfo 来提交属性值更改。
Dim usr As IADs
Dim PropArray As Variant
On Error GoTo Cleanup
' Bind to a specific user object.
Set usr = GetObject("LDAP://CN=Jeff Smith,CN=Users,DC=fabrikam,DC=com")
' The code example assumes that the property description has a single value in the directory.
' Be aware that this will implicitly call GetInfo because, at this point, GetInfo
' has not yet been called (implicitly or explicitly) on the usr object.
Debug.Print "User's common name is " + usr.Get("cn")
Debug.Print "User's title is " + usr.Get("title")
Debug.Print "User's department is " + usr.Get("department")
' Change two of the attribute values in the local cache.
usr.Put "cn", "Jeff Smith"
usr.Put "title", "Vice President"
usr.Put "department", "Head Office"
Debug.Print "User's common name is " + usr.Get("cn")
Debug.Print "User's title is " + usr.Get("title")
Debug.Print "User's department is " + usr.Get("department")
' Initialize the array of properties to pass to GetInfoEx.
PropArray = Array("department", "title")
' Get the specified attribute values.
usr.GetInfoEx PropArray, 0
' The specific attributes values were overwritten, but the attribute
' value not retrieved has not changed.
Debug.Print "User's common name is " + usr.Get("cn")
Debug.Print "User's title is " + usr.Get("title")
Debug.Print "User's department is " + usr.Get("department")
Cleanup:
If (Err.Number<>0) Then
MsgBox("An error has occurred. " & Err.Number)
End If
Set usr = Nothing
检索 Active Directory 构造的属性
在 Active Directory 中,当调用 IADs.GetInfo 方法时,会检索和缓存大部分构建的属性(如果缓存为空,则 IADs.Get 会执行隐式 IADs.GetInfo 调用)。 但是,某些构造属性不会自动检索和缓存,因此需要显式调用 IADs.GetInfoEx 方法来检索它们。 例如,在 Active Directory 中,调用 IADs.GetInfo 方法时不会检索 canonicalName 属性,IADs.Get 将返回 E_ADS_PROPERTY_NOT_FOUND。 必须调用 IADs.GetInfoEx 方法来检索 canonicalName 属性。 使用 IADsPropertyList 接口枚举属性也无法检索到这些相同的构造属性。
有关详细信息以及如何检索所有属性值的代码示例,请参阅读取构造属性的代码示例。