StringCbCopyN

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.

StringCbCopyN copies a given number of bytes from a source string.

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

Syntax

HRESULT StringCbCopyN(      
    LPTSTR pszDest,
    size_t cbDest,
    LPCTSTR pszSrc,
    size_t cbSrc
);

Parameters

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

    This value must be large enough to hold the copied bytes (the size of pszSrc or the value of cbSrc, whichever is smaller) and also account for the terminating null character.

    The maximum number of characters allowed is STRSAFE_MAX_CCH * sizeof(TCHAR).

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

    This source string must be null-terminated.

  • cbSrc
    [in] The maximum number of bytes 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 data was copied from pszSrc without truncation, and the resultant destination buffer is null-terminated.

STRSAFE_E_INVALID_PARAMETER

The value in cbDest is larger than STRSAFE_MAX_CCH * sizeof(TCHAR), or the destination buffer is 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

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

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

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

String data type String literal Function

char

"string"

StringCbCopyNA

TCHAR

TEXT("string")

StringCbCopyN

WCHAR

L"string"

StringCbCopyNW

Although this routine is meant as a replacement for strncpy, there are differences in behavior. If cbSrc is larger than the number of bytes in pszSrc, StringCbCopyN, unlike strncpy, does not continue to pad pszDest with null characters until cbSrc bytes 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 need the handling of null string pointer values, see StringCbCopyNEx.

Requirements

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

See Also

Reference

StringCchCopyN
StringCbCopyNEx
StringCbCopy