#ifndef _NTSTRSAFE_H_INCLUDED_
#ifndef NTSTRSAFE_LIB_IMPL
#ifndef NTSTRSAFE_NO_UNICODE_STRING_FUNCTIONS
/*++
NTSTATUS
RtlStringCchCopyUnicodeString(
_Out_writes_(cchDest) PWSTR pszDest,
_In_ size_t cchDest,
_In_ PCUNICODE_STRING SourceString,
);
Routine Description:
This routine copies a PUNICODE_STRING to a PWSTR. This function will not
write past the end of this buffer and it will ALWAYS null terminate the
destination buffer (unless it is zero length).
This function returns an NTSTATUS value, and not a pointer. It returns
STATUS_SUCCESS if the string was copied without truncation, otherwise it
will return a failure code. In failure cases as much of SourceString will be
copied to pszDest as possible.
Arguments:
pszDest - destination string
cchDest - size of destination buffer in characters.
length must be = ((DestinationString->Length / sizeof(wchar_t)) + 1)
to hold all of the source and null terminate the string.
SourceString - pointer to the counted unicode source string
Notes:
Behavior is undefined if source and destination strings overlap.
SourceString and pszDest should not be NULL. See RtlStringCchCopyUnicodeStringEx
if you require the handling of NULL values.
Return Value:
STATUS_SUCCESS - if there was source data and it was all copied and the
resultant dest string was null terminated
failure - the operation did not succeed
STATUS_BUFFER_OVERFLOW
Note: This status has the severity class Warning - IRPs completed with this
status do have their data copied back to user mode
- this return value is an indication that the copy
operation failed due to insufficient space. When this
error occurs, the destination buffer is modified to
contain a truncated version of the ideal result and is
null terminated. This is useful for situations where
truncation is ok.
It is strongly recommended to use the NT_SUCCESS() macro to test the
return value of this function.
--*/
NTSTRSAFEDDI
RtlStringCchCopyUnicodeString(
_Out_writes_(cchDest) NTSTRSAFE_PWSTR pszDest,
_In_ size_t cchDest,
_In_ PCUNICODE_STRING SourceString)
{
NTSTATUS status;
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;
}
View code on GitHub// ntstrsafe.h
NTSTRSAFEDDI RtlStringCchCopyUnicodeString(
[out] NTSTRSAFE_PWSTR pszDest,
[in] size_t cchDest,
[in] PCUNICODE_STRING SourceString
);
View the official Windows Driver Kit DDI referenceNo description available.
The RtlStringCchCopyUnicodeString function copies the contents of a UNICODE_STRING structure to a specified destination.
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.
cchDest [in]The size, in characters, of the destination buffer. The buffer must be large enough for the string and the terminating null character. The maximum number of characters is NTSTRSAFE_UNICODE_STRING_MAX_CCH.
SourceString [in]A pointer to a UNICODE_STRING structure that contains the string to be copied. The maximum number of characters in the string is NTSTRSAFE_UNICODE_STRING_MAX_CCH.
RtlStringCchCopyUnicodeString 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. |
RtlStringCchCopyUnicodeString returns the STATUS_INVALID_PARAMETER value when one of the following occurs:
For information about how to test NTSTATUS values, see Using NTSTATUS Values.
The RtlStringCchCopyUnicodeString function uses the destination buffer's size (which cchDest 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 RtlStringCchCopyUnicodeStringEx function.
For more information about the safe string functions, see Using Safe String Functions.
RtlStringCchCopyUnicodeStringEx