如何从多维PSafeArray中获取数据?

时间:2012-10-27 05:01:35

标签: c# delphi delphi-7 safearray

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

1 个答案:

答案 0 :(得分:6)

您必须使用SafeArrayGetLBoundSafeArrayGetUBoundSafeArrayGetElement功能。

试试这个样本

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;