BUG: Cannot Open Document From Shell if EXE Has Long File Name

ID: Q148806


The information in this article applies to:
  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++, 32-bit Editions, version 6.0
      on the following platforms: Win95, Win98


SYMPTOMS

When you try to open a document whose extension is associated with an MFC application that has a long file name on Windows 95, a dialog box with an error message similar to the following appears:

Cannot find the file 'filename' (or one of its components). Make sure the path and filename are correct and that all required libraries are available.
Closing the message box won't bring up the application's windows, but will leave the executable file open.


CAUSE

The MFC function CWinApp::RegisterShellFileTypes() writes the application's 8.3 file name to the registry. However, CWinApp::EnableShellOpen() calls ::GlobalAddAtom() with the application's long file name. Windows 95 and Windows 98 cannot handle this situation.


RESOLUTION

Override CWinApp::EnableShellOpen() using the sample code below to pass the application's short file name to ::GlobalAddAtom().


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

Steps to Reproduce the Problem on a Computer with Windows 95 or 98

  1. Select a new MFC AppWizard(exe) project to create an MDI application with a long project name.


  2. At step 3 of the AppWizard select Automation Support.


  3. Then at step 4 of the AppWizard, choose Advanced and specify a 3 letter file extension.


  4. Build the application and run it once so the file type is registered by the call to RegisterShellFileTypes in InitInstance.


  5. Save a file with the selected file extension and double-click to open it from the Windows Explorer.



This can be worked around by overriding CWinApp::EnableShellOpen() and registering the application name atom using the short filename. Following is a sample of what the code should look like:

/* Compile options needed: Default MFC applications.
*/ 

void CLongFileNameApp::EnableShellOpen()
{
   ASSERT(m_atomApp == NULL && m_atomSystemTopic == NULL); // do once

   CString strShortName;
   TCHAR szLongPathName[_MAX_PATH];
   ::GetModuleFileName(m_hInstance, szLongPathName, _MAX_PATH);
   if (::GetShortPathName(szLongPathName,
       strShortName.GetBuffer(_MAX_PATH), _MAX_PATH) == 0)
   {
      // Rare failure case (especially on not-so-modern file systems)
      strShortName = szLongPathName;
   }
   strShortName.ReleaseBuffer();
   int nPos = strShortName.ReverseFind('\\');
   if (nPos != -1)
     strShortName = strShortName.Right(strShortName.GetLength()- nPos-1);
   nPos = strShortName.ReverseFind('.');
   if (nPos != -1)
     strShortName = strShortName.Left(nPos);
   m_atomApp = ::GlobalAddAtom(strShortName);
   m_atomSystemTopic = ::GlobalAddAtom(_T("system"));

} 

Additional query words: 8.3 filename EnableShellOpen

Keywords : kbcode kbMFC kbVC600bug kbWinOS95 kbGrpMFCATL
Version : winnt:6.0
Platform : winnt
Issue type : kbbug


Last Reviewed: December 7, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.