windows Win32 与 .Net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1127546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Win32 vs .Net
提问by florin.bunau
Is .NET better than Win32 or the othe way around? Which would be the pros \ cons of both, in what situations one would be better than the other. Has Microsoft released .Net as a replacement for Win32?
.NET 是否比 Win32 或其他方式更好?这将是两者的优点\缺点,在什么情况下一个会比另一个更好。微软是否发布了 .Net 来替代 Win32?
I am not asking about the amount of projects needed to be maintained, but about new projects being developed, and which would be better for what. Do you think .Net lacks important stuff from win32 (without using dllImport )? And do you think Win32 will be replaced by .Net
我不是问需要维护的项目数量,而是正在开发的新项目,哪个更好。您是否认为 .Net 缺少来自 win32 的重要内容(不使用 dllImport )?你认为 Win32 会被 .Net 取代吗?
I am asking this, because i am having an argument with a friend of mine, and as we both agree both must be studied in depth My friend argues that .Net is incomplete, and i say that it can manage almost any task non-driver related. Where does .Net fail?
我问这个,因为我和我的一个朋友有争论,而且我们都同意必须深入研究我的朋友认为 .Net 是不完整的,我说它几乎可以管理任何非驱动程序的任务有关的。.Net 在哪里失败?
回答by Richard J Foster
These are all from my POV. Your mileage may vary.
这些都来自我的POV。你的旅费可能会改变。
Advantages of .NET:
.NET 的优势:
- Cross platform capability (via Mono and .Net Core)
- Good selection of framework classes typically means less code.
- Easy integration of components, regardless of the language the component was coded in. (You like F#, or IronPython, great! Code your assemblies in whichever language is most appropriate.)
- 跨平台能力(通过 Mono 和 .Net Core)
- 选择好的框架类通常意味着更少的代码。
- 轻松集成组件,无论组件使用何种语言编码。(您喜欢 F# 或 IronPython,太棒了!使用最合适的语言编写程序集。)
Disadvantages of .NET:
.NET 的缺点:
- .NET means different things to different people. (For example where does WPF come into the equation?)
- Sometimes DLLImport and/or PInvoke is the only way to get to the functionality you need, which means you lose the cross-platform capability.
- .NET 对不同的人意味着不同的东西。(例如,WPF 在哪里进入等式?)
- 有时 DLLImport 和/或 PInvoke 是获得所需功能的唯一方法,这意味着您将失去跨平台功能。
Advantages of WIN32:
WIN32的优点:
- If you know what you are doing, you can construct great applications with minimal dependencies.
- More suitable for "low level" operations than a .NET based solution.
- 如果您知道自己在做什么,就可以构建具有最少依赖性的出色应用程序。
- 比基于 .NET 的解决方案更适合“低级”操作。
Disadvantages of WIN32:
WIN32的缺点:
- If you don't know what you're doing, you can easily shoot yourself in the foot in a more application or system fatal manner.
- You frequently have to write code that you would get "for free" using .NET.
- 如果您不知道自己在做什么,则可以轻松地以更致命的应用程序或系统方式将自己击中脚。
- 您经常需要编写使用 .NET 可以“免费”获得的代码。
Both have their place, and will probably continue to exist until a true replacement OS (Midori? Some form of web-based OS?) comes online and gains widespread acceptance.
两者都有自己的位置,并且可能会继续存在,直到真正的替代操作系统(Midori?某种形式的基于网络的操作系统?)上线并获得广泛接受。
回答by Joel Coehoorn
.Net (or the WinForms parts, anyway) sits on top ofWin32. Or, put another way, Win32 was used to build .Net forms components. So in that sense you can think of .Net as a set of pre-built win32 widgets. You can also think of .Net as the logical successor to MFC.
.Net(或 WinForms 部分,无论如何)位于Win32之上。或者,换句话说,Win32 用于构建 .Net 表单组件。因此,从这个意义上说,您可以将 .Net 视为一组预先构建的 win32 小部件。您还可以将 .Net 视为 MFC 的逻辑继承者。
.Net also has the ability to call into the Win32 API directly when necessary, gives you garbage collection, a very nice class library in the BCL, and a lot of nice language features over C/C++ in C# and VB.Net.
.Net 还具有在必要时直接调用 Win32 API 的能力,为您提供垃圾收集、BCL 中非常好的类库以及 C# 和 VB.Net 中 C/C++ 上的许多不错的语言功能。
What you lose to get all these things is a certain amount of independence. .Net is an add-on frameworkthat is not shipped with all versions of windows by default, and therefore you have extra dependencies to worry about at deployment. You also have performance considerations to think about when using any high-level garbage collected language, where seemingly simple code might be doing a lot more than you expect behind the scenes. This is especially true for some of the winforms components.
获得所有这些东西你失去的是一定程度的独立性。.Net 是一个附加框架,默认情况下并非所有版本的 Windows 都随附,因此您在部署时需要担心额外的依赖项。在使用任何高级垃圾收集语言时,您还需要考虑性能问题,在这些语言中,看似简单的代码在幕后所做的工作可能比您预期的要多得多。对于某些 winforms 组件尤其如此。
回答by Greg D
You already seem to be aware of the short answer, but I'll reiterate it here for clarity:
您似乎已经知道简短的答案,但为了清楚起见,我将在此重申:
Win32: Powerful and complete. Any documented behavior on the windows platform can be composed via Win32, but with power comes responsibility and difficulty. There's a pretty significant amount of subtlety and difficulty in creating rich Win32 experiences.
Win32:强大而完整。Windows 平台上任何记录在案的行为都可以通过 Win32 进行组合,但权力带来了责任和困难。创建丰富的 Win32 体验有相当多的微妙之处和困难。
.Net: Powerful, but a subset of Win32. .Net provides more than enough capability for the vast majority of line-of-business apps and more, but rich UI and specific, specialized situations simply don't exist within it because the BCL, for whatever reason, doesn't believe it makes sense to support it. The most common example that I've run across is UI capabilities in newer versions of Windows, though there are other areas where PInvoke is useful. .Net is a much simpler model to grasp and work within, however, and (in my experience) the subtlety of .net is muchless likely to shoot me in the foot than Win32 is.
.Net:功能强大,但只是 Win32 的一个子集。.Net 为绝大多数业务线应用程序提供了足够多的功能,但其中根本不存在丰富的 UI 和特定的专门情况,因为无论出于何种原因,BCL 都不相信它支持它的感觉。我遇到的最常见的示例是较新版本的 Windows 中的 UI 功能,尽管 PInvoke 在其他领域也很有用。.NET是一个更简单的模型来把握和内部的工作,但是,(在我的经验).NET的精妙之处是多不容易拍我的脚比Win32的是。
If you include dllimport and PInvoke, then I'd guess that .Net is a reasonable alternative that's capable of performing suitably well in 90+% of tasks, and WPF brings a whole different level of rich UI to the managed world.
如果您包括 dllimport 和 PInvoke,那么我猜想 .Net 是一个合理的替代方案,它能够在 90+% 的任务中适当地执行,而 WPF 为托管世界带来了完全不同级别的丰富 UI。
回答by Otávio Décio
It is not a fair comparison, there is no better between the two, it all depends on what you are trying to accomplish. For most development tasks, .Net is very sophisticated and may even have better performance than Win32. On the other hand, .Net can run even outside windows, just look at Mono. Now, for developing drivers and low level stuff you're better served with Win32.
这不是一个公平的比较,两者之间没有更好的,这完全取决于您要实现的目标。对于大多数开发任务,.Net 非常复杂,甚至可能比 Win32 具有更好的性能。另一方面,.Net 甚至可以在窗口外运行,看看 Mono。现在,对于开发驱动程序和低级别的东西,您最好使用 Win32。
回答by John Deters
.Net is not replacing Win32. Win32 is the OS level interface, and you can think of .Net as a little sub-OS environment or virtual machine that sits on top of Win32. .Net provides hardware architecture neutrality (it doesn't matter if your client is running on a Pentium IV, Core 2 Duo, Itanium, 32 bit, 64 bit, or whatever.) .Net offers an optimized installer that understands these environments, so that if an instruction says 'move a million bytes' it'll use the "most optimized" move instruction available on the platform.
.Net 不会取代 Win32。Win32 是操作系统级别的接口,您可以将 .Net 视为位于 Win32 之上的小型子操作系统环境或虚拟机。.Net 提供硬件架构中立性(无论您的客户端是在 Pentium IV、Core 2 Duo、Itanium、32 位、64 位还是其他系统上运行都没有关系。).Net 提供了一个能够理解这些环境的优化安装程序,因此如果一条指令说“移动一百万字节”,它将使用平台上可用的“最优化”的移动指令。
.Net makes some things easier for the developer: built-in garbage collection prevents a very common error, that of forgetting to release memory. The Visual Studio IDE integration offers arguably the best development environment of any language, ever. It's certainly fast to prototype screens and code. (Of course Visual Studio offers many of the same advantages to the C/C++ coder writing to the Win32 API as well.)
.Net 使开发人员的一些事情变得更容易:内置垃圾收集防止了一个非常常见的错误,即忘记释放内存的错误。Visual Studio IDE 集成可以说是任何语言中最好的开发环境。对屏幕和代码进行原型设计肯定很快。(当然,Visual Studio 也为 C/C++ 编码器写入 Win32 API 提供了许多相同的优势。)
But .Net achieves these things at a performance price. Code must be "just-in-time" compiled (JITted) at the end-user's machine before it can be run. The .Net framework takes anywhere from 5-15 megabytes of RAM just to load up enough code to execute "Hello World". The .Net framework won't perform well on a machine slower than 400MHz or with less than 512 MB of RAM (you can run it on a smaller, slower box but its performance will have to be evaluated on an application-by-application basis.)
但是 .Net 以性能价格实现了这些目标。代码必须在最终用户的机器上“即时”编译(JITted),然后才能运行。.Net 框架需要 5-15 兆字节的 RAM 来加载足够的代码来执行“Hello World”。.Net 框架在速度低于 400MHz 或 RAM 低于 512 MB 的机器上性能不佳(您可以在更小、更慢的机器上运行它,但必须根据应用程序逐个评估其性能.)
回答by Kirill V. Lyadvinsky
.NET
in your question implies C#
or C++/CLI
?
.NET
with C++/CLI
is something unnatural. With the other hand using C#
with .NET
is more comfortable than with Win32
.
.NET
在你的问题中暗示C#
或C++/CLI
?
.NET
withC++/CLI
是不自然的东西。另一方面,使用C#
with.NET
比 with 更舒服Win32
。
回答by HW_pespective
for HW accessing WIN API is best choice. its faster because it talks to low level devices directly but no free lunch; Your code will be more complex and it does not forgive for bad coding.
对于硬件访问 WIN API 是最好的选择。它更快,因为它直接与低级设备对话,但没有免费午餐;你的代码会更复杂,它不会原谅糟糕的编码。