Alan Gorman
Alan Gorman

Reputation: 61

Short file names versus long file names in Windows

I have some code which gets the short name from a file path, using GetShortNameW(), and then later retrieves the long name view GetLongNameA().

The original file is of the form

"C:/ProgramData/My Folder/File.ext"

However, following conversion to short, then back to long, the filename becomes

"C:/Program Files/My Folder/Filename.ext".

The short name is of the form

"C:/PROGRA~2/MY_FOL~1/FIL~1.EXT"

The short name is being incorrectly resolved.

The code compiles using VS 2005 on Windows 7 (I cannot upgrade the project to VS2008)

Does anybody have any idea why this might be happening?

  DWORD pathLengthNeeded = ::GetShortPathNameW(aRef->GetFilePath().c_str(), NULL, 0);
  if(pathLengthNeeded != 0)
  {
   WCHAR* shortPath = new WCHAR[pathLengthNeeded];
   DWORD newPathNameLength = ::GetShortPathNameW(aRef->GetFilePath().c_str(), shortPath, pathLengthNeeded);
   if(newPathNameLength != 0)
   {
    UI_STRING unicodePath(shortPath);
    std::string asciiPath = StringFromUserString(unicodePath);

    pathLengthNeeded = ::GetLongPathNameA(asciiPath.c_str(),NULL, 0);
    if(pathLengthNeeded != 0)
    {// convert it back to a long path if possible. For goodness sake can't we use Unicode throughout?F
     char* longPath = new char[pathLengthNeeded];
     DWORD newPathNameLength = ::GetLongPathNameA(asciiPath.c_str(), longPath, pathLengthNeeded);
     if(newPathNameLength != 0)
     {
      std::string longPathString(longPath, newPathNameLength);
      asciiPath = longPathString;
     }
     delete [] longPath;
    }

    SetFullPathName(asciiPath);
   }
   delete [] shortPath;
  }

Upvotes: 1

Views: 2326

Answers (1)

Pavel Radzivilovsky
Pavel Radzivilovsky

Reputation: 19104

It should have been vice versa: GetShortPathNameA and GetLongPathNameW.

GetLongPathNameA should not necessarily succeed. Only 8.3 pathnames are [partially guaranteed] to be ANSI-compatible.

Upvotes: 1

Related Questions