Search code examples
windowswinapimsdn

"TCHAR cFileName[MAX_PATH];" - mistake in the MSDN library?


http://msdn.microsoft.com/en-us/library/windows/desktop/aa365740%28v=vs.85%29.aspx

cFileName
The name of the file.

The value of MAX_PATH is the same as almost the same as _MAX_FNAME, but using the first is misleading in this case. Anyway, if they meant the full path with name, should it be cFileName[MAX_PATH+_MAX_FNAME];? What was the purpose of using MAX_PATH?


Solution

  • The MAX_PATH constant represents the maximum length of an entire path, including the file name and the extension. It is irrelevant whether it is a relative path, a fully-qualified path, or even just a file name; they all have the same maximum length.

    Thus, you would never see MAX_PATH + _MAX_FNAME because that would exceed the maximum length allowed for a path.

    You'll find pretty much everything you ever wanted to know about paths in Win32 in this article. Note that some APIs (these will generally be called out explicitly in the docs ) accept long path names, indicated with a special prefix, which are not subject to the limitations of MAX_PATH.

    The function signature is correct in this case. Sometimes, though, there are snippets of sample code on MSDN that accompany the docs, and this sample code ranges anywhere from stylistically curious to an utter abomination. It's always worth using a good healthy dose of common sense when trying to adapt sample code you find.