This topic has not yet been rated - Rate this topic

wnsprintf function

Applies to: desktop apps only

Takes a variable-length argument list and returns the values of the arguments as a printf-style formatted string.

Note  Do not use this function. See Remarks for alternative functions.

Syntax

int wnsprintf(
  __out  PTSTR pszDest,
  __in   int cchDest,
  __in   PCTSTR pszFmt,
  __in    ...
);

Parameters

pszDest [out]

Type: PTSTR

A pointer to a buffer that, when this function returns successfully, receives the output string.

cchDest [in]

Type: int

The maximum number of characters allowed in pszDest.

pszFmt [in]

Type: PCTSTR

A printf-style format string. The %s format identifier should never be used in an unbounded form. To avoid potential buffer overruns, always specify a size; for instance "%32s".

... [in]

Additional parameters that contain the data to be output.

Return value

Type: int

Returns the number of characters written to the buffer, excluding any terminating NULL characters. A negative value is returned if an error occurs.

Remarks

Security Warning:  Using this function incorrectly can compromise the security of your application. The copied string is not guaranteed to be null-terminated. Consider using one of the following alternatives. StringCbPrintf, StringCbPrintfEx, StringCbVPrintf, StringCbVPrintfEx, StringCchPrintf, StringCchPrintfEx, StringCchVPrintf, or StringCchVPrintfEx. You should review Security Considerations: Microsoft Windows Shell before continuing.

This is a Windows version of sprintf. It does not support floating-point or pointer types. It supports only the left alignment flag.

Requirements

Minimum supported client

Windows 2000 Professional, Windows XP

Minimum supported server

Windows 2000 Server

Header

Shlwapi.h

Library

Shlwapi.lib

DLL

Shlwapi.dll (version 5.0 or later)

Unicode and ANSI names

wnsprintfW (Unicode) and wnsprintfA (ANSI)

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Security hole?
This function takes a parameter for the destination length, so it should not lead to buffer overflow if you set this parameter correctly.
The alternative functions can also produce overflows when length parameter is wrong, and bloat up your code because it's inlined.

For length of arrays, use elemof macro like:
#define elemof(x) (sizeof(x)/sizeof(*(x)))
So your code can easily compiled for Unicode (UTF-16).