我使用Unmanaged Exports從.NET .dll創建一個本地.dll,這樣我就可以在沒有COM註冊的情況下從Delphi訪問.NET代碼。FoxPro和.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中定義的界面同樣地,我是如何做到的德爾福?如果沒有,我可以從FoxPro使用這個.dll嗎?我使用Visual FoxPro 9.0 SP2。謝謝。
Rick Strahl在VFP COM Interop上有一篇很好的文章:http://www.west-wind.com/presentations/VfpDotNetInterop/DotNetFromVFP.asp – 2010-03-01 21:43:00
@Brian:我知道Rick的頁面。我想避免COM註冊,但它似乎不可能在FoxPro中。 – 2010-03-02 14:19:43