我用 不受管理的出口 从 .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.

看我的 德尔福问答 了解详情。

我的问题:
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。谢谢。

有帮助吗?

解决方案

看来,最简单的方法是用活COM注册。的另一种方法是手动承载CLR。里克施特拉尔对如何做到这一点从FoxPro广泛的职位:

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

其他提示

您还可以使用开源 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 映射中的限制。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top