Partager via


StringCchLength (Windows Embedded CE 6.0)

1/5/2010

This function is a replacement for strlen.

It is used to ensure that a string is not larger than a given length, in characters. If that condition is met, StringCchLength returns the current length of the string in characters, not including the terminating null character.

Syntax

HRESULT StringCchLength(      
    LPCTSTR psz,
    size_t cchMax,
    size_t *pcch
);

Parameters

  • psz
    [in] Pointer to a buffer containing the string whose length is being checked.
  • cchMax
    [in] The maximum number of characters allowed in psz, including the terminating null character.

    This value cannot exceed STRSAFE_MAX_CCH.

  • pcch
    [out] Pointer to a variable of type size_t containing the number of characters in psz, excluding the terminating null character.

    This value is valid only if pcch is not null and the function succeeds.

Return Value

This function returns an HRESULT, as opposed to strlen, which returns an integer.

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

Value Description

S_OK

The string at psz was not null, and the length of the string (including the terminating null character) is less than or equal to cchMax characters.

STRSAFE_E_INVALID_PARAMETER

The value in psz is NULL, cchMax is larger than STRSAFE_MAX_CCH, or psz is longer than cchMax.

Remarks

StringCchLength is an additional tool for proper buffer handling in your code. Poor buffer handling is implicated in many security issues that involve buffer overruns.

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

String data type String literal Function

char

"string"

StringCchLengthA

TCHAR

TEXT("string")

StringCchLength

WCHAR

L"string"

StringCchLengthW

Requirements

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

See Also

Reference

StrSafe.h Character-Count Functions
StringCbLength