简体   繁体   English

是否可以在针对 net6.0-windows 的 C++/CLI 项目中使用 CWinFormsControl/AfxWinForms.h 以及如何使用?

[英]Is it possible to use CWinFormsControl/AfxWinForms.h in a C++/CLI project targeting net6.0-windows and how?

We have a fairly large solution containing a mix of C++/CLI and C# projects using MFC and Winforms controls to build the UI.我们有一个相当大的解决方案,其中包含使用 MFC 和 Winforms 控件构建 UI 的 C++/CLI 和 C# 项目的混合。 Several MFC dialogs in the app make use of a CWinFormsControl from afxwinforms.h to embed WinForms controls inside them, currently everything targets .NET48 and works fine.应用程序中的几个 MFC 对话框使用 afxwinforms.h 中的 CWinFormsControl 将 WinForms 控件嵌入其中,目前一切都针对 .NET48 并且工作正常。 We are trying to update everything to .NET6.0 and everything seems OK except the one project that includes afxwinforms.h.我们正在尝试将所有内容更新到 .NET6.0,除了包含 afxwinforms.h 的一个项目之外,一切似乎都正常。

The project settings are:项目设置如下:
Common Language Runtime Support: .NET Core Runtime Support(/clr:netcore)公共语言运行时支持: .NET Core 运行时支持(/clr:netcore)
.NET Core Target Framework: net6.0-windows (I have tried just net6.0, net5.0...) .NET Core 目标框架: net6.0-windows(我只尝试过 net6.0、net5.0...)
Platform Toolset: Visual Studio 2022 (v143)平台工具集: Visual Studio 2022 (v143)

We include <afxwinforms.h> amongst several other afx includes in a precompiled header and the compiler gives this error building our stdafx.h:我们在预编译的头文件中包含 <afxwinforms.h> 以及其他几个 afx 包含,编译器在构建我们的 stdafx.h 时会出现此错误:

C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.h(30,8): fatal error C1107: could not find assembly 'System.Windows.Forms.dll': please specify the assembly search path using /AI or by setting the LIBPATH environment variable 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 环境变量指定程序集搜索路径

From some searching on the internet I found and attempted to pass the following directories using the Additional #using Directories property of the project:从互联网上的一些搜索中,我发现并尝试使用项目的Additional #using Directories属性传递以下目录:
C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0\ 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\packs\Microsoft.WindowsDesktop.App.Ref\6.0.0\ref\net6.0\
C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0\ C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0\

Any of those paths seems to sort out the above compiler error, and intellisense seems to recognise everything in afxwinforms.h too.这些路径中的任何一个似乎都可以解决上述编译器错误,并且智能感知似乎也可以识别 afxwinforms.h 中的所有内容。 However, the compiler starts giving warnings such as:但是,编译器开始发出警告,例如:

C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\um\objidl.h(9724): message : see declaration of 'IPersist' 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): message : see declaration of 'IPersistStorage' 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): message : This diagnostic occurred while importing type 'System::Windows::Forms::Control ' from assembly 'System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 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,Version=6.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'。
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): warning C4691: 'IQuickActivate': type referenced was expected in unreferenced module 'System.Windows.Forms.Primitives', type defined in current translation unit used instead C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): 警告 C4691: 'IQuickActivate': type referenced was expected in unreferenced模块“System.Windows.Forms.Primitives”,在当前使用的翻译单元中定义的类型
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): message : This diagnostic occurred while importing type 'System::Windows::Forms::Control ' from assembly 'System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3):消息:此诊断发生在导入类型 'System::Windows ::Forms::Control 来自程序集 'System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'。
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): warning C4691: 'IOleInPlaceUIWindow': type referenced was expected in unreferenced module 'System.Windows.Forms.Primitives', type defined in current translation unit used instead C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): 警告 C4691: 'IOleInPlaceUIWindow': type referenced was expected in unreferenced模块“System.Windows.Forms.Primitives”,在当前使用的翻译单元中定义的类型
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): message : This diagnostic occurred while importing type 'System::Windows::Forms::Control ' from assembly 'System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3):消息:此诊断发生在导入类型 'System::Windows ::Forms::Control 来自程序集 'System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'。

before giving this error:在给出此错误之前:

C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3): fatal error C1001: Internal compiler error. C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\atlmfc\include\afxwinforms.inl(32,3):致命错误 C1001:内部编译器错误。 (compiler file 'd:\a01_work\20\s\src\vctools\Compiler\CxxFE\sl\p1\c\cpimport.cpp', line 12502) To work around this problem, try simplifying or changing the program near the locations listed above. (编译器文件 'd:\a01_work\20\s\src\vctools\Compiler\CxxFE\sl\p1\c\cpimport.cpp',第 12502 行)要解决此问题,请尝试简化或更改位置附近的程序以上所列。

So, my main questions are:所以,我的主要问题是:
Is it possible to do this, can I embed a WinForms control in an MFC dialog using a CWinFormsControl in .NET6.0?是否可以这样做,我可以在 .NET6.0 中使用 CWinFormsControl 将 WinForms 控件嵌入到 MFC 对话框中吗?
If it is possible, where am I going wrong causing the problems above?如果可能的话,我在哪里出错导致上述问题?
If it isn't possible, is there an alternative way to do something like this: (trimmed down sample)如果不可能,是否有另一种方法可以做这样的事情:(修剪样本)

//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;
    }
};

I can reproduce this in a quick, small sample.我可以在一个快速的小样本中重现这一点。
In Visual Studio 2022 create a new project, CLR Empty Project (.NET)在 Visual Studio 2022 中创建一个新项目 CLR Empty Project (.NET)
Add a header and source file, include the header from the source.添加头文件和源文件,包括源头文件。 #include <afxwinforms.h> to the header file. #include <afxwinforms.h> 到头文件。
Project->Properties->Configuration Properties->Advanced:项目->属性->配置属性->高级:
Use of MFC: Use MFC in a shared DLL MFC的使用:在共享DLL中使用MFC
Common Language Runtime Support: (should already be) .NET Core Runtime Support (/clr:netcore)公共语言运行时支持:(应该已经).NET Core 运行时支持 (/clr:netcore)
.NET Core Target Framework: (should already be) net6.0 .NET Core 目标框架:(应该已经是)net6.0

Build the project, you should get an error about not being able to find the System.Windows.Forms.dll.构建项目,你应该得到一个关于找不到 System.Windows.Forms.dll 的错误。

Project->properties->Configuration Properties -> C/C++ -> General: Additional #using Directories:项目->属性->配置属性-> C/C++->常规:附加#using目录:
C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0\ OR 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\packs\Microsoft.WindowsDesktop.App.Ref\6.0.0\ref\net6.0\ 或
C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0<br /> C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0<br />

Build again, you should get the internal compiler error.再次构建,您应该得到内部编译器错误。

i was trying to solve the same issue.我试图解决同样的问题。 From the code inside mfc its clear they only support .net framework for now.从 mfc 中的代码可以看出,它们目前仅支持 .net 框架。

What worked for me was to make a copy of the afxwinforms.h and .inl plus some files from "C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.32.31114\atlmfc\src\mfcm" into my project directly so it stops linking into the microsoft mfcmXXX.lib and just adjusted the files slightly to get it to compile.对我有用的是复制 afxwinforms.h 和 .inl 以及“C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.32.31114\atlmfc\src\ 中的一些文件” mfcm”直接进入我的项目,因此它停止链接到 microsoft mfcmXXX.lib 并稍微调整文件以使其编译。

Here is how I fix it:这是我修复它的方法:

  1. Use Visual Studio 2022, in project.vcxproj, add this reference:使用 Visual Studio 2022,在 project.vcxproj 中,添加此引用:
    <ItemGroup><FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" /></ItemGroup>
  1. Then, you will see "System.Windows.Forms.dll" in "External Dependencies" of Solution Explorer.然后,您将在解决方案资源管理器的“外部依赖项”中看到“System.Windows.Forms.dll”。 Look at the properties for this dll, which is under: C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.7\ref\net6.0\System.Windows.dll查看此 dll 的属性,该属性位于:C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.7\ref\net6.0\System.Windows.dll
  2. open project's property, Configuration Properties -> C/C++ -> General -> Additional #Using Directories, add the folder's full path there.打开项目的属性,Configuration Properties -> C/C++ -> General -> Additional #Using Directories,在此处添加文件夹的完整路径。 enter image description here (I just use net6.0, not net6.0-window)在此处输入图像描述(我只使用 net6.0,而不是 net6.0-window)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以构建面向 Linux 和 Windows 的 .Net Core C++/CLI 应用程序 - Is it possible to build a .Net Core C++/CLI application targeting Linux and Windows 无法在Visual Studio 2012中针对.NET 3.5的C#项目中使用CLI类库中的CLR类型,并使用Windows7.1SDK工具集进行编译 - Unable to use a CLR type from CLI class library in a C# project in Visual Studio 2012 targeting .NET 3.5 and compiling with Windows7.1SDK toolset 仅在 Windows 上:net5.0-windows(或 net6.0-windows)是否允许我在 .Net 5(或 .Net 6)中重新编译 .Net 框架? - On Windows only: does net5.0-windows (or net6.0-windows) allow me to recompile .Net framework in .Net 5 ( or .Net 6)? C ++ / CLI项目如何在控制台窗口中显示消息? - How can C++/CLI project display message in console windows? 是否可以在面向Windows Phone 8和.NET for Windows Store的可移植类库中使用System.Windows.Visibility? - Is it possible to use System.Windows.Visibility in a portable class library targeting Windows Phone 8 and .NET for Windows Store? 如何在旧的 ASP.NET 网站(不是网站项目)中使用 C# 6.0 或 7.0 - How to use C# 6.0 or 7.0 in an old ASP.NET Website (not Website Project) 是否可以在 C# 中使用 C++/CLI 类中定义的实例? - Is it possible to use the instance defined in C++/CLI class in C#? Windows Azure未找到C ++ / CLI项目的DLL - Windows Azure not finding DLL of C++/CLI project 如何在.NET应用程序中使用C ++项目? - How to use a C++ project from within a .NET application? .NET 6.0 C#“新控制台模板”- 如何阅读 CLI arguments? - .NET 6.0 C# "new console template" - how to read CLI arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM