• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            Native c++ 和Managed 的 interop

            4種方法:

            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 夢在天涯 閱讀(2255) 評論(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編譯選項  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807602
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久精品亚洲中文字幕无码麻豆| 久久福利青草精品资源站免费| 久久涩综合| 久久丫忘忧草产品| 婷婷综合久久中文字幕蜜桃三电影| 国产精品一久久香蕉国产线看观看| 亚洲国产二区三区久久| 色综合久久天天综线观看| 久久久久久亚洲精品成人| 久久91这里精品国产2020| 久久人人爽人人爽人人片AV高清 | 精品久久久久国产免费 | 996久久国产精品线观看| 久久久久一本毛久久久| 亚洲中文字幕无码一久久区 | 亚洲综合久久久| 99久久中文字幕| 久久久久se色偷偷亚洲精品av | 久久国产成人精品麻豆| 99久久这里只精品国产免费| 久久91综合国产91久久精品| 色综合久久无码五十路人妻| 日韩十八禁一区二区久久| 久久精品国产半推半就| 久久亚洲AV成人出白浆无码国产| 激情久久久久久久久久| 久久国产高清字幕中文| 久久久免费精品re6| 国产成人无码精品久久久性色| 久久本道久久综合伊人| 2021精品国产综合久久| 久久99精品久久久久婷婷| 蜜桃麻豆WWW久久囤产精品| 青青热久久国产久精品 | 亚洲欧洲日产国码无码久久99| 欧美日韩精品久久久久| 亚洲国产精品久久66| 91久久香蕉国产熟女线看| 成人精品一区二区久久| 精品国产青草久久久久福利| 久久强奷乱码老熟女网站|