Directory Services

ADsEncodeBinaryData

The ADsEncodeBinaryData function converts a binary large object (BLOB) to the Unicode format suitable to be embedded in a search filter.

HRESULT ADsEncodeBinaryData(
  PBYTE pbSrcData,
  DWORD dwSrcLen,
  LPWSTR* ppszDestData
);

Parameters

pbSrcData
[in] BLOB to be converted.
dwSrcLen
[in] Size, in bytes, of the BLOB.
ppszDestData
[out] Pointer to a null-terminated Unicode string that receives the converted data.

Return Values

This method supports the standard return values, as well as the following.

Return Code Description
E_ADS_BAD_PARAMETER Invalid parameters. For example, the length of the source data is zero.
E_OUTOFMEMORY Memory allocation failed.
S_OK Operation succeeded.

Remarks

In ADSI, search filters must be Unicode strings. Sometimes, a filter contains data that is normally represented by an opaque BLOB of data. For example, you may want to include an object security identifier in a search filter, which is of binary data. In this case, you must first call the ADsEncodeBinaryData function to convert the binary data to the Unicode string format. When the data is no longer required, call the FreeADsMem function to free the converted Unicode string; that is, ppszDestData. The following code example shows how to use this function.

// Test binary values in filters and use
// a binary filter instead of a string filter in ExecuteSearch.

LPWSTR pszPrefix = L"objectSid=%s";
LPWSTR pszBinaryFilter = NULL;
LPWSTR pszDest = NULL;
HRESULT hr = S_OK;
 
BYTE column[] = {
  0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x00,
  0x00, 0x00, 0x59, 0x51, 0xb8, 0x17, 0x66, 0x72, 0x5d, 0x25,
  0x64, 0x63, 0x3b, 0x0b, 0x29, 0x99, 0x21, 0x00 };

DWORD dwSize = sizeof(column)/sizeof(BYTE);
 
hr = ADsEncodeBinaryData (
	column,
	dwSize,
	&pszDest
	);

if(hr==S_OK)
{
	dwSize = wcslen(pszPrefix) + wcslen(pszDest) + 1;
	pszBinaryFilter = new WCHAR[dwSize];
	swprintf(pszBinaryFilter,pszPrefix,pszDest);
}
else
{
	return hr;
}


 
// Perform the search with the pszDest as the filter string. Code omitted.
. . . 
// Done with the search and free the converted string.
FreeADsMem( pszDest );

Requirements

Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Redistributable: Requires Active Directory Client Extension on Windows NT 4.0 SP6a and Windows 95/98/Me.
Header: Declared in Adshlp.h.
Library: Use ActiveDS.lib.

See Also

ADSI Error Codes, ADSI Functions, FreeADsMem