Je travaille sur le rapport de certaines informations glanées à partir des API du système natif. (Je sais que c'est mauvais ... mais je reçois des informations que je ne peux pas obtenir autrement, et je n'ai pas besoin de mettre à jour mon application si/quand ce temps vient.)Comment puis-je convertir un chemin d'accès natif (NT) en nom de chemin Win32?
L'API renvoie des chemins d'accès natifs, comme vu par ob
, c'est-à-dire \SystemRoot\System32\Ntoskrnl.exe
, ou \??\C:\Program Files\VMWare Workstation\vstor-ws60.sys
.
Je peux remplacer les préfixes communs, à savoir
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"));
}
mais je serais fort surpris s'il n'y a pas une API, natif ou autre, qui les convertir en noms de chemin Win32. Est-ce qu'une telle API existe?
Est-ce que la fonction API shell 'PathCanonicalize' faire l'affaire? http://msdn.microsoft.com/en-us/library/bb773569%28v=vs.85%29.aspx – Praetorian
@Praetorian: Non, PathCanonicalize accepte les chemins d'accès Win32. J'essaie d'obtenir un chemin win32. –
Je ne connais pas une telle fonction, et ce n'est pas toujours possible: NT peut utiliser des chemins que Win32 ne peut pas du tout. Bonne chance de toute façon ... – ephemient