1

I looked at other posts and couldn't find the solution.

I am trying to use C# dll I created in VBA code without having to add reference.

In my VBA code, I declared:

Public Declare Function message Lib "path_to_my_dll" _
 (ByVal message As String) As String


Sub Test()

Dim hello As String

    hello = message("hi!!")
    Debug.Print hello

End Sub

I get an error saying entry point for my dll couldn't be found.

The C# Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace DLLImport
{
    public class Class1
    {
        [DllImport("DLLImport", EntryPoint = "Run")]
        extern string Run(string message)
        {
            return message;
        }
    }
}

Thank you in advance for your help!!

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Jung
  • 219
  • 3
  • 10
  • You have to use Interface to be able to use *.dll in VBA. I'll find my past answer and post a link to it. ===EDIT=== Got it! [Link](https://stackoverflow.com/questions/29563448/generics-and-com-visible-net-libraries/29565409#29565409) – Maciej Los Feb 07 '19 at 21:09
  • 2
    This is a `DllExport`, not a `DllImport`. VBA will also only use the StdCall calling convention. I've run across claims that this is possible to do from an unmanaged c++ caller, but have never attempted it. – Comintern Feb 07 '19 at 21:22
  • No `Dll-Export-Attribute` is available with c# yet, but you can try [this Nuget package](http://blog.gapotchenko.com/eazfuscator.net/native-dll-exports-from-dotnet-assembly). – Daniel Dušek Feb 07 '19 at 22:42

1 Answers1

0

You might want to use InteropServices to make a COM Visible DLL

using System.Runtime.InteropServices;   

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("your-GUID-1")]
public interface _Visible_Methods
{
    //--------< _Visible_Methods >--------

    //*visible COM Methods of this Control under Office,Excel, Word

    string get_Hello();

    //--------</ _Visible_Methods >--------
}

Source: https://codedocu.com/Net-Framework/Controls/COM-ActiveX/Create-C_hash_-COM-Control-for-Office?2382

Adas
  • 404
  • 2
  • 19
  • Hi Thank you for the help. But this method still requires adding reference in the VBA editor. I am trying to import DLL directly from the absolute path in local drive and call the function. – Jung Feb 08 '19 at 03:30