如何将本机(NT)路径名转换为Win32路径名? [英] How can I convert a native (NT) pathname into a Win32 path name?

查看:297
本文介绍了如何将本机(NT)路径名转换为Win32路径名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在报告从本机系统API收集的一些信息。 (我知道这是坏....但我得到的信息,我不能得到其他,我有没有问题,更新我的应用程序,如果/当时间到了。)



本机API返回本地路径名,如 ob 所示,即 \SystemRoot\System32\Ntoskrnl .exe \ ?? \C:\Program Files\VMWare Workstation \vstor-ws60.sys p>

我可以替换常见的前缀,即

  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,Lglobalroot\\))
{
ntPath.erase(ntPath.begin(),ntPath.begin ()+ 11);
}
if(boost :: istarts_with(ntPath,Lsystemroot))
{
ntPath.replace(ntPath.begin(),ntPath.begin ,GetWindowsPath());
}
if(boost :: istarts_with(ntPath,Lwindows))
{
ntPath.replace(ntPath.begin(),ntPath.begin ,GetWindowsPath());
}
return ntPath;
}

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

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

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

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

TEST(Win32Path,NtPathGlobalRootSystemRoot)
{
ASSERT_EQ(GetCombinedPath(GetWindowsPath(),LHello\\World),NtPathToWin32Path \\globalroot\\SystemRoot\\Hello\\World));
}

但我会非常惊讶,如果没有一些API, ,它会将这些转换为Win32路径名。这样的API是否存在?

解决方案

我们在生产代码中这样做。据我所知,没有API(公共或私有)来处理这个。我们只是做一些字符串比较与一些前缀,它适用于我们。



显然,在ntdll.dll中有一个函数RtlNtPathNameToDosPathName() ,但我不知道它是什么;我想,它有更多的东西像\Device\Harddisk0。



我不知道真的需要这样的功能,虽然。 Win32传递路径(在CreateFile的意义上,等)到NT; NT不传递路径到Win32。所以ntdll.dll并不需要从NT路径到Win32路径。在一些NT查询函数返回完整路径的罕见情况下,任何转换函数可能在Win32 dll内部(例如未导出)。我甚至不知道他们是否打扰,因为像GetModuleFileName()这样的东西将只返回用于加载图像的任何路径。我想这只是一个泄漏的抽象。


I'm working on reporting some information gleaned from native system APIs. (I know this is bad.... but I'm getting information that I can't get otherwise, and I have little issue with having to update my app if/when that time comes around.)

The native API returns native pathnames, as seen by ob, i.e. \SystemRoot\System32\Ntoskrnl.exe, or \??\C:\Program Files\VMWare Workstation\vstor-ws60.sys.

I can replace common prefixes, i.e.

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"));
}

but I'd be strongly surprised if there's not some API, native or otherwise, which will convert these into Win32 path names. Does such an API exist?

解决方案

We do this in production code. As far as I know there is no API (public or private) that handles this. We just do some string comparisons with a few prefixes and it works for us.

Apparently there is a function named RtlNtPathNameToDosPathName() in ntdll.dll (introduced with XP?), but I have no idea what it does; I would guess it has more to do with stuff like \Device\Harddisk0, though.

I'm not sure there is really a need for such a function, though. Win32 passes paths (in the sense of CreateFile, etc) to NT; NT doesn't pass paths to Win32. So ntdll.dll doesn't really have a need to go from NT paths to Win32 paths. In the rare case where some NT query function returns a full path, any conversion function could be internal to the Win32 dll (e.g. not exported). I don't even know if they bother, as stuff like GetModuleFileName() will just return whatever path was used to load the image. I guess this is just a leaky abstraction.

这篇关于如何将本机(NT)路径名转换为Win32路径名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆