使用C++/使用afxwinforms.h作为另一个C++/CLI DLL也使用afxwinforms.h )添加了另一个C++/CLI DLL( CLILARARIL.DLL >)。

使用C++/使用afxwinforms.h作为另一个C++/CLI DLL也使用afxwinforms.h )添加了另一个C++/CLI DLL( CLILARARIL.DLL >)。,dll,mfc,c++-cli,Dll,Mfc,C++ Cli,两者都包括stdafx.h中的afxwinforms.h 当我尝试编译时,会出现以下错误: error C2011: 'Microsoft::VisualC::MFC::CWin32Window' : 'class' type redefinition error C2011: 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' : 'class' type redefinition 如果我打开选项Reference Assembly Output

两者都包括stdafx.h中的afxwinforms.h

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

error C2011: 'Microsoft::VisualC::MFC::CWin32Window' : 'class' type redefinition
error C2011: 'Microsoft::VisualC::MFC::CWinFormsEventsHelper' : 'class' type redefinition
如果我打开选项Reference Assembly Output并使用“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'.

我怎样才能解决这个错误呢?

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

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


我认为您已经找到了最好的解决方案,使用#include和#pragma注释(disable:4944)关闭编译器。在名称空间中包含头可能是另一种可行的攻击,它重命名了CWin32Window的名称空间,但我认为链接mfcm90.lib时会出现问题。重构解决方案并将所有winforms代码保存在一个项目中是我唯一的建议。

是否使用afxwinforms.h中的类型

如果不是(与我的情况类似),解决方案是注释include并添加这两行:

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#使用
#使用
或者在项目中添加对这两个程序集的引用