使用C#編寫COM組件詳盡過程

來源:互聯網
上載者:User
1、建立一個類庫項目

2、將Class1.cs改為我們想要的名字

問是否同時給類改名,確定

3、修改Properties目錄下面的AssemblyInfo.cs
ComVisible屬性設定為True

4、項目菜單->MyLib屬性
找到“產生”選項卡

往下看,找到“為 COM Interop 註冊”勾上

5、繼續往下,找到“簽名”選項卡
  勾上“為程式集簽名”
  在下面的下拉框裡面選擇“ <建立...>”

6、在彈出的對話方塊裡面,輸入MyLib。。或者隨便取個名字
  去掉使用密碼保護檔案的選項

7、開始編碼,任何一個公開的類,必須有一個 I開通的介面定義

C# code
using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace MyLib{    [ComVisible(true)]    [Guid("2CBD3D76-35F1-4f9d-9C1B-9DBFEE412F76")]    public interface IMyClass    {        void Initialize();        void Dispose();        int Add(int x, int y);    }    [ComVisible(true)]    [Guid("EA2F140A-108F-47ae-BBD5-83EEE646CC0D")]    [ProgId("MyLib.MyClass")]    public class MyClass : IMyClass    {        public void Initialize()        {            //nothing todo        }        public void Dispose()        {            //nothing todo        }        public int Add(int x, int y)        {            return x + y;        }    }}

8、GUID屬性裡面的那個字串,在“工具”菜單下面,“建立 GUID”
  選擇 Registry Format,然後複製
 

  注意在[Guid("....... 這個裡面要去掉GUID前後的花括弧

9、編譯它
在命令提示字元下面,進入Dll所在的目錄
用 gacutil /i MyLib.dll 將這個DLL加入的全域緩衝裡
然後用 regasm MyLib.dll 註冊這個dll

10、在VBScript裡面試試。。。

HTML code
<script language="VBScript">
Dim o : Set o=CreateObject("MyLib.MyClass")  
  o.Initialize    MsgBox "1 + 2 = " & o.Add(1,2)    o.Dispose    Set o=Nothing</script>
1、建立一個類庫項目

2、將Class1.cs改為我們想要的名字

問是否同時給類改名,確定

3、修改Properties目錄下面的AssemblyInfo.cs
ComVisible屬性設定為True

4、項目菜單->MyLib屬性
找到“產生”選項卡

往下看,找到“為 COM Interop 註冊”勾上

5、繼續往下,找到“簽名”選項卡
  勾上“為程式集簽名”
  在下面的下拉框裡面選擇“ <建立...>”

6、在彈出的對話方塊裡面,輸入MyLib。。或者隨便取個名字
  去掉使用密碼保護檔案的選項

7、開始編碼,任何一個公開的類,必須有一個 I開通的介面定義

C# code
using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace MyLib{    [ComVisible(true)]    [Guid("2CBD3D76-35F1-4f9d-9C1B-9DBFEE412F76")]    public interface IMyClass    {        void Initialize();        void Dispose();        int Add(int x, int y);    }    [ComVisible(true)]    [Guid("EA2F140A-108F-47ae-BBD5-83EEE646CC0D")]    [ProgId("MyLib.MyClass")]    public class MyClass : IMyClass    {        public void Initialize()        {            //nothing todo        }        public void Dispose()        {            //nothing todo        }        public int Add(int x, int y)        {            return x + y;        }    }}

8、GUID屬性裡面的那個字串,在“工具”菜單下面,“建立 GUID”
  選擇 Registry Format,然後複製
 

  注意在[Guid("....... 這個裡面要去掉GUID前後的花括弧

9、編譯它
在命令提示字元下面,進入Dll所在的目錄
用 gacutil /i MyLib.dll 將這個DLL加入的全域緩衝裡
然後用 regasm MyLib.dll 註冊這個dll

10、在VBScript裡面試試。。。

HTML code
<script language="VBScript">
Dim o : Set o=CreateObject("MyLib.MyClass")  
  o.Initialize    MsgBox "1 + 2 = " & o.Add(1,2)    o.Dispose    Set o=Nothing</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.