views:

203

answers:

2

I use Unmanaged Exports to create a native .dll from .NET .dll so I can access .NET code from Delphi without COM registration.

For example I have this .NET assembly:

using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace DelphiNET
{
   [ComVisible(true)]
   [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
   [Guid("ACEEED92-1A35-43fd-8FD8-9BA0F2D7AC31")]
   public interface IDotNetAdder
   {
      int Add3(int left);
   }

   [ComVisible(true)]
   [ClassInterface(ClassInterfaceType.None)]
   public class DotNetAdder : DelphiNET.IDotNetAdder
   {
      public int Add3(int left)
      {
         return left + 3;
      }
   }

   internal static class UnmanagedExports
   {
      [DllExport("createdotnetadder", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
      static void CreateDotNetAdderInstance([MarshalAs(UnmanagedType.Interface)]out IDotNetAdder instance)
      {
         instance = new DotNetAdder();
      }
   }
}

When I define the same interface in Delphi, I can easily use the .NET object:

type
  IDotNetAdder = interface
  ['{ACEEED92-1A35-43fd-8FD8-9BA0F2D7AC31}']
    function Add3(left : Integer) : Integer; safecall;
  end;

procedure CreateDotNetAdder(out instance :  IDotNetAdder); stdcall;
  external 'DelphiNET' name 'createdotnetadder';

var
  adder : IDotNetAdder;
begin
  try
   CreateDotNetAdder(adder);
   Writeln('4 + 3 = ', adder.Add3(4));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

See my Delphi question and answers for details.

My question:
Is something like this possible in FoxPro? I've tried the following which fails with Data type mismatch error on line createdotnetadder(@ldnw):

DECLARE createdotnetadder IN DelphiNET.dll object @ ldnw
ldnw = 0
createdotnetadder(@ldnw)
loObject = SYS(3096, ldnw)
? loObject.Add3(4)

Can I define interface in FoxPro similarly how I did it in Delphi? If not, can I use this .dll from FoxPro at all? I use Visual FoxPro 9.0 SP2. Thanks.

A: 

It seems that the simplest approach is to live with COM Registration. The alternate approach is to manually host the CLR. Rick Strahl has an extensive post on how do do this from FoxPro:

http://www.west-wind.com/wconnect/weblog/ShowEntry.blog?id=631

Brian Vander Plaats
Unfortunately, CLR hosting does not work for me. The .NET code intesively use threads and I got Loader Lock exception when quiting the foxpro app.
Lukas Cenovsky
Well then you probably want to host the CLR out of process. You mentioned in your Q that you got Delphi/UnmangedExports working - is that running inprocess or out of process?
Brian Vander Plaats
Delphi was in process but you had to tweak floating point exception handling. Anyway, I've moved to COM - it is easier although I am afraid of registration problems on end user computers.
Lukas Cenovsky
A: 

Hi Robert. I'm making a project in which I want to you use your Unmanaged Exports. So can you please explain me your example more precisely (How is everything used).

Thanks in advance

Nikola Kocovski