asp調用c#編製的com組件執行個體

來源:互聯網
上載者:User

 

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來註冊 

方法為: 

首先把binDebug目錄的檔案拷貝到目標電腦上,然後開啟命令提示行工具輸入: 
regasm 你拷貝到的目錄/檔案名稱.dll /tlb f:/dll/檔案名稱.tlb /codebase 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.