2

I'm using Robert Giesecke's Unmanaged Exports package to access c# dll in Excel VBA. I've followed several examples and continue to get the run-time error 453: "can't find entry point MyDLLFunction in myDllName.dll"

I'm on a 64bit machine using 64bit Excel and am packaging the dll for x64.

I'm working in Visual Studio 2022 and have tried preparing the dll in both .NET 6.0 and .Net Framework 4.7.2.

Here's my exported class:

namespace MyNamespace
{
    public static class UnmanagedExports
    {
        //entry point for dll
        static UnmanagedExports()
        {
            //nothing
        }


        [DllExport("HelloWorld", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
        public static string HelloWorld()
        {
            return "Hello World";
        }

        [DllExport("MyDLLFunction", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
        [return: MarshalAs(UnmanagedType.IDispatch)]
        static object MyDLLFunction()
        {
            return new InnerNamespace.MyCsharpClass();
        }
    }

}

Then here is my other C#:

namespace MyNamespace.InnerNamespace
{
    [ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
    public class MyCsharpClass
{
        [return: MarshalAs(UnmanagedType.BStr)]
        public async Task<string> InnerFunction(string LookupType, string LookupNumber)
        {
            object Response = await GetResponseAsync(LookupType, LookupNumber);
            string ResultAsString = Response.ToString();
            return ResultAsString;
        }
}

In Excel VBA:

Private Declare PtrSafe Function AddDllDirectory Lib "kernel32" (ByVal lpLibFileName As String) As Integer
Public Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
Public Declare PtrSafe Function MyDLLFunction Lib "C:\my\long\path\to\my\project\bin\x64\Debug\net472\myDllName.dll" () As Object

Sub Test()
  Dim mObj As Object
  Set mscObj = MyDLLFunction()  //run time error here <- can't find entry point
End Sub

I followed this example but it's not working.

I've googled and tested out various configurations and have been unable to get past the error 'can't find entry point'.

9
  • 2
    There is no function named "MyFunction", spelling errors do matter a lot when you ask for help with a mishap like this. As-is, there is no hint that the package did its job, double-check by running Dumpbin.exe /exports on the DLL. Commented Dec 16, 2021 at 14:22
  • 1
    You declare static object MyDLLFunction() so by default it's private. Try changing it to public. You might also try declaring the return type as MyCsharpClass rather than object. Commented Dec 16, 2021 at 15:25
  • But there's another problem here: InnerFunction returns a Task<string> not a string. But there is no obvious way to marshal a Task<string> via P/Invoke -- I certainly don't know how. And if possible I wouldn't know how to await it in VBA. Is there any way to await the result in c#, and return the string, i.e. public string InnerFunction(string LookupType, string LookupNumber)? Commented Dec 16, 2021 at 15:26
  • So possibly related: How to call asynchronous method from synchronous method in C#? but also Async P/Invoke calls, Excel VBA make a script asynchronous (answer: no) and Wait for C# asynchronous function to finish executing in VBA? (never answered). Commented Dec 16, 2021 at 15:27
  • 2
    Then I'd narrow your question down to ask just about that, then once you have that working, ask a followup for the more complex case. But Unmanaged Exports may not even be implemented for .NET Core, it hasn't updated since 2015. And the package may not be maintained any more, see github.com/3F/DllExport/issues/87#issuecomment-438576100. Commented Dec 16, 2021 at 16:10

1 Answer 1

2

I believe it's fixed. I'm finally able to see exported functions in the dumpbin /exports output. There were several things that I believe needed to be done to correct the problem. Hope this helps someone in the future.

Packaged is Not Updated - Try Older Version of VS

Based on the age of the package I suspected it wasn't cooperating in VS2022 so, I:

  • Created the project in Visual Studio 2015 (vs VS2022 which I was using)
  • Rebuilt project from scratch and added references to new project rather than trying to open old project in VS2015

DllExportAppDomainIsolatedTask Error

Then, the project wouldn't build, it kept throwing the error:

The "DllExportAppDomainIsolatedTask" task failed unexpectedly.

Based on this answer I:

  • Installed Microsoft Build Tools for VS2015
  • Installed .NET 3.5 and manually added Microsoft.Build.Tasks.v3.5 as a reference to my project by browsing to its location after the .NET3.5 install

But I kept receiving the same error. I then increased the debug output by changing the setting: Project>Properties>Build>Errors and Warnings>Warning Level to 4.

Digging through the debug log I found several lines from the UnmanagedExports package which referenced the project platform, framework path, library tools path, tools dll path, etc. In that section I found: System.ArgumentException: Requested value 'Version47' was not found.

I was targeting .NET Framework 4.7.2 so I downgraded the project to target .NET Framework 4.5, deleted the bin/obj folder contents and rebuilt the project. Then, running dumpbin /exports showed my HelloWorld function!

It doesn't appear the package is compatible with .NET Framework 4.7.2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.