如何将本机(NT)路径名转换为Win32路径名?

我正在报告从本机系统API收集的一些信息。 (我知道这很糟糕......但是我得到的信息是我无法得到的,而且如果/当这个时间到来时我不得不更新我的应用程序。) 本机API返回本机路径名,如
ob
所示,即
SystemRootSystem32Ntoskrnl.exe
??C:Program FilesVMWare Workstationvstor-ws60.sys
。 我可以替换公共前缀,即
std::wstring NtPathToWin32Path( std::wstring ntPath )
{
    if (boost::starts_with(ntPath, L"\\?\"))
    {
        ntPath.erase(ntPath.begin(), ntPath.begin() + 4);
        return ntPath;
    }
    if (boost::starts_with(ntPath, L"\??\"))
    {
        ntPath.erase(ntPath.begin(), ntPath.begin() + 4);
    }
    if (boost::starts_with(ntPath, L"\"))
    {
        ntPath.erase(ntPath.begin(), ntPath.begin() + 1);
    }
    if (boost::istarts_with(ntPath, L"globalroot\"))
    {
        ntPath.erase(ntPath.begin(), ntPath.begin() + 11);
    }
    if (boost::istarts_with(ntPath, L"systemroot"))
    {
        ntPath.replace(ntPath.begin(), ntPath.begin() + 10, GetWindowsPath());
    }
    if (boost::istarts_with(ntPath, L"windows"))
    {
        ntPath.replace(ntPath.begin(), ntPath.begin() + 7, GetWindowsPath());
    }
    return ntPath;
}

TEST(Win32Path, NtPathDoubleQuestions)
{
    ASSERT_EQ(L"C:\Example", NtPathToWin32Path(L"\??\C:\Example"));
}

TEST(Win32Path, NtPathUncBegin)
{
    ASSERT_EQ(L"C:\Example", NtPathToWin32Path(L"\\?\C:\Example"));
}

TEST(Win32Path, NtPathWindowsStart)
{
    ASSERT_EQ(GetCombinedPath(GetWindowsPath(), L"Hello\World"), NtPathToWin32Path(L"\Windows\Hello\World"));
}

TEST(Win32Path, NtPathSystemrootStart)
{
    ASSERT_EQ(GetCombinedPath(GetWindowsPath(), L"Hello\World"), NtPathToWin32Path(L"\SystemRoot\Hello\World"));
}

TEST(Win32Path, NtPathGlobalRootSystemRoot)
{
    ASSERT_EQ(GetCombinedPath(GetWindowsPath(), L"Hello\World"), NtPathToWin32Path(L"\globalroot\SystemRoot\Hello\World"));
}
但如果没有一些原生或其他的API会将这些API转换为Win32路径名,我会非常惊讶。这样的API是否存在?     
已邀请:
我们在生产代码中执行此操作。据我所知,没有API(公共或私人)处理这个问题。我们只是用一些前缀做一些字符串比较,它对我们有用。 显然在ntdll.dll中有一个名为RtlNtPathNameToDosPathName()的函数(在XP中引入?),但我不知道它做了什么;我猜它有更多与像 Device Harddisk0这样的东西有关。 不过,我不确定是否真的需要这样的功能。 Win32将路径(在CreateFile等意义上)传递给NT; NT不会将路径传递给Win32。所以ntdll.dll实际上并不需要从NT路径转到Win32路径。在极少数情况下,某些NT查询函数返回完整路径,任何转换函数都可能是Win32 dll的内部(例如,未导出)。我甚至不知道他们是否打扰,因为像GetModuleFileName()这样的东西只会返回用于加载图像的任何路径。我想这只是一个漏洞的抽象。     
这是你可以尝试的东西。首先使用NtCreateFile打开文件,音量等进行阅读。然后使用返回的HANDLE获取此处所述的完整路径。     
检查这一点以获取Win32中的规范路径名。它可能对您有所帮助: http://pdh11.blogspot.com/2009/05/pathcanonicalize-versus-what-it-says-on.html     
看看我对这个问题的回答。 您需要首先获取该路径上的文件句柄,然后获取句柄的Win32路径。     
我写了一个函数,将不同类型的NT设备名称(文件名,COM端口,网络路径等)转换为DOS路径。 有两个功能。一个将句柄转换为NT路径,另一个将此NT路径转换为DOS路径。 看看这里: 如何获得与打开HANDLE相关联的名称
// "DeviceHarddiskVolume3"                                (Harddisk Drive)
// "DeviceHarddiskVolume3Temp"                           (Harddisk Directory)
// "DeviceHarddiskVolume3Temptransparent.jpeg"          (Harddisk File)
// "DeviceHarddisk1DP(1)0-0+6foto.jpg"                  (USB stick)
// "DeviceTrueCryptVolumePDataPasswords.txt"            (Truecrypt Volume)
// "DeviceFloppy0Autoexec.bat"                           (Floppy disk)
// "DeviceCdRom1VIDEO_TSVTS_01_0.VOB"                   (DVD drive)
// "DeviceSerial1"                                        (real COM port)
// "DeviceUSBSER000"                                      (virtual COM port)
// "DeviceMupComputerNameC$Boot.ini"                   (network drive share,  Windows 7)
// "DeviceLanmanRedirectorComputerNameC$Boot.ini"      (network drive share,  Windwos XP)
// "DeviceLanmanRedirectorComputerNameSharesDance.m3u" (network folder share, Windwos XP)
// "DeviceAfd"                                            (internet socket)
// "DeviceConsole000F"                                    (unique name for any Console handle)
// "DeviceNamedPipePipename"                             (named pipe)
// "BaseNamedObjectsObjectname"                           (named mutex, named event, named semaphore)
// "REGISTRYMACHINESOFTWAREClasses.txt"                (HKEY_CLASSES_ROOT.txt)
    

要回复问题请先登录注册