使用ASP調用C#寫的COM組件

1 新建類庫MyTestDLL

2 右擊項目“MyTestDLL”-》屬性-》生成-》勾選“爲COM互操作註冊”

3 打開 AssemblyInfo.cs 文件 修改 [assembly: ComVisible(true)]

4 打開Visual Sutdio 2008 的命令提示行工具輸入guidgen.exe 選擇DEFINE_GUID 單擊 "New GUID"

5代碼

      1、每個類名對應一個接口名,接口名是類名前加上一個大寫的I

      2、接口中聲明的方法要使用屬性 [DispId(n)]

      3、類必須有一個無參構造函數


Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MyTestDll
{

     //  這裏Guid爲第4步生成的。
    [Guid("FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF")]
    public interface IMyTestDll
    {
        [DispId(0)]
        string GetAbout();
    }
    public class Test1:IMyTestDll
    {
        PRivate string summary;
        public Test1()
        {
            summary = "這是我的第一個測試";
        }
        public string GetAbout()
        {
            return summary;
        }
    }
}

 

6 生成項目

asp測試代碼 

<%   
  Dim  o    
  Set o = Server.CreateObject("MyTestDll.Test1")   
  Response.Write o.GetAbout()
  Set o=Nothing  
  
  %>  

提示:如果要在其他的電腦使用我們用C#開發的這個COM組件還需要是用regasm來註冊

方法爲:

首先把bin\Debug目錄的文件拷貝到目標電腦上,然後打開命令提示行工具輸入:
regasm 你拷貝到的目錄/文件名.dll /tlb f:/dll/文件名.tlb /codebase

 運行既可在該電腦上使用。
 

參考資料:

http://topic.csdn.net/u/20080625/13/0294fe91-200c-4939-b36b-c9a2c6781354.html
http://topic.csdn.net/t/20060314/15/4613620.html
http://cplus.e800.com.cn/articles/2009/211/1234338268521_3.html
http://topic.csdn.net/t/20020712/10/868557.html
http://www.itzhe.cn/news/20071123/21768.html
http://www.cnblogs.com/illele/archive/2007/10/25/937050.html

發佈了24 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章