3

在 Visual C++ 2010 中,我添加了从 C++/CLI DLL ( ControlWrapper.dll ) 到另一个 C++/CLI DLL ( CliLibrary.dll ) 的引用。

两者都在 stdafx.h 中包含 afxwinforms.h。

当我尝试编译时,出现以下错误:

error C2011: 'Microsoft::VisualC::MFC::CWin32Window' : 'class' type redefinition
error C2011: 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' : 'class' type redefinition

如果我打开 Option Reference Assembly Output并添加#using "CliLibrary.dll"到 using .cpp 文件中,我会收到以下警告:

1>ControlWrapper.dll : warning C4944: 'CWin32Window' : cannot import symbol from 'c:\dev\trunk\CliLibrary.dll': as 'Microsoft::VisualC::MFC::CWin32Window' already exists in the current scope
1>     C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwinforms.h(83) : see declaration of 'Microsoft::VisualC::MFC::CWin32Window'
1>          This diagnostic occurred while importing type 'Microsoft.VisualC.MFC.CWin32Window' from assembly 'CliLibrary, Version=1.0.4843.17337, Culture=neutral, PublicKeyToken=null'.
1>ControlWrapper.dll : warning C4944: 'CWinFormsEventsHelper' : cannot import symbol from 'c:\dev\sfirm\trunk\sfclrlib\debug\sfclrlib.dll': as 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' already exists in the current scope
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\afxwinforms.h(122) : see declaration of 'Microsoft::VisualC::MFC::CWinFormsEventsHelper'
1>          This diagnostic occurred while importing type 'Microsoft.VisualC.MFC.CWinFormsEventsHelper' from assembly 'CliLibrary, Version=1.0.4843.17337, Culture=neutral, PublicKeyToken=null'.

我该如何解决这个错误?

4

2 回答 2

2

嗯,这是一个痛苦的问题。它当然解释了为什么你是我遇到的第一个真正使用它的程序员。问题是由 afxwinforms.h 中的此声明引起的:

public ref class CWin32Window : public System::Windows::Forms::IWin32Window
// etc..

public关键字是杀手,它将类添加到程序集的清单中。因此,当您在另一个也包含标头的项目中引用它时,该类有两个定义。该标头中本机类和托管类的混合阻止了干净的解决方案。

我认为您已经找到了最好的解决方案,使用#include 和#pragma comment(disable:4944) 来关闭编译器。在命名空间中包含标头可能是另一个可行的技巧,它重命名了 CWin32Window 的命名空间,但我预计在链接 mfcm90.lib 时会遇到麻烦。重构您的解决方案并将所有 winforms 代码保留在一个项目中是我唯一可以推荐的方法。

于 2013-04-05T09:46:44.680 回答
0

您是否使用 afxwinforms.h 中的类型?

如果不是(就像我的情况一样),解决方案是注释包含并添加这两行:

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

或在您的项目中添加对这两个程序集的引用。

于 2013-05-22T11:10:50.217 回答