0

内部オブジェクトの一部の機能をDLLとして公開したいのですが、その機能はバリアントを使用しています。しかし、知っておく必要があります。Variantパラメーターを使用して関数をエクスポートしたり、戻り値を返したりすることができます。または、文字列のみの表現に移行する方がよいでしょうか。

言語に依存しないPOV(コンシューマーはDelphiで作成されていませんが、すべてWindowsで実行されます)から何が優れていますか?

4

2 に答える 2

6

COM で使用されるバリアント値型である OleVariant を使用できます。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 に答える