_wsplitpath_s()替代使用长路径

时间:2013-08-05 13:17:57

标签: c++ winapi string-parsing long-filenames

我正在使用库函数_wsplitpath_s()解析路径以与Win32 API函数一起使用。

根据this MSDN document,必须使用前缀MAX_PATH来填充长路径(长度超过\\?\个字符)。但是,当我将其置于完整路径字符串时,_wsplitpath_s()无法正确解析它。

示例代码:

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;

上述代码的实际输出:

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

如何使用短路径:

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

长路径的预期行为:

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

是否有_wsplitpath_s()的替代方案支持长路径命名约定?
按给定的顺序接受STL算法,Win32 API函数或C库函数。

0 个答案:

没有答案