如何从脚本构建 COM 可见 DLL?

How to build a COM visible DLL from script?

我正在尝试从脚本构建一个简单的 VB.NET COM 可见 DLL(我希望它无需注册即可访问)。 vb 文件非常简单:

Imports System.Runtime.InteropServices
Imports RGiesecke.DllExport

Public Module Test
    <DllExport("add")> _
    Public Function TestExport(left As Integer, right As Integer) As Integer
        Return left + right
    End Function
End Module

当通过 Visual Studio 构建 ut 并尝试从 Excel 访问 DLL 时:

Public Declare Function add Lib "C:\...\VBLib.dll" (left As Integer, right As Integer) As Integer

它有效并提供正确的结果。但是,当我尝试使用脚本组装 DLL 时出现错误:Can't find DLL entry point.

这是我正在使用的脚本:

  1. 首先我运行 VBC编译器:

vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Data,System.Diagnostics,System.Linq,System.Xml.Linq,System.Threading.Tasks /optioncompare:Binary /optionexplicit+ /optionstrict:custom /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optioninfer+ /nostdlib /platform:x86 /rootnamespace:VBLib /sdkpath:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5" /highentropyva+ /reference:"C:\...RGiesecke.DllExport.Metadata.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.DataSetExtensions.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /out:"C:\...\VBLib.dll" /subsystemversion:6.00 /target:library Class1.vb

  1. 那我运行ildasm.exe:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\ildasm.exe" /quoteallnames /unicode /nobar /out:"C:\...\VBLib.il" "C:\...\VBLib.dll"

  1. 最后我运行ILAsm.exe:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\ILAsm.exe" /nologo /out:"C:\...\VBLib.dll" "C:\...\VBLib.il" /DLL /resource="C:\...\VBLib.res" /optimize

我从 Visual Studio 构建(详细视图)几乎逐字逐句地复制了上述过程,它们都 运行 w/o 错误生成了一个 DLL。但是,我仍然收到上述错误。 无论如何,如果有一个更短且有效的脚本可以从单个 VB.NET 文件提供 COM 可见的 DLL,我将不胜感激!

注意:文件路径已经缩短...

通过 this article 的 ExportDLL.exe 文件找到了一种方法。 我需要做的就是:

  1. 修改源码如下图

  2. ExportDllAttribute.dll 导入项目以使用 ExportDLL 属性

  3. 如上所述用VBC.exe构建DLL

  4. 使用 ExportDLL.exe 通过以下方式导出 DLL:

ExportDll.exe full_path_to_yours_dll\yoursdll.dll

修改后的源代码:

Imports System.Runtime.InteropServices
Imports ExportDllAttribute

    Public Module Test
        <ComVisible(True)> _
        <ExportDll("TestExport", System.Runtime.InteropServices.CallingConvention.Cdecl)> _
        Public Function TestExport(left As Integer, right As Integer) As Integer
            Return left + right
        End Function

End Module