StringCchCopyN

Other versions of this page are also available for the following:

Windows Mobile Not SupportedWindows Embedded CE Supported

8/27/2008

This function is a replacement for strncpy.

StringCchCopyN copies a given number of characters from a source string.

The size, in characters, of the destination buffer is provided to the function to ensure that StringCchCopyN does not write past the end of this buffer.

Syntax

HRESULT StringCchCopyN(      
    LPTSTR pszDest,
    size_t cchDest,
    LPCTSTR pszSrc,
    size_t cchSrc
);

Parameters

  • pszDest
    [out] Pointer to a buffer that receives the copied characters.
  • cchDest
    [in] Size of pszDest, in characters.

    This value must be large enough to hold the copied characters (the length of pszSrc or the value of cchSrc, whichever is smaller) plus 1, to account for the terminating null character.

    The maximum number of characters allowed is STRSAFE_MAX_CCH.

  • pszSrc
    [in] Pointer to a buffer containing the source string.

    This source string must be null-terminated.

  • cchSrc
    [in] The maximum number of characters to copy from pszSrc to pszDest.

Return Value

This function returns an HRESULT, as opposed to strncpy, which returns a pointer.

It is strongly recommended that you use the SUCCEEDED and FAILED macros to test the return value of this function.

Value Description

S_OK

Source data was present, the characters were copied from pszSrc without truncation, and the resultant destination buffer is null-terminated.

STRSAFE_E_INVALID_PARAMETER

The value in cchDest is larger than STRSAFE_MAX_CCH, or the destination buffer is already full.

STRSAFE_E_INSUFFICIENT_BUFFER

The copy operation failed due to insufficient buffer space.

The destination buffer contains a truncated, null-terminated version of the intended result.

Where truncation is acceptable, this is not necessarily a failure condition.

Remarks

StringCchCopyN provides additional processing for proper buffer handling in your code.

Poor buffer handling is implicated in many security issues that involve buffer overruns. StringCchCopyN always null-terminates a nonzero-length destination buffer.

StringCchCopyN can be used in its generic form, or specifically as StringCchCopyNA (for ANSI strings) or StringCchCopyNW (for Unicode strings). The form to use is determined by your data.

String data type String literal Function

char

"string"

StringCchCopyNA

TCHAR

TEXT("string")

StringCchCopyN

WCHAR

L"string"

StringCchCopyNW

Although this routine is meant as a replacement for strncpy, there are differences in behavior. If cchSrc is larger than the number of characters in pszSrc, StringCchCopyN, unlike strncpy, does not continue to pad pszDest with null characters until cchSrc characters have been copied.

If the strings pointed to by pszSrc and pszDest overlap, behavior is undefined.

Neither pszSrc nor pszDest should be NULL.

If you require the handling of null string pointer values, see StringCchCopyNEx.

Requirements

Header strsafe.h
Windows Embedded CE Windows CE 5.0 and later

See Also

Reference

StringCbCopyN
StringCchCopyNEx
StringCchCopy