Get Position of a struct var AKA Offset of record field

delphi, delphi-7, memory-management, record, struct

Solution

You can get the offset of any record member using some pointer arithmetic:

type
  PMyStruct = ^MyStruct;

var
  Offset: Integer;
begin
  Offset := Integer(@(PMyStruct(nil).MyInteger3));
  // or:
  // Offset := Integer(Addr(PMyStruct(nil).MyInteger3));
end;

If you want the offset of the function you need to code it like this:

Offset := Integer(@@PMyStruct(nil).MyFunc);
// or:
// Offset := Integer(Addr(@PMyStruct(nil).MyFunc));

Problem

I would like to get a "position" of a structure/record. Say I have this record: ``` type MyStruct = record MyInteger : Integer; MyInteger2 : Integer; MyInteger3 : Integer; MyFunc : function (FirstParam : WideString; SecondParam : String) : Integer; MyString : String; MyString2 : WideString; MyPchar : pchar; end; ``` As you can see this record has a size of 28 bytes (7 vars x 4 bytes). Basically because all the vars are either 4 byte vars (such as Integer) or pointers (also 4 bytes). Now let's say we have this struct loaded in an memory-address (X = 0) (which also means the address for MyInteger would be 0). The address of MyInteger3 (for example) would be 8 (be aware of the X = 0 !) How can I get the position (number/address) of the struct dynamically? Hope you guys know what I mean? Thanks in advance. BTW: Is any Var always 4 bytes in a struct? EDIT: This is wrong if you fix the spcae : `String[100]`

Original source