未经注册的FoxPro和.NET COM
我使用从.NET.dll创建本机.dll,这样我就可以从Delphi访问.NET代码,而无需COM注册 例如,我有一个.NET程序集:未经注册的FoxPro和.NET COM,.net,interface,com,visual-foxpro,foxpro,.net,Interface,Com,Visual Foxpro,Foxpro,我使用从.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)] [I
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)
行出现数据类型不匹配错误时失败:
我可以在FoxPro中定义接口吗?我在Delphi中是如何定义接口的?如果没有,我可以从FoxPro使用这个.dll吗?我使用visualfoxpro9.0sp2。谢谢。似乎最简单的方法就是使用COM注册。另一种方法是手动托管CLR。Rick Strahl在FoxPro上发表了一篇关于如何做到这一点的博文:
您还可以使用开源软件,它为您自动化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映射中的限制。Rick Strahl在这里有一篇关于VFP COM互操作的好文章:@Brian:我知道Rick的页面。我想避免COM注册,但在FoxPro中这似乎是不可能的。不幸的是,CLR主机对我不起作用。NET代码以内部方式使用线程,我在退出foxpro应用程序时遇到了加载程序锁异常。那么,您可能希望在进程外托管CLR。您在Q中提到,您让Delphi/UnmangedExports正常工作-是在进程内运行还是进程外运行?Delphi正在运行,但您必须调整浮点异常处理。不管怎么说,我已经转到了COM,虽然我担心最终用户计算机上的注册问题,但这更容易。如果你遇到加载程序锁异常,它们是由于未发布的线程造成的。您需要确保在关闭FoxPro应用程序之前关闭线程,否则VFP将在关闭时崩溃。这是标准配置,实际上与COM互操作无关-在使用线程并运行优秀线程的.NET或Win32应用程序中也会发生同样的情况。
loBridge = CREATEOBJECT("wwDotnetBridge","V4")
loBridge.LoadAssembly("MyAssembly.dll")
loInstance = loBridge.CreateInstance("MyNamespace.MyClass")
loInstance.DoSomething(parm1)
loBridge.InvokeMethod(loInstance,"SomeOtherMethodWithUnsupportedTypeParms",int(10))