[转]使用ASP调用C#写的COM组件

本文介绍如何使用C#创建一个可以被ASP等调用的COM组件,并提供了详细的步骤指导及测试代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: http://www.cnblogs.com/rentj1/archive/2009/02/23/1396187.html

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、类必须有一个无参构造函数

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Runtime.InteropServices;
namespaceMyTestDll
{

//这里Guid为第4步生成的。
[Guid("FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF")]
publicinterfaceIMyTestDll
{
[DispId(
0)]
stringGetAbout();
}
publicclassTest1:IMyTestDll
{
privatestringsummary;
publicTest1()
{
summary
="这是我的第一个测试";
}
publicstringGetAbout()
{
returnsummary;
}
}
}

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.youkuaiyun.com/u/20080625/13/0294fe91-200c-4939-b36b-c9a2c6781354.html


http://topic.youkuaiyun.com/t/20060314/15/4613620.html

http://cplus.e800.com.cn/articles/2009/211/1234338268521_3.html


http://topic.youkuaiyun.com/t/20020712/10/868557.html
http://www.itzhe.cn/news/20071123/21768.html

http://www.cnblogs.com/illele/archive/2007/10/25/937050.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值