Windows下DeviceDosPath转NtPath(\\Device\\HarddiskVolume1\a.dll to c:\a.dll)

xingyun86 2021-6-25 1508

Windows下DeviceDosPath转NtPath(\\Device\\HarddiskVolume1\a.dll to c:\a.dll)

        //\\Device\\HarddiskVolume1\a.dll to c:\a.dll    
        std::string DeviceDosPathToNtPath(const std::string& DeviceDosPath)
        {
            CHAR szTargetPath[MAX_PATH] = { 0 };
            CHAR szDeviceString[MAX_PATH] = { 0 };

            //获取本地磁盘字符串
            if (GetLogicalDriveStringsA(sizeof(szDeviceString) / sizeof(*szDeviceString), szDeviceString))
            {
                for (CHAR* Device = szDeviceString; Device != NULL; Device = Device + (lstrlenA(Device) + 2))
                {
                    Device[2] = '\0';
                    if (!lstrcmpiA(Device, ("A:")) || !lstrcmpiA(Device, ("B:")))
                    {
                        continue;
                    }
                    if (!QueryDosDeviceA(Device, szTargetPath, sizeof(szTargetPath) / sizeof(*szTargetPath)))//查询 Dos 设备名  
                    {
                        return FALSE;
                    }
                    if (_strnicmp(DeviceDosPath.c_str(), szTargetPath, lstrlenA(szTargetPath)) == 0)//命中  
                    {
                        return (Device + DeviceDosPath.substr(lstrlenA(szTargetPath)));//复制驱动器+路径
                    }
                }
            }

            return DeviceDosPath;
        }

        //\\Device\\HarddiskVolume1\a.dll to c:\a.dll    
        std::wstring DeviceDosPathToNtPath(const std::wstring& DeviceDosPath)
        {
            WCHAR szTargetPath[MAX_PATH] = { 0 };
            WCHAR szDeviceString[MAX_PATH] = { 0 };

            //获取本地磁盘字符串
            if (GetLogicalDriveStringsW(sizeof(szDeviceString) / sizeof(*szDeviceString), szDeviceString))
            {
                for (WCHAR* Device = szDeviceString; Device != NULL; Device = Device + (lstrlenW(Device) + 2))
                {
                    Device[2] = L'\0';
                    if (!lstrcmpiW(Device, (L"A:")) || !lstrcmpiW(Device, (L"B:")))
                    {
                        continue;
                    }
                    if (!QueryDosDeviceW(Device, szTargetPath, sizeof(szTargetPath)/sizeof(*szTargetPath)))//查询 Dos 设备名  
                    {
                        return FALSE;
                    }
                    if (_wcsnicmp(DeviceDosPath.c_str(), szTargetPath, lstrlenW(szTargetPath)) == 0)//命中  
                    {
                        return (Device + DeviceDosPath.substr(lstrlenW(szTargetPath)));//复制驱动器+路径
                    }
                }
            }

            return DeviceDosPath;
        }


×
打赏作者
最新回复 (0)
查看全部
全部楼主
返回