Search code examples
windowsdelphipointersdelphi-xe7

How to manually set adress of Pointer in Delphi


I want to manually set address of Pointer to value stored in string variable. I have:

addr : String;
ptr  : Pointer;

then:

addr:='005F5770';

How to assign it to the ptr?


Solution

  • Like this:

    ptr := Pointer($005F5770);
    

    You don't need a string variable since the address is a literal that is known at compile time.

    In fact you can make this a constant since the value is known at compile time:

    const
      ptr = Pointer($005F5770);
    

    Of course, if the value isn't a literal and really does start life as a string with hexadecimal representation then you first need to convert to an integer:

    ptr := Pointer(StrToUInt64('$' + S));
    

    Convert it to a UInt64 so that your code is immune to 32 bit pointer truncation when compiled for 64 bit.