Microsoft KB Archive/309801

= You may receive an LNK2019 error message when you build a Visual C++ 2005 or Visual C++ .NET application that uses a CString-derived class from a DLL file =

Article ID: 309801

Article Last Modified on 1/9/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q309801



Note Microsoft Visual C++ 2005, Microsoft Visual C++ .NET 2003, and Microsoft Visual C++ .NET 2002 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.



SYMPTOMS
When you build a Visual C++ 2005 or Visual C++ .NET application that uses a CString-derived class from a DLL file, you may receive an error message that is similar to one of the following:

Message 1

ClientProject error LNK2019: unresolved external symbol &quot;__declspec(dllimport) public: __thiscall ATL::CStringT>::CStringT>(char const *)&quot; (__imp_??0?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@QAE@PBD@Z) referenced in function _main

Message 2

ClientProject error LNK2005: &quot;public: __thiscall ATL::CStringT>::~CStringT>(void)&quot; (??1?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@QAE@XZ) already defined in Simple.obj

Message 3

ClientProject fatal error LNK1169: one or more multiply defined symbols found



CAUSE
This behavior may occur if you use a class derived from CString that is also exported from a DLL.

In Microsoft Visual Studio 2005 or in Microsoft Visual Studio .NET, the CString class has changed to a template class, as demonstrated by the following lines of code taken from afxstr.h: typedef ATL::CStringT< wchar_t, StrTraitMFC< wchar_t > > CStringW; typedef ATL::CStringT< char, StrTraitMFC > CStringA; typedef ATL::CStringT< TCHAR, StrTraitMFC< TCHAR > > CString; Visual Studio automatically exports the appropriate template instantiation for the CStringT template class that you derive from. However, when you import the class, Visual Studio .NET sometimes does not correctly import that instantiation. This results in the linker error messages listed in the &quot;Symptoms&quot; section of this article.



RESOLUTION
To resolve this issue, explicitly import the template class for CStringT and CSimpleStringT in the precompiled header (stdafx.h) file, as follows: template class __declspec(dllimport) CStringT > >; template class __declspec(dllimport) CSimpleStringT;



Steps to Reproduce the Problem
 In Visual C++ 2005 or in Visual C++ .NET, create a default MFC DLL project. On the Project menu, click Add New Item.</li> Name the file MyString.h. Select the Header File template, and then click Open.

Note In Visual Studio 2005, click Add instead of Open.</li>  Add the following code to MyString.h:
 * 1) ifdef _USRDLL
 * 2) define IMPEXP __declspec(dllexport)
 * 3) else
 * 4) define IMPEXP __declspec(dllimport)
 * 5) endif

class IMPEXP CMyString: public CString {  public: CMyString; virtual ~CMyString;

void MyMethod; };                   </li> Add a new C++ source file to the project named MyString.cpp.</li>  Add the following code to MyString.cpp:
 * 1) include &quot;StdAfx.h&quot;
 * 2) include &quot;mystring.h&quot;

CMyString::CMyString(void) {} CMyString::~CMyString(void) {}

void CMyString::MyMethod(void) {  AfxMessageBox(&quot;foo is called&quot;); //In Visual C++ 2005, you should rewrite this setence to &quot;AfxMessageBox(L&quot;foo is called&quot;);&quot; }                   </li> Save and build the DLL project.</li> Create a new Win32 Project. In the wizard, change the setting under Application Settings to the Console Application type, and then click to select the MFC checkbox to add MFC support.</li> In the new application's main source file, include MyString.h from the DLL project. Verify that the correct full path or relative path is being used.</li>  Before the main function, add a #pragma to link to the library for the DLL (again, be sure to use a correct full path or relative path), as follows: </li>  Add the following code in the else block of the main function: // TODO: code your application's behavior here. CString strHello = &quot;Hello&quot;; CMyString* myStr = new CMyString; myStr->MyMethod; Note In Visual C++ 2005, you must add the common language runtime support compiler option (/clr:oldSyntax) to successfully compile the previous code sample. To add the common language runtime support compiler option, follow these steps: <ol> Click Project, and then click  Properties.
 * 1) pragma comment(lib, &quot;..\\Debug\\TestDll.lib&quot;)

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> In the right pane, click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project settings.</li> Click Apply, and then click OK.</li></ol>

For more information about common language runtime support compiler options, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> Build the project.

The build fails with the &quot;LNK2019&quot; error.</li> In the console application, add a new C++ source file named Simple.cpp.</li>  Add the following code to Simple.cpp: CString bar {  CString s = &quot;Hi&quot;; return s; } </li> <li>Build the project again.
 * 1) include &quot;stdafx.h&quot;

You receive the &quot;LNK2005&quot; and &quot;LNK1169&quot; error messages.</li> <li>Apply the solution provided in the &quot;Resolution&quot; section of this article.

The project should now build without errors.</li></ol>

Keywords: kbdll kberrmsg kbprb KB309801

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.