Ich arbeite daran, einige Informationen aus nativen System-APIs zu melden. (Ich weiß, das ist schlecht .... aber ich bekomme Informationen, die ich sonst nicht bekommen kann, und ich habe wenig Problem mit meiner App zu aktualisieren, wenn/wann diese Zeit kommt.)Wie kann ich einen nativen (NT) Pfadnamen in einen Win32 Pfadnamen konvertieren?
Die native API gibt native Pfadnamen zurück, wie aus ob
, dh \SystemRoot\System32\Ntoskrnl.exe
, oder \??\C:\Program Files\VMWare Workstation\vstor-ws60.sys
ersichtlich ist.
ich gemeinsame Präfixe ersetzen kann, das heißt
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"));
}
aber ich stark wäre überrascht, wenn es nicht einige API ist, nativ oder anders, die diese in Win32-Pfadnamen umwandeln. Existiert eine solche API?
Ist die Shell-API-Funktion 'PathCanonicalize' den Trick? http://msdn.microsoft.com/en-us/library/bb773569%28v=vs.85%29.aspx – Praetorian
@ Praetorian: Nein, PathCanonicalize akzeptiert Win32-Pfade. Ich versuche einen Win32 Pfad zu bekommen. –
Ich kenne keine solche Funktion, und es ist nicht immer möglich: NT kann Pfade verwenden, die Win32 überhaupt nicht kann. Viel Glück trotzdem ... – ephemient