代码之家  ›  专栏  ›  技术社区  ›  Lukas Cenovsky

未注册的FoxPro和.NET COM

  •  3
  • Lukas Cenovsky  · 技术社区  · 16 年前

    我用 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 question and answers 详情。

    我的问题:
    在FoxPro中是否可能出现这种情况?我尝试过以下方法,但失败了 数据类型不匹配 联机错误 createdotnetadder(@ldnw) :

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

    我能在FoxPro中定义接口吗?我在Delphi中是怎么定义的?如果没有,我可以使用FoxPro的.dll吗?我使用Visual FoxPro 9.0 SP2。谢谢。

    2 回复  |  直到 9 年前
        1
  •  1
  •   Brian Vander Plaats    16 年前

    看起来最简单的方法是使用COM注册。另一种方法是手动承载CLR。Rick Strahl在FoxPro上有一篇关于如何做到这一点的博文:

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

        2
  •  0
  •   Rick Strahl    9 年前

    您也可以使用开放源码 wwDotnetBridge project 它为您自动化了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映射中的限制的帮助程序。