ReallocADsMem function (adshlp.h)

The ReallocADsMem function reallocates and copies an existing memory block.

Syntax

LPVOID ReallocADsMem(
  [in] LPVOID pOldMem,
  [in] DWORD  cbOld,
  [in] DWORD  cbNew
);

Parameters

[in] pOldMem

Type: LPVOID

Pointer to the memory to copy. ReallocADsMem will free this memory with FreeADsMem after it has been copied. If additional memory cannot be allocated, this memory is not freed. This memory must have been allocated with the AllocADsMem, AllocADsStr, ReallocADsMem, or ReallocADsStr function.

The caller must free this memory when it is no longer required by passing this pointer to FreeADsMem.

[in] cbOld

Type: DWORD

Size, in bytes, of the memory to copy.

[in] cbNew

Type: DWORD

Size, in bytes, of the memory to allocate.

Return value

Type: LPVOID

When successful, the function returns a pointer to the new allocated memory. Otherwise it returns NULL.

Remarks

If cbNew is less than cbOld, the existing memory is truncated to fit the new memory size.

Examples

The following code example shows how to use ReallocADsMem to enlarge a string.

LPWSTR pwszPrefix = L"LDAP://"
DWORD dwOldSize = (lstrlenW(pwszPrefix) + 1) * sizeof(WCHAR);

LPWSTR pwszADsPath = (LPWSTR)AllocADsMem(dwOldSize);
if(pwszADsPath)
{
    LPWSTR pwszDN = L"DC=fabrikam,DC=com";

    wcsncpy_s(pwszADsPath, pwszPrefix); // Path becomes "LDAP://"
    wprintf(L"path = %s\n", pwszADsPath);

    DWORD dwNewSize = (lstrlenW(pwszPrefix) + lstrlenW(pwszDN) + 1) * sizeof(WCHAR);
    
    /*
    If successful, this will free the old path buffer, so it does not have to be 
    freed manually. But if it fails, the original memory still exists, so the 
    reallocated memory pointer is temporarily placed in another variable.
    */
    LPWSTR pwszNewPath = (LPWSTR)ReallocADsMem(pwszADsPath, dwOldSize, dwNewSize);
    if(pwszNewPath)
    {
        pwszADsPath = pwszNewPath;

        // Path is still "LDAP://"
        wcsncat_s(pwszADsPath, pwszDN);

        // Path is "LDAP://DC=fabrikam,DC=com"
        wprintf(L"path = %s\n", pwszADsPath);
    }
    else
    {
        wprintf(L"Unable to allocate additional memory.");
    }

    // Free remaining memory.
    FreeADsMem(pwszADsPath);
}

Requirements

Requirement Value
Minimum supported client Windows Vista
Minimum supported server Windows Server 2008
Target Platform Windows
Header adshlp.h
Library Activeds.lib
DLL Activeds.dll

See also

ADSI Functions

AllocADsMem

AllocADsStr

FreeADsMem

ReallocADsMem