0

我想将内部对象的某些功能公开为 DLL - 但该功能使用变体。但我需要知道:我可以使用 Variant 参数导出函数和/或返回 - 还是更好地使用纯字符串表示?

与语言无关的 POV 有什么更好的(消费者不是用 Delphi 制造的,但都将在 Windows 中运行)?

4

2 回答 2

6

您可以使用 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;

如何从 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);
}
于 2009-11-03T14:29:01.080 回答
0

据我所知,在其他语言中使用 Variant 变量类型没有问题。但是,如果您为不同的变量类型导出相同的函数,那就太好了。

于 2009-11-03T13:33:52.953 回答