5

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

根据这个 MSDN 文档,长路径(比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 库函数。

4

0 回答 0