1
votes

I have VS 2015 and want to create a VB.NET DLL to be called from VBA.

When I try to compile and get the DLL, I get "Impossible to write assembly: access denied, make sure you have admin rights" (approximate translation) ==> a compilation error

However, amazingly enough, the DLL does get created, but cannot be linked from VBA.

How can I still do it? I do not have access to admin rights, workspace constraints...

I tried :

VB.NET code is dummy, to check the link :

Public Class main
    Public Function plus(x#) As Double
        plus = x + 1
    End Function
End Class

PS: the error:

1>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(4335,5): error MSB3216: Impossible d'inscrire l'assembly "D:\Full_Path\bin\Debug\LibName.dll" - accès refusé. Assurez-vous que vous exécutez l'application en tant qu'administrateur. L'accès à la clé de Registre 'HKEY_CLASSES_ROOT\LibName.main' est refusé.

1>Exécution de la tâche "RegisterAssembly" terminée -- ÉCHEC.

1>Génération de la cible "UnmanagedRegistration" terminée dans le projet "LibName.vbproj" -- ÉCHEC.

translation:

1>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(4335,5): error MSB3216: Impossible to write assembly "D:\Full_Path\bin\Debug\LibName.dll" - access denied. Make sure you run application as administrator. Acces to registry key 'HKEY_CLASSES_ROOT\LibName.main' denied.

Execution of task "RegisterAssembly" finished -- FAIL.

Generation of task "UnmanagedRegistration" finished in project "LibName.vbproj" -- FAIL.

1
Make sure you can open visual studio with admin rights, if not then try changing the location of your project. e.g Use your user profile folder or even the desktop to make sure visual studio can write to the path.Pure
That's the point. I do not have admin rights. Workspace... :-( I'm modifying the postPierre
just press the Rebuild button multiple times. I'm doing that since VS reports me the same when building my WPF app on a WPF project folder located on my desktop.Radinator
Does this link help? stackoverflow.com/questions/1170794/…djv
@Verdolino Thanks but not much either... This is C#Pierre

1 Answers

2
votes

Maby this is helping you: To make your VB.NET class libary accessable from a VBA project, mark your class with the <System.Runtime.InteropServices.ComVisible(True)>-Attribute.

In your VBA project, add the following references:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoree.tlb (Common Language Runtime Execution Engine 2.4 Library)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb

And in your VBA code (to "import" the libary):

Sub DLLTest()
    ' You need this to execute functions of the CLR
    Dim clr As mscoree.CorRuntimeHost

    ' Offers you the ability to load functions from the DLL
    Dim domain As mscorlib.AppDomain

    ' Variable to store a instance of your class
    Dim objTest As Object

    ' Create a new RuntimeHost
    Set clr = New mscoree.CorRuntimeHost

    ' Start the RuntimeHost
    clr.Start

    ' Get the default AppDomain
    clr.GetDefaultDomain domain

    ' Create a instance of your DLL class
    ' dll_path is the path pointing to the DLL (can be UNC path)
    Set objTest = domain.CreateInstanceFrom(dll_path, "main").Unwrap

    ' Test 
    Dim result As Double
    result = objTest.plus 1

    ' Delete the reference
    Set objTest = Nothing

    ' Stop the RuntimeHost 
    clr.Stop
End Sub

Credit:

I got the code from here: https://www.vb-paradise.de/index.php/Thread/87001-NET-Dll-in-VBA-ohne-COM-Registrierung-nutzen/

Edit: Here, this code works on my pc

Imports System.Runtime.InteropServices

<ComVisible(True)>
Public Class Main
    Public Function Add(value As Double) As Double
        Return value + 1
    End Function
End Class

Target Framework .NET Framework 3.0

VBA-Editor: Added reference to the *.tbl files and used this code:

Sub DLLTest()
    ' You need this to execute functions of the CLR
    Dim clr As mscoree.CorRuntimeHost

    ' Offers you the ability to load functions from the DLL
    Dim domain As mscorlib.AppDomain

    ' Create a new RuntimeHost
    Set clr = New mscoree.CorRuntimeHost

    ' Start the RuntimeHost
    clr.Start

    ' Get the default AppDomain
    clr.GetDefaultDomain domain

    ' Variable to store a instance of your class
    Dim objTest As Object

    ' Create a instance of your DLL class
    ' dll_path is the path pointing to the DLL (can be UNC path)
    Set objTest = domain.CreateInstanceFrom("C:\TestDll.dll", "TestDll.Main").Unwrap()

    ' Test
    Dim result As Double
    result = objTest.Add(1)
    MsgBox result

    ' Delete the reference
    Set objTest = Nothing

    ' Stop the RuntimeHost
    clr.Stop
End Sub

Placed the dll file on C:\ Combining this with a button and after pressing it, a MessageBox appears that tells me the result (input + 1)