Directory Services

Searching Binary Data

Even though the ADSI search feature only supports searching for string data, it is possible to search for binary data. To do this, use the ADsEncodeBinaryData function to convert the binary data into a string that can be used with the search methods. Searching for binary data is particularly useful when searching for a GUID or a SID because these data types are stored as binary data.

When using the ADsEncodeBinaryData function, the memory allocated must be freed using the FreeADsMem function.

The following code example shows how to build a query string to search for an object that has a particlar objectGUID value.

LPWSTR pwszGuid = NULL;
LPWSTR pwszFormat = L"(objectGUID=%s)";
LPWSTR pwszSearch = NULL;
hr = ADsEncodeBinaryData((LPBYTE)pguid, sizeof(GUID), &pwszGuid);
if(FAILED(hr))
{
	goto cleanup;
}

pwszSearch = new WCHAR[lstrlenW(pwszFormat) + lstrlenW(pwszGuid) + 1];
if(NULL == pwszSearch)
{
	goto cleanup;
}

wsprintfW(pwszSearch, pwszFormat, pwszGuid);

// Use pwszSearch to perform a query for the object.

cleanup:
if(pwszGuid)
{
	FreeADsMem(pwszGuid);
}
if(pwszSearch)
{
	delete pwszSearch;
}