2

使用 Delphi,我需要访问一个包含一个或多个数组记录的 OleVariant。

我调用的方法返回一个 VT_RECORD 的 VT_ARRAY,记录本身定义为:

struct StreamTimeInfo {
  unsigned int PID;
  LONGLONG PTS;
  LONGLONG TimeStamp;
}; 

我的代码是这样的:

procedure Test;
type
  TStreamInfo = record
    PID: Cardinal;
    PTS: Int64;
    TimeStamp: Int64;  
  end;
var
  Value: OleVariant
  StreamTime: TStreamInfo;
begin
  GetValue(Value); // Value holds a VT_ARRAY of VT_RECORD

  // How should I access the array of records in Delphi?
  // I've tried this to get to the first element:
  StreamTime := TStreamInfo(TVarData(Value).VPointer^);
end;

我不明白如何从 Delphi 访问记录。

非常感谢任何输入。

4

2 回答 2

3

我以前从未这样做过,但我认为这应该可行。

type 
  TStreamInfoArray = array [0..MaxArrayCount-1] of TStreamInfo; 
  PStreamInfoArray = ^TStreamInfoArray; 
var 
  Value: Variant;
  p: PStreamInfoArray;
  StreamInfo: TStreamInfo;
begin 
  GetValue(Value);
  p := PStreamInfoArray(VarArrayLock(Value)); 
  try
    StreamInfo := p^[Index]; 
  finally
    VarArrayUnlock(Value); 
  end;
end; 
于 2013-04-30T10:26:08.600 回答
2

供将来参考和其他人参考,这是最终的工作代码:

// Original C-Source definition of StreamTimeInfo
// import "oaidl.idl";
// import "ocidl.idl";
// [uuid(A5AA2ACD-BEA0-4570-9232-D8301A6DAE0F)] 
// struct StreamTimeInfo {
//   unsigned int    PID;       
//   LONGLONG        PTS;       
//   LONGLONG        TimeStamp; 
// };
// cpp_quote("typedef struct StreamTimeInfo StreamTimeInfo;")

procedure GetStreamTimes;
type
  TStreamTimeInfo = record
    PID: Cardinal;
    PTS: Int64;
    TimeStamp: Int64;
  end;
  TStreamTimeInfoArray = array[0..31] of TStreamTimeInfo;
  PStreamTimeInfoArray = ^TStreamTimeInfoArray;
var
  Value: OleVariant;
  SizeOfArray: Integer;
  PtrToArray: PStreamTimeInfoArray;
begin
  GetValue(EMPGPDMX_STREAMTIMES, Value);
  if VarArrayDimCount(Value) = 1 then
  begin
    SizeOfArray := 1 + VarArrayHighBound(Value, 1) - VarArrayLowBound(Value, 1);
    PtrToArray := PStreamTimeInfoArray(VarArrayLock(Value));
    try
     for I := 0 to SizeOfArray - 1 do
     begin
       StreamTimeInfo := PtrToArray^[I];
       // Usage Sample:
       // FStatus.StreamTimePID[I] := StreamTimeInfo.PID;
       // FStatus.StreamTimePTS[I] := StreamTimeInfo.PTS;
       // FStatus.StreamTimeTS[I] := StreamTimeInfo.TimeStamp;
     end;
   finally
     VarArrayUnlock(Value);
   end;
  end;
end;
于 2013-04-30T10:59:18.420 回答