5

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.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
  • What exactly are you saying is going wrong? The output you provide looks OK to me. – Mats Petersson Aug 05 '13 at 13:59
  • @Drew McGomen : Drive name returns empty. In fact, the drive name is perpended to the directory name along with the long path prefix. Of course, I can adapt my code to this behavior, but I can make sure that this function will always work like this for long paths. This is an undocumented behavior and no one can assure that the function will behave like this forever. – hkBattousai Aug 05 '13 at 16:02
  • Wait, why was I mentioned? Did I make a comment and delete it? – Drew McGowen Aug 05 '13 at 18:57
  • @Drew McGowen : Strange. I swear that the name of the writer of the comment above was "Drew McGowen" then. I directly copy/pasted it. I am so sure that I even remember wondering if it would make a problem not to remove the space after "Drew". I don't know both of you by the way. – hkBattousai Aug 05 '13 at 19:28
  • Ah, you probably grabbed my name from the edit I made (fixed the title, it was being math formatted) – Drew McGowen Aug 05 '13 at 19:34
  • Oh, silly me! I took the wrong name. I'm sorry. – hkBattousai Aug 05 '13 at 19:36

0 Answers0