Is MAX_PATH always same size, even if _UNICODE macro is defined?

Jack picture Jack · May 4, 2014 · Viewed 11k times · Source

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?

Answer

Remy Lebeau picture Remy Lebeau · May 4, 2014

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];