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.