【问题标题】:Exported functions from C# DLL not working从 C# DLL 导出的函数不起作用
【发布时间】:2019-08-12 21:53:52
【问题描述】:

我必须在 C# 中从我的 DLL 中导出 3 个基本方法,以便在 C++ 中可以访问它:

  • OnPluginStart
  • OnPluginStop
  • 插件更新

所以我发现 Unmanaged Exports 是一个不错的 C# 库,它使这变得更容易。

所以我继续使用示例代码进行测试:

using System;
using System.IO;
using RGiesecke.DllExport;

namespace Plugins
{
    public class Plugins
    {
        [DllExport("OnPluginStart", CallingConvention = CallingConvention.StdCall)]
        public static void OnPluginStart()
        {
            using (var file = new StreamWriter(@"pluginLog.txt", true))
            {
                file.WriteLine("OnPluginStart");
            }
        }

        [DllExport("OnPluginStop", CallingConvention = CallingConvention.StdCall)]
        public static void OnPluginStop()
        {
            using (var file = new StreamWriter(@"pluginLog.txt", true))
            {
                file.WriteLine("OnPluginStop");
            }
        }

        [DllExport("PluginUpdate", CallingConvention = CallingConvention.StdCall)]
        public static void PluginUpdate(float dt)
        {
            using (var file = new StreamWriter(@"pluginLog.txt", true))
            {
                file.WriteLine("PluginUpdate");
            }
        }
    }
}

但是,当我编译我的 DLL 并使用 DLL Exporter Viewer 时,它不会列出任何导出的函数,并且加载 DLL 的应用程序也不会运行我的插件。

我在这里做错了什么导致我的函数根本没有被导出?

【问题讨论】:

  • @DavidHeffernan 所以你是说 DLL Exporter Viewer 显示所有 3 个函数?
  • 我是说非托管出口对我来说很好用。所以你正在做一些不同的事情。我们采取哪些步骤来重现?
  • 他叫罗伯特。我担心这表明缺乏对细节的关注。也许您没有仔细按照说明进行操作。

标签: c# c++ dllexport


【解决方案1】:

除了您发布的代码无法编译之外,您的代码运行良好。您省略了 using System.Runtime.InteropServices 行。您的(固定)代码的 x86 类库构建的 Dependency Walker 表示:

问题的最明显原因可能是来自NuGet page for the library:

您必须将平台目标设置为 x86、ia64 或 x64。 AnyCPU 程序集无法导出函数。

【讨论】:

  • 我已经尝试了 x86 和 x64,但查看器仍然没有为我列出这些功能。
  • 在这里工作正常。您没有查看正确的 DLL。或类似的东西。退后一步,慢慢看看你在做什么。确保您查看的 DLL 具有表明您刚刚构建它的修改时间。也许您在最初构建的 AnyCPU DLL 中查找错误的文件夹。此外,不发布真实代码令人失望。
  • 构建 > 清洁解决方案然后构建 > 构建解决方案并证明自己我在查看器上的正确 DLL 中并且没有任何内容:/ 关于互操作,我确实在复制粘贴时错过了它。
  • 你做错了什么。我敢打赌,您正在查看错误的文件。也许您没有创建类库?它确实有效。我不能告诉你你做错了什么,因为你没有提供重现的步骤。
  • 使用依赖 walker 我确实看到了这些,但也收到了这条消息 Warning: At least one delay-load dependency module was not found. Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module. 是的,它是一个类库!
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多