【问题标题】:C# DLL using DllExport: No entry point when called in VBA使用 DllExport 的 C# DLL:在 VBA 中调用时没有入口点
【发布时间】:2014-05-01 19:58:19
【问题描述】:

为避免要求为电子表格的所有用户注册 Dll,我尝试使用后期绑定,以便用户无需添加对 Dll 的引用。

我已经使用 Visual Studio 在 C# 中创建了 Dll,尽管我已经包含了“使用 RGiesecke.DllExport;”。并在函数上使用 DllExport 返回包含我需要在 VBA 中访问的函数的对象,我仍然收到错误“运行时错误'453':无法在 C 中找到 DLL 入口点 CreateDotNetObject :\temp\MyFunctions.dll."

DLL代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Data;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Client;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;
using System.Collections.ObjectModel;
using System.Threading;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework;
using Microsoft.TeamFoundation.Common;
using Microsoft.VisualBasic;
using System.Diagnostics;
using RGiesecke.DllExport;

namespace MyFunctions
{
    public interface IMyFunctions
    {
        string GetWorkItemLinkList(string WIIDs);
    }

    [CLSCompliant(true), ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
    public class MyFunctions : IMyFunctions
    {
        TfsConfigurationServer server;
        WorkItemStore store;

        private void TFSconnect()
        {
        //Code to connect
        }

        [CLSCompliant(true), ComVisible(true), Description("GetWorkItemLink func")]
        public string GetWorkItemLink(int WIID)
        {
            TFSconnect();

        //Code to build return string "message"

            return message;
        }



        [CLSCompliant(true), ComVisible(true), Description("GetWorkItemLinkList func")]
        public string GetWorkItemLinkList(string WIIDs)
        {
            TFSconnect();

        //Code to build return string "returnStr"

            return returnStr;
        }


    }
    static class UnmanagedExports
    {
        [DllExport]
        [return: MarshalAs(UnmanagedType.IDispatch)]
        static Object CreateDotNetObject()
        {
            return new MyFunctions();
        }
    }
}

VBA中的声明如下:

Private Declare Function CreateDotNetObject Lib "c:\temp\MyFunctions.dll" () As Object

但是当我尝试实例化一个对象时,我得到了上面提到的错误。

Sub test()

    Dim o As Object
    Set o = CreateDotNetObject()

End Sub

这是我第一次尝试在 Excel 中使用自定义 dll 函数而不在 VBA 中添加引用。如果我添加引用(早期绑定),这些函数确实可以工作,但是 DLL 不会传播给使用电子表格的每个人,我需要它在他们运行正常函数时不会崩溃。

编辑:附加信息。我刚刚注意到,除了 DLL,当我在 Visual Studio 中构建解决方案时,我还得到一个“Object FileLibrary”和一个“Exports Library File”。当我注册 DLL 时,我应该对 .exp 或 .lib 做什么?

谢谢,

迈克

【问题讨论】:

  • 我只使用 c++ 而不是 c# 让它工作。如果有帮助,我可以发布该代码吗?
  • 据我了解,在 C++ 中,您可以使用 __declspec( dllexport ) 声明符,它更易于使用,但 RGiesecke 的 DLLExport 是 C# 的解决方法。我可能可以用 C++ 重写 DLL,但是由于如果我在 Excel 中包含引用,这些函数就可以正常工作,我希望我的代码中存在一些小的语法问题,这可能比用 C++ 重写更容易。但也许它可能会有所帮助。

标签: c# excel dll dllexport entrypointnotfoundexcept


【解决方案1】:

我正在构建解决方案,将类库属性中的平台目标设置为“任何 PC”,这显然不允许导出。当我将它切换到“x86”时,它完全可以工作。

【讨论】:

  • 很高兴你解决了它。与 Excel 集成是一个真正的痛苦,尤其是部署。对于不需要实际 Excel 函数的东西,我倾向于在 .Net 中编写一个小应用程序,然后使用 Excel VBA 编写 XML 输入文件,通过 shell 启动应用程序,然后写入 XML 输出文件,然后被检索到 VBA 中。这样,我们就不必实际安装任何东西。
猜你喜欢
  • 2022-01-19
  • 2019-07-02
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多