Converting 8Byte to Double Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
luka2477
Posts: 2
Joined: 06 Oct 2020, 13:09

Converting 8Byte to Double

06 Oct 2020, 13:31

Hey everyone,

I'm trying to read process memory using DllCall("ReadProcessMemory"...) from an address that stores a Double datatype. The read is stored as a string and converted to 8Byte (Int64?).
How would I convert this to a Double datatype value?
Example: 8Byte value is 4682434448260570601 and Double value should be 112021.153472535

Here is the code that I'm working with:

Code: Select all

ReadMemory8Byte(MemAdrs=0,Prg="")
{
	Static OldPrc, ProcessHandle
	VarSetCapacity(MemV, 8, 0)
	
	if Prg != %OldPrc%
	{
		WinGet, pid, pid, % OldPrc := Prg
		ProcessHandle := (ProcessHandle ? 0*(closed := DllCall("CloseHandle", "UInt", ProcessHandle)) : 0)+(pid ? DllCall("OpenProcess", "Int", 16, "Int", 0, "UInt", pid) : 0)
	}

	if (ProcessHandle) && DllCall("ReadProcessMemory", "UInt", ProcessHandle, "UInt", MemAdrs, "Str", MemV, "UInt", 8, "UInt *", 0)
		return *(&MemV+7)<<56 | *(&MemV+6)<<48 | *(&MemV+5)<<40 | *(&MemV+4)<<32 | *(&MemV+3)<<24 | *(&MemV+2)<<16 | *(&MemV+1)<<8 | *(&MemV)

	return !ProcessHandle ? "Handle Closed: " closed : "Fail"
}
Thanks in advance!
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Converting 8Byte to Double  Topic is solved

06 Oct 2020, 13:38

There might be better ways.

Code: Select all

Int64 := 4682434448260570601
VarSetCapacity(V,8,0)
NumPut(Int64, V, "Int64")
Double := Round( NumGet(V, "Double"), 9 )

Msgbox % Double
My Scripts and Functions: V1  V2
luka2477
Posts: 2
Joined: 06 Oct 2020, 13:09

Re: Converting 8Byte to Double

06 Oct 2020, 14:14

SKAN wrote:
06 Oct 2020, 13:38
There might be better ways.

Code: Select all

Int64 := 4682434448260570601
VarSetCapacity(V,8,0)
NumPut(Int64, V, "Int64")
Double := Round( NumGet(V, "Double"), 9 )

Msgbox % Double
Works like a charm!
Here's my implementation:

Code: Select all

Convert8ByteToDouble(ToConvert=0)
{
	VarSetCapacity(Buffer, 8, 0)
	NumPut(ToConvert, Buffer, "Int64")

	return Round(NumGet(Buffer, "Double"), 9)
}
It works great, but I don't fully understand why.. Could you possible explain why this works?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Converting 8Byte to Double

06 Oct 2020, 15:07

luka2477 wrote: It works great, but I don't fully understand why.. Could you possible explain why this works?
A buffer is a series of bytes with numbers.
Unless you know how to interpret these number, its just junk data.
NumGet() can read from a memory address up to 8 bytes.
You should be able to use it directly inside the main function.

Here is an example:

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force

; Let create a 4 byte buffer and put in a mystery number

Num := 1312901971
VarSetCapacity(V, 4, 0)
NumPut(Num, V, "Int")

; Is it an IP4 address?

MsgBox % NumGet(v, 0, "UChar") . "." . NumGet(v, 1, "UChar") . "." . NumGet(v, 2, "UChar") . "." . NumGet(v, 3, "UChar")

; Could it be a 0xAARRGGBB color ?

MsgBox % Format( "0x{:08X}", NumGet(V,"UInt") )

; Or is it a float value ?

MsgBox % NumGet(V, "float")

; OK. its none other than

MsgBox, % StrGet(&V, "cp0")
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Converting 8Byte to Double

06 Oct 2020, 17:49

the "better way" is amending ur RPM call to read in the double directly, sparing urself the VarSetCapacity and NumGet altogether
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Converting 8Byte to Double

06 Oct 2020, 18:02

SKAN already wrote:
06 Oct 2020, 15:07
NumGet() can read from a memory address up to 8 bytes.
You should be able to use it directly inside the main function.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Converting 8Byte to Double

08 Oct 2020, 17:58

Code: Select all

...
	... DllCall("ReadProcessMemory", "Ptr", ProcessHandle, "Ptr", MemAdrs, "Double*", MemV := 0, "Ptr", 8, "UInt*", 0)
		return MemV
...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Theda and 176 guests