FIX: Using CString or Collection Class Links in CWinApp Code

Last reviewed: September 16, 1997
Article ID: Q107436
1.00 WINDOWS kbprg kbfixlist kbbuglist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, version 1.0
    

SYMPTOMS

Using a CString object or collection object (CObList, CStringArray, and so forth) in an application forces the code for CWinApp to be linked into the application whether or not a CWinApp object is defined. The CWinApp code requires that SHELL.LIB and COMMDLG.LIB be specified in the link options.

If you are not linking with SHELL.LIB and COMMDLG.LIB, and you use a CString or collection object without a CWinApp object, you will receive the following linker errors:

   error L2029: 'GETFILETITLE' : unresolved external
   error L2029: 'REGSETVALUE' : unresolved external
   error L2029: 'REGQUERYVALUE' : unresolved external
   error L2029: 'DRAGFINISH' : unresolved external
   error L2029: 'DRAGQUERYFILE' :unresolved external

CAUSE

The problem described above is caused by a compiler/linker bug. To understand the problem, consider the code:

   extern int somevariable;
   __inline void func(){ somevariable=1;}
   void main(){ return;}

If the code above is compiled with "/c /AM /Z7" or with "/c /f- /AL" and then linked, the unresolved external linker error (L2029) occurs. The linker is searching for the "somevariable" variable even though the func() function isn't referenced and tries to link it into the application.

The Microsoft Foundation Class (MFC) libraries do something similar in the AFXWIN.INL file located in the \MSVC\MFC\INCLUDE directory. The code is as follows:

   _AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()
     { return afxCurrentWinApp; }
   _AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle()
        { ASSERT(afxCurrentInstanceHandle != NULL);
          return afxCurrentInstanceHandle; }
   _AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
     { ASSERT(afxCurrentResourceHandle != NULL);
       return afxCurrentResourceHandle; }
   _AFXWIN_INLINE void AFXAPI AfxSetResourceHandle(HINSTANCE hInstResource)
     { ASSERT(hInstResource != NULL); afxCurrentResourceHandle =
       hInstResource; }
  _AFXWIN_INLINE const char* AFXAPI AfxGetAppName()
     { ASSERT(afxCurrentAppName != NULL); return afxCurrentAppName; }

Therefore, modules compiling with AFXWIN.INL can force the CWinApp code to be linked into an application.

RESOLUTION

To work around this problem, do one of the following:

  • Link with SHELL.LIB and COMMDLG.LIB and ignore the fact that some extra code will be linked into your application.

    -or-

  • Migrate your application to MFC by using a CWinApp object.

STATUS

Microsoft has confirmed this to be a problem with Microsoft Foundation Classes version 2.0. This problem has been corrected in version 2.5 of the Microsoft Foundation Classes, which is included with Visual C++ version 1.5.

MORE INFORMATION

To reproduce the problem, compile and link the sample program below:

Sample Code

/* Compile options needed:  /AM /GA
* Link options needed: link without SHELL.LIB and COMMDLG.LIB
*               link with MAFXCW.LIB
*/

#include <afxwin.h>
void main(void)
{
        CString func("hello");
}

Additional Reference Words: 1.00 2.00

	
	


KBCategory: kbprg kbfixlist kbbuglist kbfasttip
KBSubcategory: MfcMisc
Keywords : kb16bitonly MfcMisc kbbuglist kbfasttip kbfixlist kbprg
Technology : kbMfc
Version : 1.00
Platform : 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 16, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.