Home > Back-end >  PSafeArray use safe array to solve, memory block has been taken, the number is up to, but how to get
PSafeArray use safe array to solve, memory block has been taken, the number is up to, but how to get

Time:10-12

Before didn't use safe array,
This is a VB docking do Com interface, memory block has been taken to//now traverse each element of the structure, but not how to read, they still hope everybody to give directions, never had this experience


PosLogInfo=the packed record
ReadMark: Byte;
CardNo: Double;
SFType: Smallint;
SFMealTypeID: Smallint;
SFAmount: Double;
CardBalance: Double;
SFDate: TDateTime;
CardUseTimes: Integer;
SFOpterNo: Smallint;
OrderDate: TDateTime;
ProductID: array [0.. 9] of Integer;
ProductNum: array [0.. 9] of Integer;
end;

function SafeArrayToText (p: PSafeArray) : string;
Var

RBound: TVarArrayBound;
I, UCount: Integer;
Xfinfo: PosLogInfo;//record consumption structure

The begin

VarResultCheck ((SafeArrayGetLBound (p, 1, RBound LowBound)));
VarResultCheck (SafeArrayGetUBound (p, 1, UCount));
RBound. ElementCount:=UCount - RBound. LowBound;

//the number of elements, not wrong, in line with the interface prompt 22.

//now traverse each element of the structure, but not how to read, they still hope everybody to give directions, never had this experience
For I:=RBound. LowBound to RBound. ElementCount do
The begin
FillChar (xfinfo, SizeOf (xfinfo), 0).
//that don't know how to write, arguably is right, but I don't know what's going on, read the value is always wrong, and each is the same actually
VarResultCheck (SafeArrayGetElement (p, I, xfinfo));

Memo1. Lines. The Add (" CardNo: "+ FloatToStr (xfinfo. CardNo));
Memo1. Lines. The Add (' SFType: + FloatToStr (xfinfo. SFType));
Memo1. Lines. The Add (' SFAmount: + FloatToStr (xfinfo. SFAmount));
Memo1. Lines. The Add (' CardBalance: + FloatToStr (xfinfo. CardBalance));
Memo1. Lines. The Add (+ formatdatetime 'SFDate:' (' ', xfinfo. SFDate));

end;
end;


//FillChar has clear 0 structure, traverse value is the same, all specifications should already have the assignment, but also not

CardNo: 0
SFType: 5120
3.79442416006077 e-321 SFAmount:
6.21476085095821 e-317 CardBalance:
SFDate: 1899-12-30

CardNo: 0
SFType: 5120
3.79442416006077 e-321 SFAmount:
6.21476085095821 e-317 CardBalance:
SFDate: 1899-12-30

CardNo: 0
SFType: 5120
3.79442416006077 e-321 SFAmount:
6.21476085095821 e-317 CardBalance:
SFDate: 1899-12-30

CardNo: 0
SFType: 5120
3.79442416006077 e-321 SFAmount:
6.21476085095821 e-317 CardBalance:
SFDate: 1899-12-30

CodePudding user response:

SafeArrayGetElement (p, I, xfinfo); What's p
SafeArrayGetElement (target array, the array indexing number, read the count of group)

CodePudding user response:

reference 1st floor sololie response:
SafeArrayGetElement (p, I, xfinfo); What's p
SafeArrayGetElement (target array, the array indexing number, read the count of group)


P: PSafeArray, is a safe array


Hello, my friend also say so, the return value should be a array, how should I keep this array into my record of consumption structure?
SafeArrayGetElement (target array, the array indexing number, read the count of group)

CodePudding user response:

Try
 
Var
RBound: TVarArrayBound;
I, UCount: Integer;
XfinfoArr: an array of PosLogInfo;
The begin
VarResultCheck ((SafeArrayGetLBound (p, 1, RBound LowBound)));
VarResultCheck (SafeArrayGetUBound (p, 1, UCount));
RBound. ElementCount:=UCount - RBound. LowBound;

SetLength (xfinfoArr, SizeOf (PosLogInfo) * 22);
For I:=RBound. LowBound to RBound. ElementCount do
The begin
//FillChar (xfinfo, SizeOf (xfinfo), 0).
VarResultCheck (SafeArrayGetElement (p, I, xfinfoArr));

Memo1. Lines. The Add (" CardNo: "+ FloatToStr (xfinfoArr [I]. CardNo));
Memo1. Lines. The Add (' SFType: + FloatToStr (xfinfoArr [I] SFType));
Memo1. Lines. The Add (' SFAmount: + FloatToStr (xfinfoArr [I] SFAmount));
Memo1. Lines. The Add (' CardBalance: + FloatToStr (xfinfoArr [I] CardBalance));
Memo1. Lines. The Add (+ formatdatetime 'SFDate:' (' ', xfinfoArr [I] SFDate));
end;
end;

CodePudding user response:

Don't know how your safearray is stored, this is an example, the deposit is two hexadecimal array data, put it out of the code:
The class procedure Convert. SafeArrayToBytes (const AData: OleVariant;
Var ABytes: TBytes);
Var
Sa: PSafeArray;
L, u: Integer;
P: Pointer;
The begin
SetLength (ABytes, 0);
If (TVarData (AData.) VType=VT_ARRAY or VT_UI1) then
The begin
Sa:=TVarData (AData). VAny;

If S_OK & lt;> SafeArrayGetLBound (sa, 1, l) then the Exit;
If S_OK & lt;> SafeArrayGetUBound (sa, 1, u) then the Exit;
If (u=0) then
The Exit;
Then the if S_OK=SafeArrayAccessData (sa, p)
Try
SetLength (ABytes, u - l + 1);
CopyMemory (@ ABytes [0], p, u - l + 1);
The finally
SafeArrayUnaccessData (sa);
end;
end;
end;
  • Related