2013-08-05 53 views
3

我正在使用库函数_wsplitpath_s()来解析路径,以便与Win32 API函数一起使用。_wsplitpath_s()替代长路径使用

根据this MSDN document,长路径(长于MAX_PATH个字符)必须与前缀\\?\一起执行。但是,当我将其转换为完整路径字符串时,_wsplitpath_s()无法正确分析它。

示例代码:

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 

为长路径预期行为:

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     : K: 
Directory    : \A Directory\Another Directory\ 
File name    : File Name.After Period 
Extension    : .extension 

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

+0

究竟是什么,你说是哪里错了?你提供的输出对我来说看起来没问题。 –

+0

@Drew McGomen:驱动器名称返回空。实际上,驱动器名称会随着长路径前缀一起驻留到目录名称。当然,我可以使我的代码适应这种行为,但我可以确保这个函数对于长路径总是这样工作。这是一种无证的行为,没有人可以保证函数的行为永远如此。 – hkBattousai

+0

等一下,我为什么提到?我是否发表评论并删除它? –

回答

0

你可以写一个包装函数,简单地跳过“长路”介绍人,例如:

errno_t _wsplitpath_long_s(
    const wchar_t * path, 
    wchar_t * drive, 
    size_t driveNumberOfElements, 
    wchar_t *dir, 
    size_t dirNumberOfElements, 
    wchar_t * fname, 
    size_t nameNumberOfElements, 
    wchar_t * ext, 
    size_t extNumberOfElements 
) 
{ 
    if (_wcsnicmp(path, L"\\\\?\\", 4) == 0) 
     path += 4; 
    return _wsplitpath_s(path, drive, driveNumberOfElements, 
     dir, dirNumberOfElements, fname, nameNumberOfElements, 
     ext, extNumberOfElements); 
}