无法将 C# dll 注册为 COM 组件,模块已加载但未找到入口点 DLLRegisterServer

Unable to register the C# dll as a COM Component, The module was loaded but the entry-point DLLRegisterServer was not found

我创建了一个 class 并使其可见:

[ComVisible(true)]
    [Guid("FD909333-3CD0-477F-8A7E-B8045B0B84EC")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("TestComApp.TestApp.TestClass")]
    public class TestClass:ITestCom
    {
        public int Add(int a, int b) { return a + b; }
    }

同时接口设置为COM可见:

[ComVisible(true)]
    [Guid("26567B41-15DB-4EE2-A277-357EAE96BF6A")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    interface ITestCom
    {
         int Add(int a, int b);
    }

但是当我尝试注册 DLL 时

regsvr32 /i TestComApp.dll

我收到以下错误 "The module was loaded but the entry-point DLLRegisterServer was not found"

要注册 .NET DLL,您需要使用 Regasm.exe 而不是 regsvr32.exe。 Regasm.exe 位于 C:\Windows\Microsoft.NET\Framework\v4.0.30319(或类似版本,具体取决于 .NET 版本)。

还要确保指定 /codebase 选项,或使程序集具有强名称,否则 COM 将无法找到 DLL。