C++ CLR win32 还是其他?
我正在使用 C++,发现有不同类型的 C++,例如 CLR、Win32、MFC...
此外,我发现某些 C++ 库可以在其他 C# 程序中调用。怎么可能呢?
由于人们认为C++比C#更快,因为它不需要运行平台,但是,如果在CLR中使用C++,它会因为需要.Net框架而变得更慢吗?
问题是: 我想制作一个调用 Windows 命令并返回输出的 C++ 库,并且可以在其他 C# 程序中调用该库。可以这样做吗?如果是,C++ 库是否需要 .Net 框架并且运行速度较慢?
I am working with C++ and found that there are different kinds of C++ such as CLR, Win32, MFC....
Besides, I found that some C++ library cab be called inside some other C# program. How can it be?
Since it is believed that C++ is faster than C# as it doesn't require a platform to run it, however, if using C++ in CLR, will it become slower as the .Net framework is required?
The question is:
I wanna make a C++ library that call the windows command and return the output, and this library could be called in other C# program. Is it possible to do so? and if yes, will the C++ library required the .Net framework and run slower?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的困惑是可以理解的,但你的理解是有缺陷的。
C++ 和使用 MFC 之间没有区别,MFC 是用 C++ 编写的类框架,就像您可能用 C++ 编写的任何其他代码一样。它只是微软随其产品提供的一个框架。
C++/CLI(不是 CLR)并不是真正的 C++。它是一种类似 C++ 的语言,可编译为 .net IL 字节码。它有很多限制,并且与 C# 和 VB.NET 以及其他 .NET 语言具有相同的限制。它与 C++ 非常相似,但又不完全相同。
C++/CLI 应用程序编译为 .net IL 字节码,因此它们与 C# 应用程序没有什么不同。事实上,几乎任何基于 .net 的语言都会编译为相同的字节码,并使用相同的框架。
C# 或 C++/CLI 程序是否“较慢”尚不明确。太多的 C++ 爱好者延续了托管代码速度缓慢的神话。它不是。有些事情很慢,例如第一次运行它(它必须按需编译代码),但是因为.NET Jitter(及时编译器)可以针对其运行的平台优化代码,所以它可以使代码表现更好。
当然,托管代码也有垃圾收集功能,这非常方便,但在某些情况下可能会导致性能问题。但是,这并不是所有情况,甚至不是大多数情况。
Your confusion is understandable, but your understanding is flawed.
There's no difference between C++ and using MFC, MFC is a class framework written in C++, just like any other code you might write in C++. It's just a framework that Microsoft provides with their product.
C++/CLI (not CLR) is not really C++. It's a C++-like language that compiles to .net IL bytecode. It has many limitations, and has all the same restrictions that C# and VB.NET and other .NET languages have. It's very similar to C++, but not quite the same.
C++/CLI applications compile to .net IL bytecode, so they are no different than C# applications. In fact, pretty much any .net based language will compile down to the same bytecode, and use the same frameworks.
Whether or not C# or C++/CLI programs are "slower" is not so clear cut. All too many C++ enthusiests perpetuate the myth that managed code is slow. It's not. Some things are slow, such as running it the first time (it has to compile the code on demand), but because the .NET Jitter (just in time compiler) can optimize code for the platform it's running on, it came make the code perform better.
Of course managed code also has garbage collection, which is very convenient, but might cause issues with performance in some situations. But, this is not every situation and it's not even most situations.
是的,这是可能的。
如果您使用 C++/CLI,则只需将程序集包含在 C# 项目中,然后像普通 C# 代码一样调用函数。如果您使用非 CLI C++ 编写它(Win32 和 MFC 不是 C++ 的一种,它们只是库...),那么您必须使用 P/Invoke 从 C# 调用 C++ 编写的函数。
是的,在某些方面,C++/CLI 可能比普通 C++ 慢一些。但幅度不大。 CLR 相当快。
Yes, it's possible.
If you use C++/CLI, you can just include the assembly in your C# project and call the functions like normal C# code. If you write it using non-CLI C++ (Win32 and MFC are not kinds of C++, they're just libraries...) then you'll have to use P/Invoke from C# to call functions written in C++.
Yes, C++/CLI is probably a little slower than vanilla C++ for some things. But not by much. The CLR is quite fast.