Home > Enterprise >  Function GetDriveTypeW always returns DRIVE_NO_ROOT_DIR
Function GetDriveTypeW always returns DRIVE_NO_ROOT_DIR

Time:05-05

As title says, standard Windows GetDriveTypeW returns value 1 (DRIVE_NO_ROOT_DIR) for all passed paths.

Documentation says

The root path is invalid; for example, there is no volume mounted at the specified path.

Although I'm pretty sure the supplied paths (e.g. "C:\\temp", "c:\\temp") is valid and is fixed drive (hard disk).

Am I missing something? What could possibly cause this? Any help would be appreciated - I'm not very familiar with WinAPI.

bool FileAttributesRetriever::IsExternalPath(const std::filesystem::path& path)
{
    uint32_t driveType = GetDriveTypeW(path.wstring().c_str()); // Always 1 / DRIVE_NO_ROOT_DIR

    switch (driveType)
    {
        case DRIVE_REMOTE:      // Network storage.
        case DRIVE_REMOVABLE:   // External drive.
        {
            return true;
        }
        default:
        {
            return false;
        }
    }
}

CodePudding user response:

The documentation also says that the argument must be:

The root directory for the drive.
A trailing backslash is required...

That is you should call it with C:\ if you want to check the C: drive, or C:\temp\ if you want to check the drive mounted at C:\temp.

  • Related