Directory Services

AllocADsMem

The AllocADsMem function allocates, in bytes, a block of memory of the specified size.

LPVOID AllocADsMem(
  DWORD cb
);

Parameters

cb
[in] The amount of memory, in bytes, to be allocated.

Return Values

When successful, the function returns a non-NULL pointer to the allocated memory. Otherwise, it returns NULL. Call ADsGetLastError to obtain extended error status. For more information about error code values, see ADSI Error Codes.

Remarks

The memory block returned by AllocADsMem is initialized to zero.

The following code example builds an ADsPath string by concatenating the relative name of an object and the path of its parent. To do so, it first calls AllocADsMem to create a buffer of sufficient length. Error checking is omitted in the example.

HRESULT buildADsPath(BSTR parent, BSTR child, BSTR *pADsPath)
{
	LPWSTR lpADsPath=NULL;
	HRESULT hr = S_OK;
	DWORD dwLen = 0;

	if(!parent || !child || !pADsPath)
	{
		return ERROR_INVALID_PARAMETER;
}
	dwLen = wcslen(parent) + wcslen(Name) + 2 + MAX_PATH;
 
	// Create a buffer for the string.
	lpADsPath = (LPWSTR)AllocADsMem(dwlen*sizeof(WCHAR));
	if(!lpADspath) return (E_OUTOFMEMORY);
 
	wcsncpy(lpADsPath,parent, dwLen);
	wcsncat(lpADsPath,L"/", dwLen - wcslen(lpADsPath));
	wcsncat(lpADsPath, child, dwLen - wcslen(lpADsPath));
	pADsPath = AllocADsStr(lpADsPath);
 
	// Release the buffer.
	if(lpADsPath) FreeADsMem(lpADsPath);
	return (hr);
}

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, ADsGetLastError, FreeADsMem