Microsoft KB Archive/107436

{|
 * width="100%"|

FIX: Using CString or Collection Class Links in CWinApp Code

 * }

Q107436

-

The information in this article applies to:


 * The Microsoft Foundation Classes (MFC), included with:
 * Microsoft Visual C++, 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.
 * 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

void main(void) {       CString func("hello"); } Additional Reference Words: 1.00 2.00
 * 1) include 

Additional query words:

Keywords : kb16bitonly kbnokeyword kbMFC kbVC

Issue type :

Technology : kbAudDeveloper kbMFC