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")]
publicinterface IMyClass
{
void Initialize();
void Dispose();
int Add(int x, int y);
}
[ComVisible(true)]
[Guid("EA2F140A-108F-47ae-BBD5-83EEE646CC0D")]
[ProgId("MyLib.MyClass")]
publicclass MyClass : IMyClass
{
publicvoid Initialize()
{
//nothing todo }
publicvoid Dispose()
{
//nothing todo }
publicint 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>