Short file names versus long file names in Windows

Posted by normski on Stack Overflow See other posts from Stack Overflow or by normski
Published on 2010-06-01T20:10:44Z Indexed on 2010/06/01 20:23 UTC
Read the original article Hit count: 213

Filed under:
|

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;
  }

© Stack Overflow or respective owner

Related posts about c++

Related posts about Windows