繁体   English   中英

FoxPro和.NET COM没有注册

[英]FoxPro and .NET COM without registration

我使用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)

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

似乎最简单的方法是使用COM注册。 另一种方法是手动托管CLR。 Rick Strahl有一篇关于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 Interop访问:

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

和许多助手让你解决COM中提供的.NET映射限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM