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:WINDOWSMicrosoft.NETFrameworkv2.0.50727RegAsm /tlb:ComTest.tlb ComTest.dll /CodeBase
5. 加入全局緩存中(非必須,建議不進行)
gacutil -i ComTest.dll
GetKey.bat(得到KeyFile批處理):
path E:Program FilesMicrosoft Visual Studio 8SDKv2.0in
sn -k c:keyfile.snk
RegCom.bat(生成tlb並注冊成com批處理):
path E:Program FilesMicrosoft Visual Studio 8SDKv2.0in
tlbexp C:DownloadsComTestComTestComTestinReleaseComTest.dll /out:C:DownloadsComTestComTestComTestinReleaseComTest.tlb
C:WINDOWSMicrosoft.NETFrameworkv2.0.50727RegAsm /tlb:C:DownloadsComTestComTestComTestinReleaseComTest.tlb C:DownloadsComTestComTestComTestinReleaseComTest.dll /CodeBase