Bewerken

Delen via


RtlUnicodeStringCbCopyNEx function (ntstrsafe.h)

The RtlUnicodeStringCbCopyNEx function copies a string from one UNICODE_STRING structure to another while limiting the size of the copied string.

Syntax

NTSTRSAFEDDI RtlUnicodeStringCbCopyNEx(
  [out]           PUNICODE_STRING  DestinationString,
  [in]            PCUNICODE_STRING SourceString,
  [in]            size_t           cbToCopy,
  [out, optional] PUNICODE_STRING  RemainingString,
  [in]            DWORD            dwFlags
);

Parameters

[out] DestinationString

Optional. A pointer to a UNICODE_STRING structure that receives the copied string. The string that the SourceString parameter's UNICODE_STRING structure points to is copied to the buffer that the DestinationString parameter's UNICODE_STRING structure points to. The maximum number of bytes in the DestinationString structure's string buffer is NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR). DestinationString can be NULL, but only if STRSAFE_IGNORE_NULLS is set in dwFlags.

[in] SourceString

Optional. A pointer to a UNICODE_STRING structure that contains the string to be copied. The maximum number of bytes in the structure's string buffer is NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR). SourceString can be NULL, but only if STRSAFE_IGNORE_NULLS is set in dwFlags.

[in] cbToCopy

The number of bytes to be copied from the source to the destination.

[out, optional] RemainingString

Optional. If the caller supplies a non-NULL pointer to a UNICODE_STRING structure, the function sets this structure's Buffer member to the end of the concatenated string, sets the structure's Length member to zero, and sets the structure's MaximumLength member to the number of bytes that are remaining in the destination buffer. RemainingString can be NULL, but only if STRSAFE_IGNORE_NULLS is set in dwFlags.

[in] dwFlags

One or more flags and, optionally, a fill byte. The flags are defined as follows:

Value Meaning
STRSAFE_FILL_BEHIND_NULL If this flag is set and the function succeeds, the low byte of dwFlags is used to fill the portion of the destination buffer that follows the last character in the string.
STRSAFE_IGNORE_NULLS If this flag is set, the source or destination pointer, or both, can be NULL. RtlUnicodeStringCbCopyNEx treats NULL source buffer pointers like empty strings (TEXT("")), which can be copied. NULL destination buffer pointers cannot receive nonempty strings.
STRSAFE_FILL_ON_FAILURE If this flag is set and the function fails, the low byte of dwFlags is used to fill the entire destination buffer. This operation overwrites any preexisting buffer contents.
STRSAFE_NULL_ON_FAILURE If this flag is set and the function fails, the destination buffer is set to an empty string (TEXT("")). This operation overwrites any preexisting buffer contents.
STRSAFE_NO_TRUNCATION

If this flag is set and the function returns STATUS_BUFFER_OVERFLOW:

  • If STRSAFE_FILL_ON_FAILURE is also specified, STRSAFE_NO_TRUNCATION fills the destination buffer accordingly.
  • Otherwise, the contents of the destination buffer will be set to an empty string, even if STRSAFE_NULL_ON_FAILURE is not set. STRSAFE_FILL_BEHIND_NULL is ignored.
STRSAFE_ZERO_LENGTH_ON_FAILURE If this flag is set and the function returns STATUS_BUFFER_OVERFLOW, the destination string length is set to zero bytes.

Return value

RtlUnicodeStringCbCopyNEx returns one of the following NTSTATUS values.

Return code Description
STATUS_SUCCESS This success status means source data was present, and the strings were concatenated without truncation.
STATUS_BUFFER_OVERFLOW This warning status means that the copy operation did not complete because of insufficient space in the destination buffer. If STRSAFE_NO_TRUNCATION is set in dwFlags, the destination buffer is not modified. If the flag is not set, the destination buffer contains a truncated version of the copied string.
STATUS_INVALID_PARAMETER This error status means that the function received an invalid input parameter. For more information, see the following list.

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

  • The contents of a UNICODE_STRING structure are invalid.
  • An invalid flag is specified in dwFlags.
  • The destination buffer is already full.
  • A buffer pointer is NULL and the STRSAFE_IGNORE_NULLS flag is not specified in dwFlags.
  • The destination buffer pointer is NULL, but the buffer size is not zero.
  • The destination buffer pointer is NULL, or its length is zero, but a nonzero length source string is present.
  • The cbToCopy parameter's value is greater than NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR).

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

Remarks

The RtlUnicodeStringCbCopyNEx function uses the destination buffer's size to ensure that the copy operation does not write past the end of the buffer. By default, the function does not terminate the resultant string with a null character value (that is, with zero). As an option, the caller can use the STRSAFE_FILL_BEHIND flag and a fill byte value of zero to null-terminate a resultant string that does not occupy the entire destination buffer.

RtlUnicodeStringCbCopyNEx adds to the functionality of the RtlUnicodeStringCbCopyN function by returning a UNICODE_STRING structure that identifies the end of the destination string and the number of bytes that are left unused in that string. You can pass flags to RtlUnicodeStringCbCopyNEx for additional control.

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

The SourceString and DestinationString pointers cannot be NULL unless the STRSAFE_IGNORE_NULLS flag is set in dwFlags. If STRSAFE_IGNORE_NULLS is set, one or both of these pointers can be NULL. If the DestinationString pointer is NULL, the SourceString pointer must be NULL or the UNICODE_STRING structure must contain an empty string.

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

Requirements

Requirement Value
Minimum supported client Available starting with Windows XP with Service Pack 1 (SP1).
Target Platform Desktop
Header ntstrsafe.h (include Ntstrsafe.h)
Library Ntstrsafe.lib
IRQL Any if strings being manipulated are always resident in memory, otherwise PASSIVE_LEVEL

See also