开发者

How do I get a variant from a pointer in Delphi?

开发者 https://www.devze.com 2023-02-24 05:15 出处:网络
I need to be able to convert a naked pointer to a variant.I know that the 开发者_JAVA百科pointer points to a variant, but I can\'t seem to get it back out.A straight cast (as I pretty much thought) fa

I need to be able to convert a naked pointer to a variant. I know that the 开发者_JAVA百科pointer points to a variant, but I can't seem to get it back out. A straight cast (as I pretty much thought) fails:

Result := Variant(FAddress)^

returns a compiler error: [DCC Error] E2089 Invalid typecast

I've scoured the variants.pas unit as well, but nothing jumped out at me.

Obviously I'm missing something. What is the way to do this?


If the pointer points at a Variant, then its type is PVariant. Type-cast it to that, and then dereference:

Result := PVariant(FAddress)^;

Better yet, declare FAddress with the right type to begin with, and then you don't need to typecast:

var
  FAddress: PVariant;

Result := FAddress^;

The compiler considers your attempted type-cast invalid because Variant is a bigger type than Pointer is. The compiler doesn't know where to get the additional data to create a full Variant value. And if the type-cast were valid, the use of the ^ operator isn't allowed on Variants anyway. You might have gotten away with this:

Result := Variant(FAddress^);

I've never liked that; if FAddress is an untyped pointer, then dereferencing it yields a value without any size or type at all, and it's just weird to type-cast such a thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消