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

Amy
  • 165
  • 1
  • 10
  • 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. – Hans Passant Dec 16 '21 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`. – dbc Dec 16 '21 at 15:25
  • But there's another problem here: `InnerFunction` returns a `Task` not a string. But there is no obvious way to marshal a `Task` 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)`? – dbc Dec 16 '21 at 15:26
  • So possibly related: [How to call asynchronous method from synchronous method in C#?](https://stackoverflow.com/q/9343594/3744182) but also [Async P/Invoke calls](https://stackoverflow.com/q/4051364/3744182), [Excel VBA make a script asynchronous](https://stackoverflow.com/q/24574884/3744182) (answer: no) and [Wait for C# asynchronous function to finish executing in VBA?](https://stackoverflow.com/q/52426895/3744182) (never answered). – dbc Dec 16 '21 at 15:27
  • @HansPassant Good catch on the function name typo - it's corrected in the OP. Dumpbin shows no functions, only displays (Summary 2000 .rsrc 6000 .text) – Amy Dec 16 '21 at 15:35
  • @dbc I added public to `MyDLLFunction()` and changed the return type to `MyCsharpClass` . dumpbin still shows no exported headers. I agree the async may cause problems, hoping to at least connect to the function `MyDLLFunction` and then work through any async issues. – Amy Dec 16 '21 at 15:42
  • Is export of `HelloWorld` working? – dbc Dec 16 '21 at 15:49
  • @dbc `HelloWorld` doesn't appear to be exporting. Dumpbin /exports shows only `Summary 2000 .rsrc 6000 .text` – Amy Dec 16 '21 at 15:54
  • 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 https://github.com/3F/DllExport/issues/87#issuecomment-438576100. – dbc Dec 16 '21 at 16:10

1 Answers1

1

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.

Amy
  • 165
  • 1
  • 10