如何决定在新的c++项目中使用ATL、MFC、Win32还是CLR ?

时间:2022-05-13 12:58:27

I'm just starting my first C++ project. I'm using Visual Studio 2008. It's a single-form Windows application that accesses a couple of databases and initiates a WebSphere MQ transaction. 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.

我刚开始我的第一个c++项目。我用的是Visual Studio 2008。它是一个单表单Windows应用程序,访问两个数据库并启动WebSphere MQ事务。我基本上理解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. So, assuming C++ is the only/best option, which should I choose?

编辑:我之所以选择c++来做这个项目,是因为我在文章中没有提到它,这并不完全是技术性的。那么,假设c++是唯一/最好的选项,我应该选择哪个呢?

5 个解决方案

#1


66  

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 framework),代价是限制你的可执行文件在运行时需要安装。net框架,以及限制你的Windows平台(然而,所有4列出技术只是窗户,所以平台限制可能是最麻烦的)。

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. Doing so gives you many "extras," such as access to the .net libraries, full garbage collection, etc.

然而,CLR要求您使用c++语言的c++ /CLI扩展,因此,在本质上,您需要学习一些额外的语言特性才能使用它。这样做会给你带来很多额外的功能,比如访问。net库、完整的垃圾收集等等。

ATL & MFC are somewhat trickier to decide between. I'd refer you to MSDN's page for choosing in order to decide between them. 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之间的选择有些棘手。我建议你参考MSDN的页面,以便在两者之间做出选择。ATL/MFC的优点是,您不需要. net框架,只需要为部署安装VC/MFC运行时。

Using Win32 directly provides the smallest executables, with the fewest dependencies, but is more work to write. You have the least amount of helper libraries, so you're writing more of the code.

使用Win32直接提供最小的可执行文件,具有最少的依赖性,但是需要编写更多的工作。您拥有最少的帮助库,因此您正在编写更多的代码。

#2


19  

Win32 is the raw, bare-metal way of doing it. It's tedious, difficult to use, and has alot of small details you need to remember otherwise things will fail in relatively mysterious ways.

Win32是最原始、最简单的方法。它很乏味,很难使用,并且有很多你需要记住的小细节,否则事情会以相对神秘的方式失败。

MFC builds upon Win32 to provide you an object oriented way of building your application. It's not a replacement for Win32, but rather an enhancement - it does alot of the hard work for you.

MFC构建在Win32之上,为您提供了一种面向对象的构建应用程序的方式。它不是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. It's by far the easiest to use, but requires the .NET framework, which may or may not be a hindrance in your case.

System.Windows。表单(我假设您是指CLR)是完全不同的,但是与MFC的基本结构有很大的相似之处。到目前为止,它是最容易使用的,但是需要. 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#,因为使用c#更容易)。

#3


13  

As far as C++ goes, I would use 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.

就c++而言,我将使用WTL。它是lightweght,您将几乎没有(如果有的话)依赖项,因此很容易发布和安装。当我的应用程序包含一个在大多数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

#4


8  

I would be very curious as to why you would do this in C++ at all. Based on your brief description, C# sounds like a much more appropriate choice.

我很好奇为什么你要用c++来做这个。根据您的简要描述,c#听起来更合适。

Just to elaborate a bit, look at the link you gave describing the 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++ CLR的链接。评分最高的答案是(准确地说,在我看来)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. 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#.

MFC、ATL等将会被支持,因为你可以在Visual Studio的未来版本上编译你的应用,并在Windows的未来版本上运行它们。但它们不受支持,因为在API或语言中没有很多新的开发,就像在CLR和c#中一样。

#5


4  

There is nothing wrong with 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).

CLR没有问题。和这里的其他人一样,我建议您使用c#,但是由于您有坚持使用c++的理由,那么使用。net框架要比使用ATL/MFC容易几千倍,如果您还不熟悉它们的话。

It may be worth mentioning that if you're using C++/CLR then you're not really using C++ at all. C++/CLR compiles to CIL just like C#. 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. There are other methods of calling native code from .NET which, perhaps, you should explore.

值得一提的是,如果您使用的是c++ /CLR,那么您实际上根本就没有使用c++。c++ /CLR编译到像c#一样。我自己从来没有使用过它,但我相信它的目的是让您能够编译遗留代码,并使新的。net代码更容易使用,而不是让新的代码与旧的c++可执行文件一起工作。从。net调用本机代码还有其他方法,也许您应该研究一下。

#1


66  

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 framework),代价是限制你的可执行文件在运行时需要安装。net框架,以及限制你的Windows平台(然而,所有4列出技术只是窗户,所以平台限制可能是最麻烦的)。

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. Doing so gives you many "extras," such as access to the .net libraries, full garbage collection, etc.

然而,CLR要求您使用c++语言的c++ /CLI扩展,因此,在本质上,您需要学习一些额外的语言特性才能使用它。这样做会给你带来很多额外的功能,比如访问。net库、完整的垃圾收集等等。

ATL & MFC are somewhat trickier to decide between. I'd refer you to MSDN's page for choosing in order to decide between them. 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之间的选择有些棘手。我建议你参考MSDN的页面,以便在两者之间做出选择。ATL/MFC的优点是,您不需要. net框架,只需要为部署安装VC/MFC运行时。

Using Win32 directly provides the smallest executables, with the fewest dependencies, but is more work to write. You have the least amount of helper libraries, so you're writing more of the code.

使用Win32直接提供最小的可执行文件,具有最少的依赖性,但是需要编写更多的工作。您拥有最少的帮助库,因此您正在编写更多的代码。

#2


19  

Win32 is the raw, bare-metal way of doing it. It's tedious, difficult to use, and has alot of small details you need to remember otherwise things will fail in relatively mysterious ways.

Win32是最原始、最简单的方法。它很乏味,很难使用,并且有很多你需要记住的小细节,否则事情会以相对神秘的方式失败。

MFC builds upon Win32 to provide you an object oriented way of building your application. It's not a replacement for Win32, but rather an enhancement - it does alot of the hard work for you.

MFC构建在Win32之上,为您提供了一种面向对象的构建应用程序的方式。它不是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. It's by far the easiest to use, but requires the .NET framework, which may or may not be a hindrance in your case.

System.Windows。表单(我假设您是指CLR)是完全不同的,但是与MFC的基本结构有很大的相似之处。到目前为止,它是最容易使用的,但是需要. 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#,因为使用c#更容易)。

#3


13  

As far as C++ goes, I would use 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.

就c++而言,我将使用WTL。它是lightweght,您将几乎没有(如果有的话)依赖项,因此很容易发布和安装。当我的应用程序包含一个在大多数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

#4


8  

I would be very curious as to why you would do this in C++ at all. Based on your brief description, C# sounds like a much more appropriate choice.

我很好奇为什么你要用c++来做这个。根据您的简要描述,c#听起来更合适。

Just to elaborate a bit, look at the link you gave describing the 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++ CLR的链接。评分最高的答案是(准确地说,在我看来)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. 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#.

MFC、ATL等将会被支持,因为你可以在Visual Studio的未来版本上编译你的应用,并在Windows的未来版本上运行它们。但它们不受支持,因为在API或语言中没有很多新的开发,就像在CLR和c#中一样。

#5


4  

There is nothing wrong with 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).

CLR没有问题。和这里的其他人一样,我建议您使用c#,但是由于您有坚持使用c++的理由,那么使用。net框架要比使用ATL/MFC容易几千倍,如果您还不熟悉它们的话。

It may be worth mentioning that if you're using C++/CLR then you're not really using C++ at all. C++/CLR compiles to CIL just like C#. 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. There are other methods of calling native code from .NET which, perhaps, you should explore.

值得一提的是,如果您使用的是c++ /CLR,那么您实际上根本就没有使用c++。c++ /CLR编译到像c#一样。我自己从来没有使用过它,但我相信它的目的是让您能够编译遗留代码,并使新的。net代码更容易使用,而不是让新的代码与旧的c++可执行文件一起工作。从。net调用本机代码还有其他方法,也许您应该研究一下。