C# Unmanaged exports when multiple projects involved (Robert Giesecke)

我的未来我决定 提交于 2019-12-10 15:18:15

问题


I have this situation where I need to create an unmanaged DLL in .Net that can be invoked from a delphi program. I've been doing some research and I found Robert Giesecke's library (RGiesecke.DllExport). I started with a pretty simple DLL that displays a windows form with a textbox, something like this:

[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());
}

This works fine, I setup the delphi program to invoke my dll and it works just fine. The problem comes when I add a reference to another project in my solution and create an instance of an object inside that project. At that point, the delphi program stops displaying the form like it couldn't find the exported function but it doesn't throw any errors either:

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

This line:

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

is the source of my problem, as soon as I comment it everything works again. I've been looking for examples like this on google for a while, but everything I find is similar to my first piece of code. Any ideas would be really appreciated at this point, I'm starting to think it isn't possible :(.

Regards.


回答1:


I have that same problem. In my case, I trying to call a C# DLL from a Visual FoxPro application. As long as a method makes a call to an external DLL, the FoxPro application returns an error/exception.

I recommend that you follow this guide from another StackOverflow question. Basically:

  1. Create the a COM visible C# dll
  2. Register the DLL on the deploy machine using regasm


来源:https://stackoverflow.com/questions/21373901/c-sharp-unmanaged-exports-when-multiple-projects-involved-robert-giesecke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!