CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Long FileNames

  1. #1
    Join Date
    May 1999
    Posts
    3

    Long FileNames

    When I use file open, mru, or drag and drop a longfilename onto my program the view window has the name in it's title. If I start my app by dropping a longfilename onto the app, my view is titled abcdef~1, not abcdefghi. What is going on. How do I set the correct filename.

    Thanks


  2. #2
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Long FileNames

    This appears to be the shell using the 8.3 filename (perhaps in case the program being started is 16-bit). In your app's InitInstance(), where the command line is parsed, you should check if a filename is specified and use FindFirstFile(), GetFullPathName() or GetLongFileName() to get the long version before passing it on.



    --
    Jason Teagle
    [email protected]

  3. #3
    Join Date
    May 1999
    Posts
    40

    Re: Long FileNames

    The last remnants of DOS 8.3 filenames that still haven't been cleaned out of windows! I haven't had this problem with the MRU or file open but drag and drop is a killer.

    All you need is the function that turns a sausage into a pig!
    The function that turns a DOS 8.3 filename into a long file name.
    I recently saw a posting on CodeGuru of a function that does this.
    Look at http://www.codeguru.com/win32/ShowPath.shtml
    If that doesn't work for you I've appended my own version of such a function to the end of this message.

    You then need to convert the short name to a long name before you open the file.
    You can do this by overriding CWinApp::OpenDocumentFile() and converting the path passed in to a long name before passing it on to the base class e.g.


    CDocument* CMyApp::OpenDocumentFile(LPCTSTR lpszFileName)
    {
    return CWinApp::OpenDocumentFile(shortNameToLongName( lpszFileName));
    }




    This catches the short name and converts before the window title gets set. Has the added advantage that if you use the standard C file functions like fopen they will work (they fail on 8.3 filenames).

    Here is the code to turn a short name into a long name:



    // convert short DOS 8.3 name to long name for file path
    CString shortNameToLongName(CString shortName)
    {
    WIN32_FIND_DATA fd;
    HANDLE hSearch;
    DWORD size;
    BOOL res;

    // get current directory so we can restore later
    size = GetCurrentDirectory(0, NULL);
    LPTSTR currDir = new _TCHAR[size + 1];
    GetCurrentDirectory(size, currDir);

    if (shortName.Find(_T('\\')) < 0)
    {
    // not a full path - just return it
    return shortName;
    }


    // Is it UNC style (//server/share/...) or normal (drive:/...)
    if (shortName.Left(2) == _T("\\\\"))
    {
    // UNC so we to do CD //server/share
    CString s = shortName.Right(shortName.GetLength() - 2);
    int pos = s.Find(_T('\\'));
    ASSERT(pos);
    CString server = shortName.Left(pos + 2);
    s = s.Right(s.GetLength() - pos - 1);
    pos = s.Find(_T('\\'));
    ASSERT(pos);
    CString share = s.Left(pos);
    res = SetCurrentDirectory(server + _T("\\") + share);
    ASSERT(res);
    shortName = s.Right(s.GetLength() - pos - 1);
    } else
    {
    // Normal so we do CD "C:\"
    int pos = shortName.Find(_T('\\'));
    CString drive = shortName.Left(pos + 1);
    res = SetCurrentDirectory(drive);
    ASSERT(res);
    shortName = shortName.Right(shortName.GetLength() - pos - 1);
    }

    int pos = shortName.Find(_T('\\'));
    while (pos >= 0)
    {
    CString s = shortName.Left(pos);
    hSearch = FindFirstFile( s, &fd );
    ASSERT(hSearch != INVALID_HANDLE_VALUE);
    FindClose(hSearch);
    res = SetCurrentDirectory(fd.cFileName);
    ASSERT(res);
    shortName = shortName.Right(shortName.GetLength() - pos - 1);
    pos = shortName.Find(_T('\\'));
    }

    // Whats left in shortName is filename - convert it long
    hSearch = FindFirstFile( shortName, &fd );
    ASSERT(hSearch != INVALID_HANDLE_VALUE);

    // Now append it to current
    // directory.
    size = GetCurrentDirectory(0, NULL);
    LPTSTR dir = new _TCHAR[size + 1];
    GetCurrentDirectory(size, dir);
    CString result = CString(dir) + _T("\\") + fd.cFileName;

    // go back to initial directory
    res = SetCurrentDirectory(currDir);
    ASSERT(res);
    delete currDir;
    delete dir;

    return result;
    }







Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured