Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - TValue to pointer and back

I'm struggling with a simple piece of code, and even it is simple, I can not find a solution for it. On a part I have an event called like this

OnReadMessageParameter(Self, aName, aTypeInfo, pointer(@aValue), [psIsTValue]);

What is important is aValue parameter which is of type TValue. When this is called aValue.IsObject is true. I've assigned to this event my routine in order to handle the data from it:

.DoOnReadMessageParameter(Sender: TROMessage; const aName: string;
  aTypeInfo: PTypeInfo; const DataRef: pointer; Attributes: TParamAttributes);

Now, what my problem is, that I've tried in several ways to convert the DataRef back to a TValue item:

var val: tvalue;

tvalue.Make(@DataRef^,TypeInfo(TValue),val);
or another attempt
val := TValue(@DataRef^);

but the IsObject property is false. It must be something really simple which I'm missing out. Any idea?

like image 218
RBA Avatar asked Sep 18 '25 23:09

RBA


1 Answers

Where aValue is a TValue, @aValue is a pointer to the TValue structure, not the value it wraps. A better design would be to make the OnReadMessageParameter event have its DataRef parameter typed to TValue itself, rather than an untyped pointer.

However, if you can't control that, you need to cast DataRef to a pointer to a TValue, then deference it -

type
  PValue = ^TValue;
var
  val: TValue;
begin
  val := PValue(DataRef)^;
like image 129
Chris Rolliston Avatar answered Sep 23 '25 11:09

Chris Rolliston