RtlUnicodeStringPrintf - 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
#ifndef _M_CEE_PURE

/*++

  NTSTATUS
  RtlUnicodeStringPrintf(
  _Inout_                     PUNICODE_STRING DestinationString,
  _In_ _Printf_format_string_ PCWSTR          pszFormat,
  ...
  );

  Routine Description:

  This routine is a safer version of the C built-in function 'sprintf' for
  PUNICODE_STRINGs.

  This function returns an NTSTATUS value, and not a pointer. It returns
  STATUS_SUCCESS if the string was printed without truncation, otherwise it
  will return a failure code. In failure cases it will return a truncated
  version of the ideal result.

Arguments:

DestinationString   -  pointer to the counted unicode destination string

pszFormat           -  format string which must be null terminated

...                 -  additional parameters to be formatted according to
the format string

Notes:
Behavior is undefined if destination, format strings or any arguments
strings overlap.

DestinationString and pszFormat should not be NULL.  See RtlUnicodeStringPrintfEx if you
require the handling of NULL values.

Return Value:

STATUS_SUCCESS -   if there was sufficient space in the dest buffer for
the resultant string

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 print
operation failed due to insufficient space. When this
error occurs, the destination buffer is modified to
contain a truncated version of the ideal result. 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
RtlUnicodeStringPrintf(
        _Inout_ PUNICODE_STRING DestinationString,
        _In_ _Printf_format_string_ NTSTRSAFE_PCWSTR pszFormat,
        ...)
{
    NTSTATUS status;
    wchar_t* pszDest;
    size_t cchDest;

    status = RtlUnicodeStringValidateDestWorker(DestinationString,
            &pszDest,
            &cchDest,
            NULL,
            NTSTRSAFE_UNICODE_STRING_MAX_CCH,
            0);

    if (NT_SUCCESS(status))
    {
        va_list argList;
        size_t cchNewDestLength = 0;

        va_start(argList, pszFormat);

        status = RtlWideCharArrayVPrintfWorker(pszDest,
                cchDest,
                &cchNewDestLength,
                pszFormat,
                argList);

        va_end(argList);

        // safe to multiply cchNewDestLength * sizeof(wchar_t) since cchDest < NTSTRSAFE_UNICODE_STRING_MAX_CCH and sizeof(wchar_t) is 2
        DestinationString->Length = (USHORT)(cchNewDestLength * sizeof(wchar_t));
    }

    return status;
}

#endif
#endif
#endif
#endif

View code on GitHub
// ntstrsafe.h

NTSTRSAFEDDI RtlUnicodeStringPrintf(
  [out] PUNICODE_STRING  DestinationString,
  [in]  NTSTRSAFE_PCWSTR pszFormat,
        ...              
);
View the official Windows Driver Kit DDI reference

NtDoc

No description available.

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

RtlUnicodeStringPrintf function

Description

The RtlUnicodeStringPrintf function creates a text string, with formatting that is based on supplied formatting information, and stores the string in a UNICODE_STRING structure.

Parameters

DestinationString [out]

A pointer to a UNICODE_STRING structure that receives a formatted string. RtlUnicodeStringPrintf creates this string from the formatting string that pszFormat specifies and the function's argument list. The maximum number of characters in the string is NTSTRSAFE_UNICODE_STRING_MAX_CCH.

pszFormat [in]

A pointer to a null-terminated text string that contains printf-styled formatting directives.

...

Optional. A list of arguments that the function interprets, based on formatting directives that the pszFormat string contains.

Return value

RtlUnicodeStringPrintf 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 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.

RtlUnicodeStringPrintf 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 RtlUnicodeStringPrintf function uses the destination buffer's size to ensure that the string formatting operation does not write past the end of the buffer. The function does not terminate the resultant string with a null character.

If the format string and destination string overlap, the behavior of the function is undefined.

The pszFormat and DestinationString pointers cannot be NULL. If you need to handle NULL pointer values, use the RtlUnicodeStringPrintfEx function.

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

See also

RtlUnicodeStringPrintfEx

RtlUnicodeStringVPrintf

RtlUnicodeStringVPrintfEx

UNICODE_STRING