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

Jer*_*cob 2 c# delphi delphi-7 safearray

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

RRU*_*RUZ 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;
Run Code Online (Sandbox Code Playgroud)