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

时间:2015-10-25 10:42:57

标签: .net vb.net dll com

我正在尝试从脚本构建一个简单的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构建并尝试从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编译器:
  2. 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
    2. "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
      2. "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 Build(详细视图)逐字复制上述过程,并且它们都运行无错误生成DLL。但是,我仍然得到上面的错误。 在任何情况下,如果有一个较短的工作脚本从单个VB.NET文件提供COM可见的DLL,我将非常感谢!

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

1 个答案:

答案 0 :(得分:0)

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

  1. 修改源代码,如下所示

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

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

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

  5. 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