日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#创建com组件

發(fā)布時間:2023/12/18 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#创建com组件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
 ???? 本文詳細闡述如何用C#創(chuàng)建COM組件,并能用VC6.0等調(diào)用。并附有完整測試通過的代碼。廢話不多說,下面開始介紹:

開發(fā)工具:VS2010

VS2010命令提示符在:開始-所有程序-Visual?Studio?2010-Visual?Studio?Tool-命令提示符?

在用C#創(chuàng)建COM組件時,一定要記住以下幾點:

1:所要導(dǎo)出的類必須為公有;

2:所有屬性、方法也必須為公有;

3:要導(dǎo)出的屬性、方法必須用接口方式;如果沒有在接口中聲明,即使該方法(屬性)為公有,也不能正常導(dǎo)出到COM。但他們可以被別的.NET程序所使用;

4:所有的事件也必須用接口方式;

一、新建一個Visual C#解決方案,選擇類型“類庫”;名稱為:MyCom。

二、編寫導(dǎo)出接口。為了大家理解方便,我僅一加法操作舉例。如下:

[csharp] view plaincopyprint?
  • [Guid("51917E6C-87A2-4B70-B1A8-47C47F8007E4")]??
  • ????public?interface?MyCom_Interface??
  • ????{??
  • ???????[DispId(1)]??
  • ????????int?Add(int?param1,?int?param1);??
  • ????}??
  • [Guid("51917E6C-87A2-4B70-B1A8-47C47F8007E4")]public interface MyCom_Interface{[DispId(1)]int Add(int param1, int param1);}在VS2010的命令提示符中輸入:guidgen 就會出來它的窗口。在幾個復(fù)選框選擇,點擊"新建 Guid",然后"復(fù)制"就行了(以下如有g(shù)uid的字符串,全部同樣操作)

    [DispId(1)]為函數(shù)的標(biāo)識。如果有多個函數(shù)可相應(yīng)的在函數(shù)前面加[DispId(2)], [DispId(3)]…

    三、創(chuàng)建事件接口。

    [csharp] view plaincopyprint?
  • [Guid("0C807AEC-4223-4A03-95A1-3E3AC065E4B7"),??
  • ????InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]??
  • ????public?interface?MyCom_Events??
  • ????{??
  • ????}??
  • [Guid("0C807AEC-4223-4A03-95A1-3E3AC065E4B7"),InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]public interface MyCom_Events{}

    Guid同二,不多說

    InterfaceType表求向COM公開的方式,這里選擇為以調(diào)度的方式向COM公開。

    四、 創(chuàng)建具體類:

    [csharp] view plaincopyprint?
  • [Guid("FE6A8C43-D4B6-4100-BC6B-06333F95A57F"),??
  • ??????ClassInterface(ClassInterfaceType.None),??
  • ??????ComSourceInterfaces(typeof(MyCom_Events))]??
  • ????public?class?Class1?:?MyCom_Interface??
  • ????{??
  • ????????public?int?Add(int?a,?int?b)??
  • ????????{??
  • ????????????return?a?+?b;??
  • ????????}??
  • ????}??
  • [Guid("FE6A8C43-D4B6-4100-BC6B-06333F95A57F"),ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(MyCom_Events))]public class Class1 : MyCom_Interface{public int Add(int a, int b){return a + b;}}把整體代碼放在下面(注意引用InteropServices)

    [csharp] view plaincopyprint?
  • using?System;??
  • using?System.Runtime.InteropServices;??
  • using?System.Text;??
  • ??
  • namespace?MyCom??
  • {??
  • ????[Guid("51917E6C-87A2-4B70-B1A8-47C47F8007E4")]??
  • ????public?interface?MyCom_Interface??
  • ????{??
  • ???????[DispId(1)]??
  • ????????int?Add(int?param1,?int?param1);??
  • ????}??
  • ??
  • ????[Guid("0C807AEC-4223-4A03-95A1-3E3AC065E4B7"),??
  • ????InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]??
  • ????public?interface?MyCom_Events??
  • ????{??
  • ????}??
  • ??
  • ????[Guid("FE6A8C43-D4B6-4100-BC6B-06333F95A57F"),??
  • ??????ClassInterface(ClassInterfaceType.None),??
  • ??????ComSourceInterfaces(typeof(MyCom_Events))]??
  • ????public?class?Class1?:?MyCom_Interface??
  • ????{??
  • ????????public?int?Add(int?a,?int?b)??
  • ????????{??
  • ????????????return?a?+?b;??
  • ????????}??
  • ????}??
  • }??
  • using System; using System.Runtime.InteropServices; using System.Text;namespace MyCom {[Guid("51917E6C-87A2-4B70-B1A8-47C47F8007E4")]public interface MyCom_Interface{[DispId(1)]int Add(int param1, int param1);}[Guid("0C807AEC-4223-4A03-95A1-3E3AC065E4B7"),InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]public interface MyCom_Events{}[Guid("FE6A8C43-D4B6-4100-BC6B-06333F95A57F"),ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(MyCom_Events))]public class Class1 : MyCom_Interface{public int Add(int a, int b){return a + b;}} }

    五、大家都知道COM是需要注冊的。注冊時要加密鑰文件.SNK。這一部就是生成SNK文件。進入VS2008命令提示符。用命令:sn –k MyCom.snk回車。我的在D:\Program Files\Microsoft Visual Studio 10.0\VC(就是你VS安裝目錄下)下面就生成了一個(MyCom.snk)的文件。。然后把它COPY到你的工程根目錄下。

    六、打開AssemblyInfo.cs。在里面加入[assembly:AssemblyKeyFile("MyCom.snk")]

    七、(1)項目屬性->應(yīng)用程序->程序集信息->選中“使程序集COM可見”。

    ?????? (2)項目屬性->生成->選中“為COM互操作注冊”。

    八、生成。如果在Debug下有一個MyCom.tlb,那你就成功了(肯定還有MyCom.dll)要此tlb文件是為了在VC6.0里面測試。

    九、在VC里面建一個MFC對話框程序(當(dāng)然Console程序也一樣,我為了看著方便)。建好后,把剛剛生成的MyCom.tlb拷到你的根目錄下。

    十、選在代碼里加入#import "MyCom.tlb",然后編寫核心測試代碼:(我是在一個Button1按鈕里面添加的),如下:

  • void?CTestDlg::OnButton1()? ?
  • { ?
  • ????CoInitialize(NULL);??//注意初始化 ?
  • ????MyCom::MyCom_InterfacePtr?p(__uuidof(MyCom::Class1));??//創(chuàng)建智能指針 ?
  • ????MyCom::MyCom_Interface?*s?=?p; ?
  • ????int?a?= 2; ?
  • ????int?b?= 2; ?
  • ????int?c?=?s->Add(a,b); ?
  • ????CString?str; ?
  • ????str.Format("%d",c); ?
  • ????MessageBox(str); ?
  • }?
  • 當(dāng)彈出一個4,就說明你已經(jīng)成功用C#創(chuàng)建COM組件了。

    就介紹到這吧。關(guān)于具體的com組件的知識可以查看百度百科:點擊打開鏈接

    總結(jié)

    以上是生活随笔為你收集整理的C#创建com组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。