Search code examples
c++cwinapimax-path

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


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?


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