3

我使用Unmanaged Exports从 .NET .dll 创建本机 .dll,这样我就可以从 Delphi 访问 .NET 代码而无需 COM 注册。

例如,我有这个 .NET 程序集:

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

当我在 Delphi 中定义相同的接口时,我可以轻松地使用 .NET 对象:

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.

有关详细信息,请参阅我的Delphi 问题和答案

我的问题:
在 FoxPro 中是否可能发生这样的事情?我已经尝试了以下失败并出现数据类型不匹配错误在线createdotnetadder(@ldnw)

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

我可以像在 Delphi 中那样在 FoxPro 中定义接口吗?如果没有,我可以使用 FoxPro 中的这个 .dll 吗?我使用 Visual FoxPro 9.0 SP2。谢谢。

4

2 回答 2

1

似乎最简单的方法是使用 COM 注册。另一种方法是手动托管 CLR。Rick Strahl 在 FoxPro 发表了一篇关于如何做到这一点的文章:

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

于 2010-03-02T23:05:57.580 回答
0

您还可以使用开源wwDotnetBridge 项目,该项目为您自动执行 CLR 运行时托管过程,并提供许多其他支持功能,使在 FoxPro 中使用 .NET 类型和结构变得更加容易。

loBridge = CREATEOBJECT("wwDotnetBridge","V4")
loBridge.LoadAssembly("MyAssembly.dll")
loInstance = loBridge.CreateInstance("MyNamespace.MyClass")

loInstance.DoSomething(parm1)
loBridge.InvokeMethod(loInstance,"SomeOtherMethodWithUnsupportedTypeParms",int(10))

wwDotnetBridge 为您处理对象创建并像本机 COM 互操作一样将 COM 实例传回,但它提供了其他功能,否则无法通过 COM 互操作访问:

  • 访问静态方法和成员
  • 访问值类型
  • 支持更新数组和集合
  • 支持重载方法和构造函数
  • 访问泛型类型

和许多帮助程序让您解决所提供的 COM->.NET 映射中的限制。

于 2017-03-17T22:56:01.967 回答