使用Delphi,我需要访问包含数组中一个或多个记录的OleVariant。
我调用的方法返回VT_RRORD的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访问记录。
非常感谢任何输入。
答案 0 :(得分: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;
答案 1 :(得分: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;