RtlStringCbCopyUnicodeString - NtDoc

Native API online documentation, based on the System Informer (formerly Process Hacker) phnt headers
#ifndef _NTSTRSAFE_H_INCLUDED_
#ifndef NTSTRSAFE_LIB_IMPL
#ifndef NTSTRSAFE_NO_UNICODE_STRING_FUNCTIONS

NTSTRSAFEDDI
    RtlStringCbCopyUnicodeString(
            _Out_writes_bytes_(cbDest) NTSTRSAFE_PWSTR pszDest,
            _In_ size_t cbDest,
            _In_ PCUNICODE_STRING SourceString)
{
    NTSTATUS status;
    size_t cchDest = cbDest / sizeof(wchar_t);

    status = RtlStringValidateDestW(pszDest,
            cchDest,
            NTSTRSAFE_UNICODE_STRING_MAX_CCH);
    if (NT_SUCCESS(status))
    {
        wchar_t* pszSrc;
        size_t cchSrcLength;

        status = RtlUnicodeStringValidateSrcWorker(SourceString,
                &pszSrc,
                &cchSrcLength,
                NTSTRSAFE_UNICODE_STRING_MAX_CCH,
                0);

        if (NT_SUCCESS(status))
        {
            status = RtlStringCopyWideCharArrayWorker(pszDest,
                    cchDest,
                    NULL,
                    pszSrc,
                    cchSrcLength);
        }
        else
        {
            *pszDest = L'\0';
        }
    }

    return status;
}

#endif
#endif
#endif

View code on GitHub
// ntstrsafe.h

NTSTRSAFEDDI RtlStringCbCopyUnicodeString(
  [out] NTSTRSAFE_PWSTR  pszDest,
  [in]  size_t           cbDest,
  [in]  PCUNICODE_STRING SourceString
);
View the official Windows Driver Kit DDI reference

NtDoc

No description available.

Windows Driver Kit DDI reference (nf-ntstrsafe-rtlstringcbcopyunicodestring)

RtlStringCbCopyUnicodeString function

Description

The RtlStringCbCopyUnicodeString function copies the contents of a UNICODE_STRING structure to a specified destination.

Parameters

pszDest [out]

A pointer to a buffer that receives the copied string. The string that the SourceString parameter's UNICODE_STRING structure points to is copied to the buffer at pszDest and terminated with a null character.

cbDest [in]

The size, in bytes, of the destination buffer that pszDest points to. The buffer must be large enough to contain the string and the terminating null character. The maximum number of bytes in the buffer is NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR).

SourceString [in]

A pointer to a UNICODE_STRING structure that contains the string to be copied. The maximum number of bytes in the string is NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR).

Return value

RtlStringCbCopyUnicodeString returns one of the following NTSTATUS values.

Return code Description
STATUS_SUCCESS This success status means that source data was present, the string was copied without truncation, and the resultant destination buffer is null-terminated.
STATUS_BUFFER_OVERFLOW This warning status means that the copy operation did not complete because of insufficient buffer space. The destination buffer contains a truncated, null-terminated version of the intended result.
STATUS_INVALID_PARAMETER This error status means that the function received an invalid input parameter. For more information, see the following list.

RtlStringCbCopyUnicodeString returns the STATUS_INVALID_PARAMETER value when one of the following occurs:

For information about how to test NTSTATUS values, see Using NTSTATUS Values.

Remarks

The RtlStringCbCopyUnicodeString function uses the destination buffer's size (which the cbDest parameter specifies) to ensure that the copy operation does not write past the end of the buffer.

If the source and destination strings overlap, the behavior of the function is undefined.

The SourceString and pszDest pointers cannot be NULL. If you need to handle NULL pointer values, use the RtlStringCbCopyUnicodeStringEx function.

For more information about the safe string functions, see Using Safe String Functions.

See also

RtlStringCbCopyUnicodeStringEx

RtlStringCchCopyUnicodeString

UNICODE_STRING