【问题标题】:In VBA, C# dll says there is no entry point在 VBA 中,C# dll 说没有入口点
【发布时间】:2019-07-02 02:22:08
【问题描述】:

我查看了其他帖子,找不到解决方案。

我正在尝试使用我在 VBA 代码中创建的 C# dll,而无需添加引用。

在我的 VBA 代码中,我声明:

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

我收到一条错误消息,提示找不到我的 dll 的入口点。

C# 代码:

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;
        }
    }
}

提前感谢您的帮助!!

【问题讨论】:

  • 您必须使用接口才能在 VBA 中使用 *.dll。我会找到我过去的答案并发布一个链接。 ===编辑=== 明白了! Link
  • 这是DllExport,而不是DllImport。 VBA 也将只使用 StdCall 调用约定。我遇到过声称可以通过非托管 c++ 调用者来做到这一点,但从未尝试过。
  • 没有Dll-Export-Attribute 可用于c#,但您可以尝试this Nuget package

标签: c# excel vba dll


【解决方案1】:

您可能希望使用 InteropServices 来制作 COM 可见 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 >--------
}

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

【讨论】:

  • 您好,感谢您的帮助。但是这种方法仍然需要在 VBA 编辑器中添加引用。我正在尝试直接从本地驱动器中的绝对路径导入 DLL 并调用该函数。
猜你喜欢
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多