Fab*_*ujo 0 delphi dll variant delphi-2010
我想将内部对象的一些功能公开为DLL - 但该功能使用变体.但我需要知道:我可以使用Variant参数导出函数和/或返回 - 或者更好地转到仅字符串表示形式?
什么是更好的,从语言无关的POV(消费者不是用Delphi制作 - 但所有将在Windows中运行)?
您可以使用OleVariant,它是COM使用的变量值类型.确保不将其作为函数结果返回,因为stdcall和复杂的结果类型很容易导致问题.
一个简单的示例库DelphiLib;
uses
SysUtils,
DateUtils,
Variants;
procedure GetVariant(aValueKind : Integer; out aValue : OleVariant); stdcall; export;
var
doubleValue : Double;
begin
case aValueKind of
1: aValue := 12345;
2:
begin
doubleValue := 13984.2222222222;
aValue := doubleValue;
end;
3: aValue := EncodeDateTime(2009, 11, 3, 15, 30, 21, 40);
4: aValue := WideString('Hello');
else
aValue := Null();
end;
end;
exports
GetVariant;
Run Code Online (Sandbox Code Playgroud)
如何从C#中消费:
public enum ValueKind : int
{
Null = 0,
Int32 = 1,
Double = 2,
DateTime = 3,
String = 4
}
[DllImport("YourDelphiLib",
EntryPoint = "GetVariant")]
static extern void GetDelphiVariant(ValueKind valueKind, out Object value);
static void Main()
{
Object delphiInt, delphiDouble, delphiDate, delphiString;
GetDelphiVariant(ValueKind.Int32, out delphiInt);
GetDelphiVariant(ValueKind.Double, out delphiDouble);
GetDelphiVariant(ValueKind.DateTime, out delphiDate);
GetDelphiVariant(ValueKind.String, out delphiString);
}
Run Code Online (Sandbox Code Playgroud)