Question

Should I make room to it, like this:

 len = MAX_PATH * sizeof(_TCHAR) + sizeof(_TCHAR);

or is:

len = MAX_PATH + sizeof(_TCHAR);

Right size to hold a path including unicode?

Was it helpful?

Solution

MAX_PATH (which is always 260) is expressed in characters, not in bytes.

Use the first one when allocating raw memory that is expressed in byte sizes, eg:

LPTSTR path = (LPTSTR) LocalAlloc(LMEM_FIXED, (MAX_PATH + 1) * sizeof(TCHAR));

Use the second one when allocating memory that is expressed in characters, eg:

TCHAR path[MAX_PATH + 1];

LPTSTR path = new TCHAR[MAX_PATH +1];

OTHER TIPS

MAX_PATH is defined as 260 in Windef.h irrespective of Unicode.

First approach is fine if you are holding number of bytes. Second approach does not make any sense. If you are holding number of characters, you should use MAX_PATH+1.

The MAX_PATH macro definition (260 bytes) is an adopted ANSI standard. Unfortunately, this standard was developed long before the advent of the 32-bit OS.

Currently, Unicode versions of the (Windows) file libraries support a path up to 32,767 bytes. However, the MAX_PATH definition remains as it was for (16-bit) MS-DOS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top