簡體   English   中英

Using UnmanagedExports Package [DllExport] to call C# DLL in VBA Triggers "Can't Find DLL Entry Point" Error

[英]Using UnmanagedExports Package [DllExport] to call C# DLL in VBA Triggers "Can't Find DLL Entry Point" Error

I'm using Robert Giesecke's Unmanaged Exports package to access c# dll in Excel VBA. 我遵循了幾個示例並繼續得到運行時錯誤 453:“在 myDllName.dll 中找不到入口點 MyDLLFunction”

我在使用 64 位 Excel 的 64 位機器上,並且正在為 x64 打包 dll。

我在 Visual Studio 2022 中工作,並嘗試在 .NET 6.0 和 .Net Framework 4.7.2 中准備 dll。

這是我導出的 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();
        }
    }

}

然后這里是我的另一個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

我按照這個例子,但它不工作。

我已經用谷歌搜索並測試了各種配置,但無法通過錯誤“找不到入口點”。

我相信它是固定的。 我終於能夠在 dumpbin /exports output 中看到導出的函數。 我認為需要做幾件事來糾正這個問題。 希望這對將來的某人有所幫助。

打包未更新 - 嘗試舊版本的 VS

根據 package 的使用年限,我懷疑它在 VS2022 中不合作,所以,我:

  • 在 Visual Studio 2015 中創建項目(與我使用的 VS2022 相比)
  • 從頭開始重建項目並添加對新項目的引用,而不是嘗試在 VS2015 中打開舊項目

DllExportAppDomainIsolatedTask 錯誤

然后,該項目無法構建,它一直拋出錯誤:

The "DllExportAppDomainIsolatedTask" task failed unexpectedly.

基於這個答案我:

  • 為 VS2015 安裝了 Microsoft Build Tools
  • 安裝 .NET 3.5 並手動添加 Microsoft.Build.Tasks.v3.5 作為對我的項目的引用,方法是在 .NET3.5 安裝后瀏覽到其位置

但我一直收到同樣的錯誤。 然后我通過更改設置來增加調試 output:項目>屬性>構建>錯誤和警告>警告級別為 4。

挖掘調試日志,我發現 UnmanagedExports System.ArgumentException: Requested value 'Version47' was not found.中的幾行引用了項目平台、框架路徑、庫工具路徑、工具 dll 路徑等。 System.ArgumentException: Requested value 'Version47' was not found.

我的目標是 .NET Framework 4.7.2,所以我將項目降級為目標 .NET Framework 4.5,刪除 bin/obj 文件夾內容並重建項目。 然后,運行 dumpbin /exports 顯示了我的 HelloWorld 函數!

package 似乎與 .NET 框架 4.7.2 不兼容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM