FIX: CFile::GetFileName() Always Returns CString w/Zero Length

Last reviewed: September 19, 1997
Article ID: Q143086
4.00 WINDOWS NT kbprg kbbuglist kbfixlist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, version 4.0

SYMPTOMS

If you call CFile::GetFileName(), it will return a CString with the correct file name. However, if you call GetLength() on the CString, it will return 0.

CAUSE

The MFC function CFile::GetFileName() writes directly to the CString's character pointer. However, it doesn't set the CString's size before returning from this function.

RESOLUTION

Call CString::ReleaseBuffer() after calling CFile::GetFileName().

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ 4.1.

MORE INFORMATION

Sample Code

void CTestDoc::Serialize(CArchive& ar)
{
   if (ar.IsStoring())
   {
      // TODO: add storing code here
   }
   else
   {
      CFile* fp = ar.GetFile ();
      ASSERT (fp);
      CString fname = fp->GetFileName ();
      // this line is needed to set the size of CString
      fname.ReleaseBuffer ();
      int nLength = fname.GetLength();
   }
}


Additional reference words: 4.00 4.10 GetFileName GetLength CFile CString
KBCategory: kbprg kbbuglist kbfixlist
KBSubcategory: MfcUI vcbuglist400 vcfixlist410
Keywords : MfcUI vcbuglist400 vcfixlist410 kbbuglist kbfixlist kbprg
Technology : kbMfc
Version : 4.00
Platform : NT WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 19, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.