c++/cli 如何在内部与非托管部件一起工作?
它是如何运作的?它是否有不同的部分 - 有些方法是托管的,有些是非托管的,它是否将每个方法转换为托管,尝试保持所有内容托管并在必要时进行互操作调用?
How does it work? Does it have distinct parts - some methods are managed, some are unmanaged, does it convert every method to managed, trying to keep everything managed and doing the interop calls when he must?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存在与 C++/CLI 中托管代码生成相关的三种不同的编译器开关 :
/clr:pure
使编译器生成 MSIL 代码。不允许使用非托管函数(所有内容都编译为 MSIL)。此模式类似于不安全
C# 代码。在此模式下,您可以使用 CRT,它有一个纯 MSIL 版本可用。在此模式下编译的程序集不能用于需要可验证代码的部分信任环境(例如某些 SQL Server 托管程序集)。/clr:safe
使编译器生成可验证的 MSIL 代码,类似于 C# 编译器。不允许 C++ 互操作。您可以在安全策略强制执行可验证性的部分信任环境中运行/clr:safe
程序集。/clr
生成混合程序集。二进制文件将包含 MSIL 代码和本机代码。它们不会混合在一起,形成一个类。托管和非托管部分是分开的,编译器将根据需要生成代码以在两个部分之间互操作和编组数据。为了直接回答您的问题,不执行特定的“转换”。
pure
和safe
模式中根本不允许使用非托管代码。在允许的情况下,托管内容会编译为 MSIL,而非托管内容会编译为机器代码。它们之间的任何互操作都需要编译器生成代码。那里没有魔法。它只是向程序员隐藏了一些互操作的东西,但代码仍然存在。There are three different compiler switches related to managed code generation in C++/CLI:
/clr:pure
makes the compiler produce MSIL code. No unmanaged functions are allowed (everything is compiled to MSIL). This mode is similar tounsafe
C# code. In this mode, you can use the CRT there's a pure MSIL version of it available. Assemblies compiled in this mode cannot be used in partial trust environments that require verifiable code (e.g. some SQL Server hosted assemblies)./clr:safe
makes the compiler produce verifiable MSIL code, similar to the C# compiler. No C++ interop is allowed. You can run/clr:safe
assemblies in partial trust environments where verifiability is enforced by security policy./clr
generates mixed assemblies. The binary will contain both MSIL code and native code. They are not mixed together in say, a single class. Managed and unmanaged parts are separate and the compiler will generate code to interop and marshal data between two parts as necessary.To directly answer your question, no specific "conversion" is performed. Unmanaged code is simply not allowed in
pure
andsafe
modes. Where it's allowed, managed stuff are compiled down to MSIL and unmanaged stuff are compiled to machine code. Any interop between them requires the compiler to generate code. There's no magic there. It just hides some interop stuff from the programmer but the code is there nonetheless.