• <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#基礎}

            C# DLL COM


            前言:為了介紹C#寫界面,C++寫算法的快捷交互開發方式,首先介紹c++,C#內部的DLL,COM調用.

            一, C# DLL
            C#創建DLL非常的簡單,只需要創建工程,選擇工程類型為class libaray即可。這里不介紹。

            二,C# COM
            C#創建COM,相對與一般的DLL,有幾個地方需要注意,看下面的實例:

            Steps to create a Managed .NET C# COM Object:

            1. Open VS.NET2003->New Project->Visual C# Projects->Class Library.
            2. Project name: MyInterop.
            3. Create MyDoNetClass.cs file, and add the following lines of code:
              using System.Runtime.InteropServices;
                  using System.Windows.Forms;
            4. Create an Interface IMyDotNetInterface.
            5. Create a class MyDoNetClass.
            6. Add the following line for MyDotNetClass:
              [ClassInterface(ClassInterfaceType.None)]

            Although a .NET class is not directly invokable from unmanaged code, Microsoft has provided the capability of wrapping a .NET interface in an unmanaged layer of code that exposes the methods and properties of the .NET class as if the class were a COM object. There are two requirements for making a .NET class visible to unmanaged code as a COM object:

            Requirement 1:

            You have to add GUIDs - Globally Unique Identifiers - into your code for the interface and the class separately, through a GUID tool.

            1. Now, create a GUID for the Interface, and add the following line for the interface:
              [Guid("03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
            2. Now, create a GUID for the class, and add the following line for the class:
              [Guid("0490E147-F2D2-4909-A4B8-3533D2F264D0")]
            3. Your code will look like:      
            using System;
            using System.Runtime.InteropServices;
            using System.Windows.Forms;

            namespace MyInterop
            {
                [Guid(
            "03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
                interface IMyDotNetInterface
                {
                    void ShowCOMDialog();
                }
                 
                [ClassInterface(ClassInterfaceType.None)]
                [Guid(
            "0490E147-F2D2-4909-A4B8-3533D2F264D0")]
                class MyDotNetClass : IMyDotNetInterface
                {
                    
            // Need a public default constructor for COM Interop.
                    
            public MyDotNetClass()
                    {}
                    
            public void ShowCOMDialog()
                    {
                        System.Windows.Forms.MessageBox.Show(“I am a
            " + 
                              "  Managed DotNET C# COM Object Dialog”);
                    }
                }
            }

             

            10,Compile the solution.

            11,You will see inside the project directory->obj->debug directory, the file “MyInterop.dll” generated after compilation.

            Requirement 2:

            Registration of the COM Class and Interfaces

            For a COM class to be accessible by the client at runtime, the COM infrastructure must know how to locate the code that implements the COM class. COM doesn't know about .NET classes, but .NET provides a general "surrogate" DLL - mscoree.dll -- which acts as the wrapper and intermediary between the COM client and the .NET class.

            1. Hard-code a specific version number in your AssemblyVersion attribute in the AssemblyInfo.cs file which is in your project.

              Example:

              [assembly: AssemblyVersion("1.0.0.0")]
            2. Create a strong-name key pair for your assembly and point to it via the AssemblyKeyFile attribute in the AssemblyInfo.cs file which is in your project. Example:
              sn -k TestKeyPair.snk
              [assembly: AssemblyKeyFile("TestKeyPair.snk")]
            3. Add your assembly to the GAC using the following command:
              gacutil /i MyInterop.dll
            4. Register your assembly for COM by using the REGASM command along with the "/tlb" option to generate a COM type library.
              REGASM MyInterop.dll /tlb:com.MyInterop.tlb
            5. Close the C# project.

                到此COM創建完畢,可以被C#和Native的client調用。

            三,C#內部調用
            C#對C#的COM的調用和一般的DLL的調用一樣的簡單,只需要在工程中reference 所需要的COM或DLL。(Net真強!)

            四,總結
            一般使用C#開發DLL,即程序集,一般不開發COM,如果是要開發COM,一般了為了兼容以前的COM,則這個時候可以考慮把以前的COM轉化net的程序集來使用。一般C#的COM會被Native代碼來調用,具體的C++調用C#的COM見后面的章節。

            參考:http://www.codeproject.com/csharp/ManagedCOM.asp  (包含上面的實例代碼下載,C# 的Client的調用很簡單,自己寫!:~)

            簡單實例下載:http://www.shnenglu.com/Files/mzty/C#COM.rar

            posted on 2007-05-29 11:02 夢在天涯 閱讀(6716) 評論(3)  編輯 收藏 引用 所屬分類: C#/.NET

            評論

            # re: C# DLL COM 2007-06-22 12:25 .net編程

            非常的好  回復  更多評論   

            # re: C# DLL COM 2007-10-25 13:14 king2003

            C#是托管的吧.這個沒有.NET類庫能用嗎?  回復  更多評論   

            # re: C# DLL COM 2007-10-25 13:17 夢在天涯

            不能!  回復  更多評論   

            公告

            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

            搜索

            •  

            積分與排名

            • 積分 - 1804759
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产香蕉久久精品综合网| 久久久久人妻精品一区| 99久久国产亚洲高清观看2024| 狠狠干狠狠久久| 亚洲美日韩Av中文字幕无码久久久妻妇 | 欧美性大战久久久久久| 久久经典免费视频| 久久青青草原国产精品免费 | 久久国产免费直播| 国内精品久久九九国产精品| 久久青青草原精品国产不卡| 久久精品国产亚洲av影院| 久久亚洲av无码精品浪潮| 香蕉久久av一区二区三区| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 国产三级久久久精品麻豆三级| 久久久久18| 国产99久久精品一区二区| 国产精品久久久久久久app| 久久精品国产影库免费看| 狠狠精品久久久无码中文字幕| 99久久婷婷国产综合精品草原| 久久综合国产乱子伦精品免费| 久久伊人亚洲AV无码网站| 国产L精品国产亚洲区久久| 97精品久久天干天天天按摩 | 久久国产精品-国产精品| 亚洲va中文字幕无码久久不卡| 久久综合亚洲色HEZYO国产| 99久久精品无码一区二区毛片| 国产精品久久久久影视不卡| 久久香蕉超碰97国产精品| 亚洲精品国精品久久99热一| 日韩精品久久久久久久电影| 欧美亚洲另类久久综合婷婷 | 99久久人妻无码精品系列| 亚洲va久久久噜噜噜久久狠狠| 成人午夜精品无码区久久| 色欲av伊人久久大香线蕉影院| 麻豆亚洲AV永久无码精品久久| 亚洲精品乱码久久久久久久久久久久|