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

最佳答案

您必须使用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;

关于c# - 如何从多维PSafeArray获取数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13097395/

10-13 02:20