2009-11-03 20 views
0

我想公開內部對象的一些功能作爲DLL - 但該功能使用變體。但我需要知道:我可以使用Variant參數和/或返回來導出函數 - 或者更好地轉到僅字符串表示形式?我如何(或者如果我不能)在簡單的DLL上使用變體?

從語言不可知的POV(消費者不是用Delphi製作 - 但所有的都可以在Windows中運行)更好嗎?

+0

有一個Delphi 2010?我以爲delphi結束VB6 – 2009-11-03 13:34:32

+2

顯然,你不覺得太好。 :-)德爾福仍然活着 - 2010年幾個月前發佈,目前正在開發至少兩個版本(一個64位,一個跨平臺)。你應該更好地跟上目前的新聞。 – 2009-11-03 13:36:37

+2

德爾福還活着,踢,克里斯;-) – 2009-11-03 14:16:27

回答

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); 
} 
+1

+1。基本上,堅持類型庫導入單元中看到的數據類型和約定似乎是一個好主意。 – mghie 2009-11-03 14:38:13

+0

嗯......很好。我今晚會試一試。 – 2009-11-03 14:55:31

0

據我所知,在其他語言中使用Variant變量類型沒有問題。 但是,如果您爲不同的變量類型導出相同的函數將會很好。

+0

據我所知在那裏有refcounted類型,通過德爾福heapmanager分配。 Olevariant的方式可能會避免這種情況。 – 2009-11-03 16:51:16

相關問題