0

I am trying to build a simple VB.NET COM-visible DLL (I want it to be accessible without registering it) from script. The vb file is simple as hell:

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

When buliding ut via Visual Studio and trying to access the DLL from Excel:

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

it works and provide correct results. However, when I tried assembly the DLL with a script I get the error: Can't find DLL entry point.

Here is the script I am using:

  1. First I run VBC compiler:

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. Then I run 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. And lastly I run 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

I copied the above procedures almost word for word from Visual Studio Build (Detailed view) and they all run w/o errors producing a DLL. However, I still get the error above. In any case if there is a shorter and working script to provide a COM-visible DLL from a single VB.NET file I would much appreciate!

Note: the file paths have been shortened ...

AnalystCave.com
  • 4,884
  • 2
  • 22
  • 30
  • maybe the [ _](https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comvisibleattribute(v=vs.100).aspx) attribute – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 25 '15 at 11:20
  • 2
    This does not have anything to do with COM. Gieseke's utility is an IL rewriting tool. You went through all the steps, except taking care of the rewriting. You have to use the undocumented .export directive. Very little point to that, it is not "better" in any way. If you want to do this in one step without build or rewriting hacks then you have to use the [C++/CLI language](http://stackoverflow.com/a/17131801/17034). – Hans Passant Oct 25 '15 at 13:53
  • Could you share a link as to how to use the `export` directive in this case? – AnalystCave.com Oct 25 '15 at 15:39
  • Thanks @HansPassant! You were right in your statement. Although I missed the details and any links. I managed to overcome the obstacles with another solution - read my answer below. – AnalystCave.com Oct 25 '15 at 20:15

1 Answers1

0

Found a way to do it via the ExportDLL.exe file from this article. All I needed to do was:

  1. Modify the source code as shown below and

  2. Import the ExportDllAttribute.dll to the project to use the ExportDLL attrib

  3. Build the DLL with VBC.exe as mentioned above

  4. Export the DLL using ExportDLL.exe via:

ExportDll.exe full_path_to_yours_dll\yoursdll.dll

Modified sourcecode:

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
AnalystCave.com
  • 4,884
  • 2
  • 22
  • 30