简体   繁体   English

我如何决定对新的 C++ 项目使用 ATL、MFC、Win32 还是 CLR?

[英]How do I decide whether to use ATL, MFC, Win32 or CLR for a new C++ project?

I'm just starting my first C++ project.我刚刚开始我的第一个 C++ 项目。 I'm using Visual Studio 2008 .我正在使用Visual Studio 2008 It's a single-form Windows application that accesses a couple of databases and initiates a WebSphere MQ transaction.它是一个单一形式的 Windows 应用程序,它访问几个数据库并启动一个 WebSphere MQ 事务。 I basically understand the differences among ATL, MFC, Win32 (I'm a little hazy on that one actually) and CLR, but I'm at a loss as to how I should choose.我基本上了解 ATL、MFC、Win32(实际上我对那个有点模糊)和 CLR 之间的区别,但我不知道应该如何选择。

Is one or more of these just there for backward-compatibility?其中一个或多个是否只是为了向后兼容?

Is CLR a bad idea ? CLR是个坏主意吗?

Any suggestions appreciated.任何建议表示赞赏。

Edit: I've chosen C++ for this project for reasons I didn't go into in the post, which are not entirely technical.编辑:由于我没有在帖子中讨论的原因,我为这个项目选择了 C++,这不完全是技术性的。 So, assuming C++ is the only/best option, which should I choose?那么,假设C++ 是唯一/最好的选择,我应该选择哪个?

It depends on your needs. 这取决于您的需求。

Using the CLR will provide you with the most expressive set of libraries (the entire .NET framework), at the cost of restricting your executable to requiring the .NET framework to be installed at runtime, as well as limiting you to the Windows platform (however, all 4 listed technologies are windows only, so the platform limitation is probably the least troublesome). 使用CLR将为您提供最具表现力的库集(整个.NET框架),代价是将可执行文件限制为要求在运行时安装.NET框架,以及限制您使用Windows平台(但是,所有4种列出的技术都只是Windows,因此平台限制可能是最不麻烦的)。

However, CLR requires you to use the C++/CLI extensions to the C++ language, so you'll, in essense, need to learn some extra language features in order to use this. 但是,CLR要求您使用C ++语言的C ++ / CLI扩展,因此您需要学习一些额外的语言功能才能使用它。 Doing so gives you many "extras," such as access to the .net libraries, full garbage collection, etc. 这样做会为您提供许多“附加功能”,例如访问.net库,完全垃圾回收等。

ATL & MFC are somewhat trickier to decide between. ATL和MFC之间的决定有点棘手。 I'd refer you to MSDN's page for choosing in order to decide between them. 我会引用您的MSDN页面进行选择 ,以便在它们之间做出决定。 The nice thing about ATL/MFC is that you don't need the .NET framework, only the VC/MFC runtimes to be installed for deployment. ATL / MFC的优点在于您不需要.NET框架,只需要安装VC / MFC运行时进行部署。

Using Win32 directly provides the smallest executables, with the fewest dependencies, but is more work to write. 使用Win32直接提供最小的可执行文件,具有最少的依赖项,但更多的工作要写。 You have the least amount of helper libraries, so you're writing more of the code. 您拥有最少量的帮助程序库,因此您正在编写更多代码。

Win32 is the raw, bare-metal way of doing it. Win32是原始的,裸机的方式。 It's tedious, difficult to use, and has a lot of small details you need to remember otherwise things will fail in relatively mysterious ways. 这很乏味,难以使用,并且需要记住许多小细节,否则事情会以相对神秘的方式失败。

MFC builds upon Win32 to provide you an object-oriented way of building your application. MFC构建在Win32之上,为您提供面向对象的构建应用程序的方法。 It's not a replacement for Win32, but rather an enhancement - it does a lot of the hard work for you. 它不是Win32的替代品,而是一种增强功能 - 它为您做了很多艰苦的工作。

System.Windows.Forms (which is what I assume you meant by CLR) is completely different but has large similarities to MFC from its basic structure. System.Windows.Forms(我认为你的意思是CLR)是完全不同的,但它与MFC的基本结构有很大的相似之处。 It's by far the easiest to use but requires the .NET framework, which may or may not be a hindrance in your case. 它是迄今为止最容易使用的,但需要.NET框架,这可能会或可能不会成为您的一个障碍。

My recommendation: If you need to avoid .NET, then use MFC, otherwise use .NET (in fact, in that case, I'd use C# as it's much easier to work with). 我的建议:如果你需要避免使用.NET,那么使用MFC,否则使用.NET(事实上,在这种情况下,我会使用C#,因为它更容易使用)。

As far as C++ goes, I would use WTL. 就C ++而言,我会使用WTL。 It's lightweght and you will have few (if any) dependencies, making it easy to ship and install. 这是轻微的,您将很少(如果有)依赖,使其易于发货和安装。 I find it very satisfying when my app consists of a single EXE that will run on most versions of Windows, but this may not be a concern to you. 当我的应用程序包含一个可在大多数Windows版本上运行的EXE时,我发现它非常令人满意,但这可能不是您的担忧。

If you choose to go .NET instead, then C# is almost certainly the way to go. 如果您选择使用.NET,那么C#几乎肯定是要走的路。

More in WTL here: 更多WTL在这里:

http://www.codeproject.com/KB/wtl/wtl4mfc1.aspx http://www.codeproject.com/KB/wtl/wtl4mfc1.aspx

I would be very curious as to why you would do this in C++ at all. 我很好奇你为什么要在C ++中这样做。 Based on your brief description, C# sounds like a much more appropriate choice. 根据您的简要描述,C#听起来更合适。

Just to elaborate a bit, look at the link you gave describing the C++ CLR. 只是详细说明一下,看看你给出的描述C ++ CLR的链接。 The top rated answer notes (accurately, in my opinion) that C++ is appropriate for "kernel, games, high-performance and server apps" - none of which seems to describe what you're doing. 评价最高的答案(准确地说,在我看来)C ++适用于“内核,游戏,高性能和服务器应用程序” - 这些都不能描述你正在做的事情。

MFC, ATL, etc are going to be supported in the sense that, yes you'll be able to compile your app on future versions of Visual Studio and run them on future versions of Windows. 从某种意义上说,MFC,ATL等将得到支持,是的,您将能够在未来版本的Visual Studio上编译您的应用程序,并在未来版本的Windows上运行它们。 But they're not supported in the sense that there's not a lot of new development going on in the API or the language the same way there is in the CLR and C#. 但是,它们不受支持,因为API或语言中没有很多新的开发与CLR和C#中的相同。

There is nothing wrong with CLR. CLR没有任何问题。 Like others here I'd suggest C# but as you have reasons for sticking with C++ then using the .NET framework is several thousand times easier than messing with ATL/MFC if you're not already familiar with them (IMO). 和其他人一样,我建议使用C#但是因为你有理由坚持使用C ++,那么使用.NET框架比使用ATL / MFC更容易几千倍,如果你还不熟悉它们(IMO)。

It may be worth mentioning that if you're using C++/CLR then you're not really using C++ at all. 值得一提的是,如果您使用的是C ++ / CLR,那么您根本就不会使用C ++。 C++/CLR compiles to CIL just like C#. C ++ / CLR就像C#一样编译成CIL。 I've never used it myself but I believe its purpose is to allow you to compile legacy code and make it easily available to new .NET code rather than allow new code work with old C++ executables. 我自己从未使用它,但我相信它的目的是允许您编译遗留代码并使其易于用于新的.NET代码,而不是允许新代码使用旧的C ++可执行文件。 There are other methods of calling native code from .NET which, perhaps, you should explore. 还有其他从.NET调用本机代码的方法,也许你应该探索。

The modern (2021) answer to this question would appear to be to use C++/WinRT instead of C++/CLR (or C++/CLI or C++/CX... jeez Microsoft):这个问题的现代 (2021) 答案似乎是使用 C++/WinRT 而不是 C++/CLR(或 C++/CLI 或 C++/CX... jeez Microsoft):

https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/intro-to-using-cpp-with-winrt https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/intro-to-using-cpp-with-winrt

C++/WinRT is an entirely standard modern C++17 language projection for Windows Runtime (WinRT) APIs, implemented as a header-file-based library, and designed to provide you with first-class access to the modern Windows API. C++/WinRT 是 Windows 运行时 (WinRT) API 的完全标准的现代 C++17 语言投影,作为基于头文件的库实现,旨在为您提供对现代 Windows API 的一流访问。 With C++/WinRT, you can author and consume Windows Runtime APIs using any standards-compliant C++17 compiler.借助 C++/WinRT,您可以使用任何符合标准的 C++17 编译器创作和使用 Windows 运行时 API。

... ...

C++/WinRT is Microsoft's recommended replacement for the C++/CX language projection C++/WinRT 是 Microsoft 推荐的 C++/CX 语言投影替代品

It's basically standard C++ but the UI is defined with XAML.它基本上是标准的 C++,但 UI 是用 XAML 定义的。

Still though, as with the other answers, it would appear that using C# is really microsoft's favorite approach.尽管如此,与其他答案一样,使用 C# 似乎确实是微软最喜欢的方法。 C++/WinRT really looks like it's almost C# anyways.无论如何,C++/WinRT 看起来真的几乎是 C#。

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

相关问题 如何在 Win32 C++ 项目中使用 _malloca 而不是 _alloca? - How do I use _malloca instead of _alloca in Win32 C++ project? 如何在 Win32 应用程序中通过 C++/MFC 在鼠标 cursor 上显示动态文本 - How do I display dynamic text at the mouse cursor via C++/MFC in a Win32 application C++ CLR win32 还是其他? - C++ CLR win32 or others? 如何在Visual C ++ 2008中将MFC应用程序项目添加到Win32应用程序项目 - How to add MFC application project to Win32 application project in Visual C++ 2008 How to export a function of a regular MFC DLL that is statically linked to MFC to use with a non MFC Win32 Console App in C++ - How to export a function of a regular MFC DLL that is statically linked to MFC to use with a non MFC Win32 Console App in C++ 如何在C ++ Win32应用程序中使用非托管DLL? - How do I use unmanaged DLL in C++ win32 application? 是Visual C ++ Win32还是MFC C ++? - Visual C++ Win32 or MFC C++? C ++,MFC,选项卡式文档界面应用程序,Win32 - C++, MFC, tabbed document interface application, Win32 使用C ++的Win32 / MFC的命名约定 - Naming convention for Win32/MFC with C++ 在旧的MFC / Win32 DLL中实现CLR并将DLL托管到MFC / Win32应用程序中 - Implementing the CLR into old MFC/Win32 DLL and hosting DLL into MFC/Win32 Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM