【发布时间】:2022-03-23 21:03:13
【问题描述】:
我们有一个相当大的解决方案,其中包含使用 MFC 和 Winforms 控件构建 UI 的 C++/CLI 和 C# 项目的混合。应用程序中的几个 MFC 对话框使用 afxwinforms.h 中的 CWinFormsControl 将 WinForms 控件嵌入其中,目前一切都针对 .NET48 并且工作正常。我们正在尝试将所有内容更新到 .NET6.0,除了包含 afxwinforms.h 的一个项目之外,一切似乎都正常。
项目设置为:
公共语言运行时支持:.NET Core 运行时支持(/clr:netcore)
.NET Core 目标框架:net6.0-windows(我只尝试过 net6.0、net5.0...)
平台工具集:Visual Studio 2022 (v143)
我们将
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.h(30,8): 致命错误 C1107:找不到程序集“System.Windows.Forms.dll”: 请使用 /AI 或通过设置 LIBPATH 环境变量
通过互联网上的一些搜索,我发现并尝试使用项目的 Additional #using Directories 属性传递以下目录:
C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0\
C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\6.0.0\ref\net6.0\
C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0\
这些路径中的任何一个似乎都可以解决上述编译器错误,并且智能感知似乎也可以识别 afxwinforms.h 中的所有内容。但是,编译器开始发出警告,例如:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\um\objidl.h(9724):消息:见 'IPersist'
C:\Program Files (x86)\Windows 的声明 Kits\10\Include\10.0.22000.0\um\objidl.h(11125):消息:见 'IPersistStorage'
C:\Program Files\Microsoft Visual 的声明 Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32): 消息:导入类型时发生此诊断 'System::Windows::Forms::Control' 来自程序集 'System.Windows.Forms,版本=6.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089'。
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): 警告 C4691:“IQuickActivate”:引用的类型应在 未引用的模块“System.Windows.Forms.Primitives”,类型定义在 当前使用的翻译单元
C:\Program Files\Microsoft 视觉的 Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): 消息:导入类型时发生此诊断 'System::Windows::Forms::Control' 来自程序集 'System.Windows.Forms,版本=6.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089'。
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): 警告 C4691:“IOleInPlaceUIWindow”:引用的类型应在 未引用的模块“System.Windows.Forms.Primitives”,类型定义在 当前使用的翻译单元
C:\Program Files\Microsoft 视觉的 Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): 消息:导入类型时发生此诊断 'System::Windows::Forms::Control' 来自程序集 'System.Windows.Forms,版本=6.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089'。
在给出这个错误之前:
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): 致命错误 C1001:内部编译器错误。 (编译文件 'd:\a01_work\20\s\src\vctools\Compiler\CxxFE\sl\p1\c\cpimport.cpp', 第 12502 行)要解决此问题,请尝试简化或更改 上面列出的位置附近的程序。
所以,我的主要问题是:
是否可以这样做,我可以在 .NET6.0 中使用 CWinFormsControl 将 WinForms 控件嵌入到 MFC 对话框中吗?
如果可能的话,我在哪里出错导致上述问题?
如果不可能,是否有另一种方法可以做这样的事情:(修剪样本)
//C#
public class MyUserControl : System.Windows.Forms.UserControl
{
}
//C++
#include <afxwinforms.h>
class MyWnd : public CWnd
{
public:
CWinFormsControl<MyUserControl> mMyControl;
int OnCreate(LPCREATESTRUCT lpCreateStruct)
{
auto rc = __super::OnCreate(lpCreateStruct);
if (rc >= 0)
{
CRect rcChild(0, 0, lpCreateStruct->cx, lpCreateStruct->cy);
m_optionsControl.CreateManagedControl(WS_CHILD | WS_VISIBLE, rcChild, this, 1);
}
return rc;
}
};
我可以在一个快速的小样本中重现这一点。
在 Visual Studio 2022 中创建一个新项目 CLR Empty Project (.NET)
添加头文件和源文件,包括源头文件。 #include
项目->属性->配置属性->高级:
MFC 的使用:在共享 DLL 中使用 MFC
公共语言运行时支持:(应该已经).NET Core 运行时支持 (/clr:netcore)
.NET Core 目标框架:(应该已经是)net6.0
构建项目,你应该得到一个关于找不到 System.Windows.Forms.dll 的错误。
Project->properties->Configuration Properties -> C/C++ -> General: Additional #using Directories:
C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0\ 或
C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\6.0.0\ref\net6.0\ OR
C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0
再次编译,你应该得到内部编译器错误。
【问题讨论】:
标签: c# winforms mfc c++-cli .net-6.0