C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

Native c++ 和Managed 的 interop


There are four main ways to do interop in .NET between the managed and native worlds. COM interop can be accomplished using Runtime Callable Wrappers (RCW) and COM Callable Wrappers (CCW). The common language runtime (CLR) is responsible for type marshaling (except in the rare scenarios where a custom marshaler is used) and the cost of these calls can be expensive. You need to be careful that the interfaces are not too chatty; otherwise a large performance penalty could result. You also need to ensure that the wrappers are kept up to date with the underlying component. That said, COM interop is very useful for simple interop scenarios where you are attempting to bring in a large amount of native COM code.

The second option for interop is to use P/Invoke. This is accomplished using the DllImport attribute, specifying the attribute on the method declaration for the function you want to import. Marshaling is handled according to how it has been specified in the declaration. However, DllImport is only useful if you have code that exposes the required functions through a DLL export.

When you need to call managed code from native code, CLR hosting is an option. In such a scenario, the native application has to drive all of the execution: setting up the host, binding to the runtime, starting the host, retrieving the appropriate AppDomain, setting up the invocation context, locating the desired assembly and class, and invoking the operation on the desired class. This is definitely one of the most robust solutions in terms of control over what and when things happen, but it is also incredibly tedious and requires a lot of custom code.

The fourth option, and quite possibly the easiest and the most performant, is to use the interop capabilities in C++. By throwing the /clr switch, the compiler generates MSIL instead of native machine code. The only code that is generated as native machine code is code that just can't be compiled to MSIL, including functions with inline asm blocks and operations that use CPU-specific intrinsics such as Streaming SIMD Extensions (SSE). The /clr switch is how the Quake II port to .NET was accomplished. The Vertigo software team spent a day porting the original C code for the game to code that successfully compiled as C++ and then threw the /clr switch. In no time they were up and running on the .NET Framework. Without adding any additional binaries and simply by including the appropriate header files, managed C++ and native C++ can call each other without any additional work on the part of the developer. The compiler handles the creation of the appropriate thunks to go back and forth between the two worlds.

posted on 2006-08-17 16:26 梦在天涯 阅读(2167) 评论(5)  编辑 收藏 引用 所属分类: CPlusPlusC#/.NETManage c++ /CLIVS2005/2008


# re: Native c++ 和Managed 的 interop 2006-08-17 16:28 梦在天涯

msdn enlish:http://msdn.microsoft.com/msdnmag/issues/04/05/VisualC2005/default.aspx#S1  回复  更多评论   

# re: Native c++ 和Managed 的 interop 2006-08-17 16:39 梦在天涯

1 使用RCW和CCW来访问
是否可以在 .NET 框架程序中使用 COM 对象?
是。您现在部署的任何 COM 组件都可以在托管代码中使用。通常情况下,所需的调整是完全自动进行的。

特别是,可以使用运行时可调用包装 (RCW) 从 .NET 框架访问 COM 组件。此包装将 COM 组件提供的 COM 接口转换为与 .NET 框架兼容的接口。对于 OLE 自动化接口,RCW 可以从类型库中自动生成;对于非 OLE 自动化接口,开发人员可以编写自定义 RCW,手动将 COM 接口提供的类型映射为与 .NET 框架兼容的类型。

是否可以在 COM 程序中使用 .NET 框架组件?
是。您现在创建的托管类型都可以通过 COM 访问。通常情况下,所需的配置是完全自动进行的。托管开发环境的某些新特性不能在 COM 中访问。例如,不能在 COM 中使用静态方法和参数化构造函数。一般,提前确定给定类型所针对的用户是一种较好的办法。如果类型需要在 COM 中使用,您将被限制在使用 COM 可访问的特性。


特别是,可以使用 COM 可调用包装 (CCW) 从 COM 访问 .NET 框架组件。这与 RCW(请参阅上一个问题)相似,但它们的方向相反。同样,如果 .NET 框架开发工具不能自动生成包装,或者如果自动方式不是您所需要的,则可以开发自定义的 CCW。

  回复  更多评论   

# re: Native c++ 和Managed 的 interop 2006-08-17 16:53 梦在天涯

2 使用p\invoke方法
是否可以在 .NET 框架程序中使用 Win32 API?
是。使用 P/Invoke,.NET 框架程序可以通过静态 DLL 入口点的方式来访问本机代码库。

下面是 C# 调用 Win32 MessageBox 函数的示例:

using System;
using System.Runtime.InteropServices;

class MainApp
[DllImport("user32.dll", EntryPoint="MessageBox")]
public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);

public static void Main()
MessageBox( 0, "您好,这是 PInvoke!", ".NET", 0 );
  回复  更多评论   

# re: Native c++ 和Managed 的 interop 2006-08-17 16:57 梦在天涯

3 使用 CLR hosting  回复  更多评论   

# re: Native c++ 和Managed 的 interop 2006-08-17 16:58 梦在天涯

4 修改clr编译选项  回复  更多评论   

【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理





  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0










My self

Often go

Useful Webs





  • 积分 - 1779536
  • 排名 - 5