2012-10-27 56 views
2

我需要在Delphi中讀取PSafeArray的數據。
PSafeArray由在C#中開發的DLL中實現的方法返回。此方法返回一個二維字符串數組string[,]。如何在Delphi中讀取PSafeArray結果?如何從多維PSafeArray獲取數據?

回答

6

您必須使用SafeArrayGetLBound,SafeArrayGetUBound,SafeArrayGetElement函數。

試試這個樣本

var 
    LSafeArray: PSafeArray; 
    LBound, UBound, I: LongInt; 
    LYBound, UYBound, J: LongInt; 
    Index: array [0..1] of Integer; 
    LData: OleVariant; 
begin 
    //get the PSafeArray 
    LSafeArray := GetArray;// GetArray is your own function 
    //get the bounds of the first dimension 
    SafeArrayGetLBound(LSafeArray, 1, LBound); 
    SafeArrayGetUBound(LSafeArray, 1, UBound); 
    //get the bounds of the second dimension 
    SafeArrayGetLBound(LSafeArray, 2, LYBound); 
    SafeArrayGetUBound(LSafeArray, 2, UYBound); 

    //iterate over the array 
    for I := LBound to UBound do 
    for J := LYBound to UYBound do 
    begin 
     //set the index of the element to get 
     Index[0]:=I; 
     Index[1]:=J; 
     SafeArrayGetElement(LSafeArray, Index, LData); 

     //do something with the data 
     Memo1.Lines.Add(LData); 
    end; 
    SafeArrayDestroy(LSafeArray); 
end;