FIX: Using CString or Collection Class Links in CWinApp CodeID: Q107436
|
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
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.
_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.
To work around this problem, do one of the following:
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.
To reproduce the problem, compile and link the sample program below:
/* 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
Additional query words:
Keywords : kb16bitonly kbnokeyword kbMFC kbVC
Version : 1.00
Platform : WINDOWS
Issue type :
Last Reviewed: August 3, 1999