PathCchAddExtension function (pathcch.h)

Adds a file name extension to a path string.

This function differs from PathAddExtension in that it accepts paths with "\", "\?" and "\?\UNC" prefixes.

Note  This function should be used in place of PathAddExtension to prevent the possibility of a buffer overrun.


  [in, out] PWSTR  pszPath,
  [in]      size_t cchPath,
  [in]      PCWSTR pszExt


[in, out] pszPath

A pointer to the path string. When this function returns successfully, the buffer contains the string with the appended extension. This value should not be NULL.

Note  If the original string already has a file name extension present, no new extension will be added and the original string will be unchanged.

[in] cchPath

The size of the buffer pointed to by pszPath, in characters.

[in] pszExt

A pointer to the file name extension string. This string can be given either with or without a preceding period (".ext" or "ext").

Return value

This function returns an HRESULT code, including the following.

Return code Description
The function succeeded. Note that this also includes the case of an empty extension, such as a period with no characters following it. In that case, the original string is returned unaltered.
This value can be caused by several things, such as the pszPath param being set to NULL, the cchPath being set to 0 or a value greater than PATHCCH_MAX_CCH, or the extension string containing illegal characters or otherwise not being a valid extension.
The original string already has an extension.
The buffer is too small to hold the returned string.


Requirement Value
Minimum supported client Windows 8 [desktop apps | UWP apps]
Minimum supported server Windows Server 2012 [desktop apps | UWP apps]
Target Platform Windows
Header pathcch.h
Library Pathcch.lib