2

我需要从PSafeArrayDelphi 中读取数据。
PSafeArray是由用 C# 开发的 DLL 中实现的方法返回的。此方法返回一个二维字符串数组string[,]。如何PSafeArray在 Delphi 中读取这样的结果?

4

1 回答 1

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;
于 2012-10-27T14:06:16.953 回答