C#写COM组件提供ASP使用

1. C#代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace ComTest
{
    [Guid("DE03FB14-23D6-4be0-93EA-C27651A22A37")]
    public interface ITest
    {
        string Test(string test);
    }
    [Guid("0ED3C99A-6EBB-4df5-B03F-16CACE242C67")]
    public class CTest : ITest
    {
        public string Test(string test)
        {
            return test;
        }
    }
}
 
2. AssemblyInfo.cs文件设置
[assembly: ComVisible(true)] 必须设置为True
[assembly: AssemblyVersion("1.0.0.0")] 必须明确指定,不能用*号
[assembly: AssemblyKeyFile(@"c:/keyfile.snk")]
注:c:/keyfile.snk 由 sn -k c:/keyfile.snk 生成
 
3. 生成tlb文件
通过Ctrl+Shift+B生成DLL文件,然后通过tlbexp ComTest.dll /out:ComTest.tlb得到.tlb文件
 
4. 注册tlb成com
通过C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/RegAsm /tlb:ComTest.tlb ComTest.dll /CodeBase
 
5. 加入全局缓存中(非必须,建议不进行)
gacutil -i ComTest.dll
 

GetKey.bat(得到KeyFile批处理):
path E:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/bin
sn -k c:/keyfile.snk
 
RegCom.bat(生成tlb并注册成com批处理):
path E:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/bin
tlbexp C:/Downloads/ComTest/ComTest/ComTest/bin/Release/ComTest.dll /out:C:/Downloads/ComTest/ComTest/ComTest/bin/Release/ComTest.tlb
C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/RegAsm /tlb:C:/Downloads/ComTest/ComTest/ComTest/bin/Release/ComTest.tlb C:/Downloads/ComTest/ComTest/ComTest/bin/Release/ComTest.dll /CodeBase

你可能感兴趣的:(编程,c#,asp,assembly,string,interface,path)