Delphi/Win32: Fun with RTTI

In .NET, it’s easy to use Reflection to peek into other objects’ private fields (though it’s considered bad form, and of course it requires full trust). But doing the same thing in Delphi/Win32, as far as I knew, involved using the Disassembler window to find some magic numbers, and then hard-coding those magic numbers, which would be nastily brittle (and is why I never bothered).

Not necessarily so, as it turns out. Hallvard Vassbotn just posted about a Delphi/Win32 hack to write to a read-only property, using nothing but RTTI, a nice twist on inheritance, and of course some pointer arithmetic — but no hard-coded magic numbers, so it’ll still work even if you change ancestors or add more fields. Cool!

Leave a Reply

Your email address will not be published. Required fields are marked *