Recent

Author Topic: [Solved] Registry (Hex or int) to ip Address?  (Read 1971 times)

TheLastCayen

  • Jr. Member
  • **
  • Posts: 81
[Solved] Registry (Hex or int) to ip Address?
« on: November 15, 2021, 05:31:56 pm »
I am using

Lazarus 2.0.12
FPC 3.2.0
Windows 10

I need to read an IP Address from the registry. The IP has been written per a 3rd party software and it's part of that software configuration.

As an example, the value from the registry is:

1E32A8C0

1E = 30
32 = 50
A8 = 168
C0 = 192

So the ip address is 192.168.50.30 (not sure why they saved it in this order, probably a pile)
If I read the number as an integer, the value is 506636480.

From there, I am totally lost. It seems easier to use the Hex value in order to extract my ip and I know I can "Registry.ReadInteger", then convert using IntToHex((X), 8));. fpc don't seem really friendly to work with Hex value. I am not sure how to isolate the number in pair of 2 so I can get the ip address per section. I am not sure how to work with the decimal version either. I don't see any hex variable I can work with and I am not too familiar with pointers. 

Anyone can recommend a way to read this ip address or some documentation to  point me in the right direction?

Thank you

« Last Edit: November 16, 2021, 12:07:26 am by TheLastCayen »

MarkMLl

  • Hero Member
  • *****
  • Posts: 4159
Re: Registry (Hex or int) to ip Address?
« Reply #1 on: November 15, 2021, 05:43:38 pm »
So it looks as though it's stored as a little-endian (i.e. x86 native format) 32-bit word. Try https://www.freepascal.org/docs-html/current/rtl/system/ntobe.html etc.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3045
Re: Registry (Hex or int) to ip Address?
« Reply #2 on: November 15, 2021, 06:09:56 pm »
Hi!

IP addresses in the Windows registry are from the type REG_SZ whichs is a zero terminated string.

So you should not look for integers/Dwords but for strings with 3 dots in it.

Winni

Bart

  • Hero Member
  • *****
  • Posts: 4689
    • Bart en Mariska's Webstek
Re: Registry (Hex or int) to ip Address?
« Reply #3 on: November 15, 2021, 06:17:10 pm »
From your example it looks like you need to parse is as (pseudocode).
Lo(LoWord(Value)) . Hi(LoWord(Value)) . Lo(HiWord(Value)) . Hi(HiWordValue))
This would give you (Value=1E32A8C0): 192.168.50.30

Bart

wp

  • Hero Member
  • *****
  • Posts: 9573
Re: Registry (Hex or int) to ip Address?
« Reply #4 on: November 15, 2021, 06:27:33 pm »
This is a simple converter:

Code: Pascal  [Select][+][-]
  1. function IPAddressFromInteger(x: LongInt): String;
  2. type
  3.   TIPAddress = packed record b1, b2, b3, b4: byte; end;
  4. begin
  5.   with TIPAddress(x) do
  6.     Result := Format('%d.%d.%d.%d', [b1, b2, b3, b4]);
  7. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1099
    • Lebeau Software
Re: Registry (Hex or int) to ip Address?
« Reply #5 on: November 15, 2021, 07:01:00 pm »
IP addresses in the Windows registry are from the type REG_SZ whichs is a zero terminated string.

Clearly, the Registry value in question is not stored in REG_SZ format, but in REG_DWORD or REG_BINARY format, otherwise this wouldn't even be an issue.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

TheLastCayen

  • Jr. Member
  • **
  • Posts: 81
Re: Registry (Hex or int) to ip Address?
« Reply #6 on: November 16, 2021, 12:07:04 am »
Thank you everyone for your quick answer.

wp function fixed my puzzle  :)

 

TinyPortal © 2005-2018