代码之家  ›  专栏  ›  技术社区  ›  Fabricio Araujo

如何(或者如果我不能)在简单的DLL上使用变体?

  •  0
  • Fabricio Araujo  · 技术社区  · 15 年前

    我想将内部对象的某些功能公开为dll,但该功能使用了变体。但我需要知道:我可以导出一个带有变量参数和/或返回的函数——或者最好是只使用字符串表示?

    从语言不可知的POV(消费者不是用Delphi制造的,但所有这些都将在Windows中运行)来看,还有什么更好呢?

    2 回复  |  直到 15 年前
        1
  •  6
  •   Robert Giesecke    15 年前

    您可以使用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);
    }
    
        2
  •  0
  •   silent    15 年前

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