涉及多个项目时的 C# 非托管导出 (Robert Giesecke)

标签 c# export unmanaged

在这种情况下,我需要在 .Net 中创建一个可以从 delphi 程序调用的非托管 DLL。我一直在做一些研究,并找到了 Robert Giesecke 的库 (RGiesecke.DllExport)。我从一个非常简单的 DLL 开始,它显示带有文本框的 Windows 窗体,如下所示:

[ComVisible(true)]
[DllExport("PlaceOrder", CallingConvention = CallingConvention.StdCall)]
public static IntPtr PlaceOrder(IntPtr lnpInXml)
{
    string inputXml = Marshal.PtrToStringAnsi(lnpInXml);
    StringBuilder sbOutputXml = new StringBuilder();

    Form1 pti = new Form1(inputXml, sbOutputXml);
    pti.ShowDialog();

    return Marshal.StringToHGlobalAnsi(sbOutputXml.ToString());
}

这很好用,我设置了 delphi 程序来调用我的 dll,它工作得很好。当我在我的解决方案中添加对另一个项目的引用并在该项目中创建一个对象的实例时,问题就来了。那时,delphi 程序停止显示表单,就像它找不到导出的函数一样,但它也没有抛出任何错误:

using MyCommonCode;

namespace UnmanagedDLLTest
{
    [ComVisible(true)]
    public static class UnmanagedDLL
    {
        [ComVisible(true)]
        [DllExport("PlaceOrder", CallingConvention = CallingConvention.StdCall)]
        public static IntPtr PlaceOrder(IntPtr lnpInXml)
        {
            string inputXml = Marshal.PtrToStringAnsi(lnpInXml);
            StringBuilder sbOutputXml = new StringBuilder();

            Form1 pti = new Form1(inputXml, sbOutputXml);
            pti.ShowDialog();

            MyCommonCode.MyClass mc = new MyCommonCode.MyClass();

            return Marshal.StringToHGlobalAnsi(sbOutputXml.ToString());
        }
    }
}

这一行:

MyCommonCode.MyClass mc = new MyCommonCode.MyClass();

是我问题的根源,一旦我评论它,一切都会恢复正常。一段时间以来,我一直在 google 上寻找这样的示例,但我发现的所有内容都与我的第一段代码相似。在这一点上,任何想法都会非常感激,我开始认为这是不可能的:(。

问候。

最佳答案

我也有同样的问题。在我的例子中,我尝试从 Visual FoxPro 应用程序调用 C# DLL。只要方法调用外部 DLL,FoxPro 应用程序就会返回错误/异常。

我推荐你follow this guide from another StackOverflow question .基本上:

  1. 创建一个 COM 可见的 C# dll
  2. 使用 regasm 在部署机器上注册 DLL>

关于涉及多个项目时的 C# 非托管导出 (Robert Giesecke),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21373901/

相关文章:

c# - 使用 Razor 将字符串转换为 `cshtml` 文件中的日期时间?

c# - 在此上下文中仅支持原始类型或枚举类型。链接到 SQL

module - ES6 导出对象的所有值

c# - Xamarin.Forms 中的 Application.Current Null 异常

c# - 从 Azure 表捕获错误的简洁方法(除了字符串匹配?)

excel - 如何将一些矩阵写入/导出到 XLS 文件?

jquery - Kendo UI 在.net 中导出为 PDF 多页

.net - 如何使用硬件加速将 .NET 中的 Half 转换为 Single?

asp.net - 具有非托管依赖项的 64 位托管程序集未在 IIS/ASP.NET MVC 4 中加载

c# - 管理托管 (C#) 和非托管 (C++) 对象的析构函数