MORE INFORMATION
Attributes of
DNWithBinary syntax have an object identifier (OID) of 2.5.5.7 in the
attributeSyntax property. The data in these attributes is stored in the directory as an OctetString that contains a binary value and a distinguished name (DN). These attributes have the following format:
B:CharCount:binaryvalue:ObjectDN
where
CharCount is the count of hexadecimal digits in the binary value,
binaryvalue is the hexadecimal digit representation of the binary value stored, and
ObjectDN is the DN of an object referenced by this attribute.
The Active Directory maintains the
ObjectDN so that it contains the current DN of the object originally specified when the value was created, even if the object is moved. The
otherWellKnownObjects attribute is an example of an attribute with
DNWithBinary syntax.
The Platform Software Development Kit (SDK) contains both a Microsoft Visual C++ and a Microsoft Visual Basic sample of setting this attribute on an object. These samples use the format given above to set the value for
DNWithBinary. Both samples create a MyWKOTestContainer at the root of the domain and assign an
otherWellKnownObject to a child container.
The following example code assumes that one of these samples has been run, and that the containers still exist in the domain. You can easily modify the samples to bind to a different object, or to retrieve another attribute of
DNWithBinary type.
Here is an example of using
IADsDNWithBinary from Visual C++:
IADs *pIADs, *pRootDSE = NULL;
IADsDNWithBinary *pDNWithBinary = NULL;
HRESULT hr;
VARIANT var;
LPOLESTR szDSPath = new OLECHAR[MAX_PATH];
hr = ADsGetObject(L"LDAP://rootDSE",IID_IADs,(void**)&pRootDSE);
if (!SUCCEEDED(hr))
{
wprintf(L"Fail to get RootDSE!");
return hr;
}
hr = pRootDSE->Get(L"defaultNamingContext",&var);
wcscpy(szDSPath,L"LDAP://CN=MyWKOTestContainer,");
wcscat(szDSPath,var.bstrVal);
pRootDSE->Release();
// Get the TestContainer created by the SDK sample which has the otherWellKnownObjects attribute populated.
hr = ADsGetObject(szDSPath, IID_IADs,(void**) &pIADs);
if ( !SUCCEEDED(hr) )
{
wprintf(L"Failed to GetObject\n");
return hr;
}
hr = pIADs->GetEx(L"otherWellKnownObjects", &var );
if ( SUCCEEDED(hr) )
{
LONG lstart, lend;
SAFEARRAY *sa = V_ARRAY( &var );
VARIANT varItem,varBin;
BSTR bstrDN = NULL;
// Get the lower and upper bound of the Variant array of IDisp's.
hr = SafeArrayGetLBound( sa, 1, &lstart );
hr = SafeArrayGetUBound( sa, 1, &lend );
// Now iterate and print the DNs and Binary Values.
VariantInit(&varItem);
printf("Getting each WellKnownGuid\n");
for ( long idx=lstart; idx <= lend; idx++ )
{
hr = SafeArrayGetElement( sa, &idx, &varItem );
hr = V_DISPATCH(&varItem)->QueryInterface(IID_IADsDNWithBinary,(void**)&pDNWithBinary);
if (SUCCEEDED(hr))
{
hr = pDNWithBinary->get_DNString(&bstrDN);
if(!SUCCEEDED(hr))
{
wprintf(L"Failed to get DN");
return hr;
}
else
{
wprintf(L"DN : %s\n",bstrDN);
SysFreeString(bstrDN);
}
hr = pDNWithBinary->get_BinaryValue(&varBin);
if(!SUCCEEDED(hr))
{
wprintf(L"Failed to get Binary");
return hr;
}
else
{
SAFEARRAY *saBinVal = V_ARRAY( &varBin );
LONG lStartBinVal, lEndBinVal;
BYTE btVal;
// Get the lower and upper bound of the Byte array.
hr = SafeArrayGetLBound( saBinVal, 1, &lStartBinVal );
hr = SafeArrayGetUBound( saBinVal, 1, &lEndBinVal );
// Now iterate and print the binary values as hexadecimal.
printf("Getting each Binary Value\n");
for ( long idx2=lStartBinVal; idx2 <= lEndBinVal; idx2++ )
{
hr = SafeArrayGetElement( saBinVal, &idx2, &btVal );
wprintf(L"0x%x\n",btVal );
}
VariantClear(&varBin);
}
}
pDNWithBinary->Release();
VariantClear(&varItem);
}
printf("\n");
VariantClear(&var);
}
// Clean up.
if ( pIADs )
{
pIADs->Release();
}
Here is an example of using
IADsDNWithBinary from Visual Basic:
Dim oCont As IADs
Dim oRootDSE As IADs
Dim oDNWithBinary As IADsDNWithBinary
Dim strADsPath As String
Dim vArray() As Variant
Dim var As Variant
Set oRootDSE = GetObject("LDAP://RootDSE")
strADsPath = "LDAP://cn=MyWKOTestContainer," & oRootDSE.Get("defaultNamingContext")
' Get the TestContainer created by the SDK sample which has otherWellKnownObjects attribute populated.
Set oCont = GetObject(strADsPath)
vArray = oCont.GetEx("otherWellKnownObjects")
' Now iterate through and pring the DNs and Binary Values.
For Each var In vArray
Set oDNWithBinary = var
Debug.Print oDNWithBinary.DNString
'Now iterate through and print the binary values as hexadecimal.
For Each btElement In oDNWithBinary.BinaryValue
Debug.Print "0x" & Hex(btElement)
Next
Next
Set oDNWithBinary = Nothing
Set oCont = Nothing
Set oRootDSE = Nothing
REFERENCES
For additional information on Active Directory Services Interfaces (ADSI), see the following Microsoft Web site:
For additional information on using Active Directory attributes with
DNWithBinary syntax and the
otherWellKnownObjects attribute, see "Enabling Rename-Safe Binding with the otherWellKnownObjects Property" at the following Microsoft Developer Network (MSDN) Web site:
For the latest Platform SDK samples, see the following MSDN Web site: