【发布时间】:2022-01-19 05:35:28
【问题描述】:
我正在使用Robert Giesecke's Unmanaged Exports 包来访问 Excel VBA 中的 c# dll。我遵循了几个示例并继续得到运行时错误 453:“在 myDllName.dll 中找不到入口点 MyDLLFunction”
我在使用 64 位 Excel 的 64 位机器上,正在为 x64 打包 dll。
我正在使用 Visual Studio 2022 并尝试在 .NET 6.0 和 .Net Framework 4.7.2 中准备 dll。
这是我导出的类:
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();
}
}
}
然后这是我的另一个 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;
}
}
在 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
我关注了this example,但它不起作用。
我已经用谷歌搜索并测试了各种配置,但无法通过错误“找不到入口点”。
【问题讨论】:
-
没有名为“MyFunction”的函数,当您为此类事故寻求帮助时,拼写错误非常重要。照原样,没有任何迹象表明包完成了它的工作,通过在 DLL 上运行 Dumpbin.exe /exports 进行仔细检查。
-
您声明
static object MyDLLFunction(),所以默认情况下它是私有的。尝试将其更改为公开。您也可以尝试将返回类型声明为MyCsharpClass而不是object。 -
但是这里还有另一个问题:
InnerFunction返回一个Task<string>而不是一个字符串。但是没有明显的方法可以通过 P/Invoke 编组Task<string>——我当然不知道如何。如果可能的话,我不知道如何在 VBA 中等待它。有什么方法可以在c#中等待结果,并返回字符串,即public string InnerFunction(string LookupType, string LookupNumber)? -
然后我会缩小你的问题范围,只问这个问题,然后一旦你有这个工作,就更复杂的情况询问后续行动。但非托管导出甚至可能不会为 .NET Core 实现,它自 2015 年以来就没有更新。并且该包可能不再维护,请参阅github.com/3F/DllExport/issues/87#issuecomment-438576100。
标签: c# vba dllexport unmanagedexports