【问题标题】:Use a C++/CLI DLL using afxwinforms.h as Reference of another C++/CLI DLL also using afxwinforms.h使用使用 afxwinforms.h 的 C++/CLI DLL 作为另一个也使用 afxwinforms.h 的 C++/CLI DLL 的参考
【发布时间】:2013-03-27 11:42:00
【问题描述】:

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

两者都在 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 并将 #using "CliLibrary.dll" 添加到正在使用的 .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'.

我该如何解决这个错误?

【问题讨论】:

    标签: dll mfc c++-cli


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-25
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 2010-10-20
        相关资源
        最近更新 更多