_wsplitpath_s() alternative to use with long paths

2.6k Views Asked by At

I'm parsing paths by using the library function _wsplitpath_s() to use with Win32 API functions.

According to this MSDN document, long paths (longer than MAX_PATH characters) must be perpended with with the prefix \\?\. However, when I perpend it to a full path string, _wsplitpath_s() cannot parse it correctly.

Example code:

std::wstring FullPath = L"\\\\?\\K:\\A Directory\\Another Directory\\File Name.After Period.extension";
std::wstring Drive, Directory, FileName, Extension;
Drive.resize        (FullPath.size());
Directory.resize    (FullPath.size());
FileName.resize     (FullPath.size());
Extension.resize    (FullPath.size());
errno_t Error = _wsplitpath_s(  FullPath.c_str(),
                                &Drive[0],
                                Drive.size(),
                                &Directory[0],
                                Directory.size(),
                                &FileName[0],
                                FileName.size(),
                                &Extension[0],
                                Extension.size());
std::wcout << L"Full path to be splitted :    " << FullPath.c_str()     << std::endl;
std::wcout << L"Drive                    :    " << Drive.c_str()        << std::endl;
std::wcout << L"Directory                :    " << Directory.c_str()    << std::endl;
std::wcout << L"File name                :    " << FileName.c_str()     << std::endl;
std::wcout << L"Extension                :    " << Extension.c_str()    << std::endl;
std::wcout << L"Error value returned     :    " << Error                << std::endl;
std::wcout << L"Did error occur?         :    " << (Error == EINVAL)    << std::endl;
std::wcout << L"Value of 'EINVAL'        :    " << EINVAL               << std::endl;

Actual output of the code above:

Full path to be splitted :    \\?\K:\A Directory\Another Directory\File Name.After Period.extension
Drive                    :
Directory                :    \\?\K:\A Directory\Another Directory\
File name                :    File Name.After Period
Extension                :    .extension
Error value returned     :    0
Did error occur?         :    0
Value of 'EINVAL'        :    22

How it works with short paths:

Full path to be splitted :    K:\A Directory\Another Directory\File Name.After Period.extension
Drive                    :    K:
Directory                :    \A Directory\Another Directory\
File name                :    File Name.After Period
Extension                :    .extension

Expected behavior for long paths:

Full path to be splitted :    \\?\K:\A Directory\Another Directory\File Name.After Period.extension
Drive                    :    K:
Directory                :    \A Directory\Another Directory\
File name                :    File Name.After Period
Extension                :    .extension

Is there an alternative of _wsplitpath_s() which supports long path naming convention?
An STL algorithm, Win32 API function or a C library function is accepted in the given order.

0

There are 0 best solutions below