哪位知道如何用c#生成一个com组件?

来源:百度知道 编辑:UC知道 时间:2024/06/08 16:17:21
谢谢!

将.net托管dll导出为com,以便于非托管的程序调用
CCW(COM Callable wrapper) 是vs2005提供的一个机制,它可以把 .Net 托管DLL导出成传统COM,便于VB6, ASP 调用, 下面是一个普通例子:

1> 在vs2005中建立一个普通C# 的 class library 工程, 并在写入代码
//
//假定空间名为csLib2,类名为Class1
//蓝色部分很重要
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace csLib2
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Class1
{
public int add(int a, int b)
{
return(a + b);
}

public string mergeString(string a, string b)
{
return(a+b);
}
}
}

2> 在工程-->"Properties"-->"Build"-->选中"Register for COM interop"
3> 在工程中的 AssemblyInfo.cs 文件中修改[assembly: ComVisible(true)]//原来默认为false
4> 在Visual studio 2005 Command prompt命令行工具