AHK Write Memory (Pointer)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Stupify
Posts: 8
Joined: 28 May 2018, 03:09

AHK Write Memory (Pointer)

28 May 2018, 03:32

Hello. I'm getting back into messing with AHK again, and I'm having trouble thinking of how to write memory to a multi level pointer, and was hoping I'd be able to get some help. Thanks to functions released a while ago on AHK forums, I have the read memory/write memory/read pointer functions, but I don't know how I would go about writing to a multi level pointer.

These are the functions I'm using currently:

Code: Select all


;READ MEMORY
ReadMemory(MADDRESS=0,PROGRAM="",BYTES=4)
{
   Static OLDPROC, ProcessHandle
   VarSetCapacity(MVALUE, BYTES,0)
   IF PROGRAM != %OLDPROC%
   {
      WinGet, pid, pid, % OLDPROC := PROGRAM
      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",MADDRESS,"Str",MVALUE,"UInt",BYTES,"UInt *",0)
   {	Loop % BYTES
			Result += *(&MVALUE + A_Index-1) << 8*(A_Index-1)
		Return Result
	}
   return !ProcessHandle ? "Handle Closed:" closed : "Failed"
}

Code: Select all

;READ ML POINTERS
ReadPointer(GameTitle, BaseAddress, offsets*)
{ 
	IF Offsets.MaxIndex() = 1
		ReadPointer := Offsets[1] + ReadMemory(BaseAddress, GameTitle)
	ELSE
		For Index, Offset IN Offsets 
		{
			IF Index = 1 
				ReadPointer := ReadMemory(Offset + ReadMemory(BaseAddress, GameTitle), GameTitle)
			ELSE IF (Offsets.MaxIndex() = A_Index)
				ReadPointer += offset
			ELSE ReadPointer := ReadMemory(ReadPointer + Offset, GameTitle)
		}	
	RETURN ReadMemory(ReadPointer, GameTitle)
}

Code: Select all

;Read Weight Pointer
CurrentWeight_Pointer := ReadPointer(GameTitle, BaseAddress, 0x360,0x14,0x18,0x1C)
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

Re: AHK Write Memory (Pointer)

28 May 2018, 03:54

hello,
before you start, read this first : Reading memory
you can get classMemory lib from here

this is some example :

Code: Select all


#Include <ClassMemory>
game := new _ClassMemory("ahk_exe SomeGame.exe", "", hProcessCopy) 
 if !isObject(game) 
            {
                msgbox failed to open a handle
                if (hProcessCopy = 0)
                    msgbox The program isn't running (not found) or you passed an incorrect program identifier parameter. 
                else if (hProcessCopy = "")
                    msgbox OpenProcess failed. If the target process has admin rights, then the script also needs to be ran as admin. Consult A_LastError for more information.
                ExitApp
            }
GameOffset := [0x360,0x14,0x18,0x1C]
BaseAddress := 0xE555FC

;Read Weight Pointer
CurrentWeight_Pointer := game.read(BaseAddress, "UInt", GameOffset*) 
msgbox, % CurrentWeight_Pointer
Stupify
Posts: 8
Joined: 28 May 2018, 03:09

Re: AHK Write Memory (Pointer)

28 May 2018, 04:18

Thank you for pointing me to that memory library, I think that should be enough to go on. I'll try it out tonight if I have time and post back after work if I have any problems. Appreciated, thank you! :)
Stupify
Posts: 8
Joined: 28 May 2018, 03:09

Re: AHK Write Memory (Pointer)

15 Jul 2018, 20:42

After spending quite a bit of time with the Kalamity Memory Class that was recommended here, I've got basically everything working the way I need. The only current problem I am having trouble with, is with the WriteString function. I can't seem to get it to work right. The Read/ReadString/Write functions are all working as expected, but not the WriteString Function.

I'm using this function:

Code: Select all

WriteString(address, string, encoding := "utf-8", aOffsets*)
    {
        encodingSize := (encoding = "utf-16" || encoding = "cp1200") ? 2 : 1
        requiredSize := StrPut(string, encoding) * encodingSize - (This.insertNullTerminator ? 0 : encodingSize)
        VarSetCapacity(buffer, requiredSize)
        StrPut(string, &buffer, StrLen(string) + (This.insertNullTerminator ?  1 : 0), encoding)
        return DllCall("WriteProcessMemory", "Ptr", This.hProcess, "Ptr", aOffsets.maxIndex() ? This.getAddressFromOffsets(address, aOffsets*) : address, "Ptr", &buffer, "Ptr", requiredSize, "Ptr", This.pNumberOfBytesWritten)
    }
This is the Gui Edit/Var:

Code: Select all

Gui, 11:Add, Edit, X4 Y100 W150 H18 vEO1_ClientIP, %EO1_ClientIP_Pointer%
I'm trying to write my gui var to memory like below:

Code: Select all

IF (EO1_ClientIP != EO1_ClientIP_Pointer && EOCH_Cur_Ctrl == "Edit1")
EOClient1.WriteString(0x006DD100, %EO1_ClientIP%, "UTF-8", 0x0C, 0x300, 0x0C, 0x1C, 0x0)
^Can someone give me an example of the WriteString function as I intended to use in the above example? Using a variable "EO1_ClientIP" also gives me an illegal character error. How would I make the WriteString function work so that I can write My Gui Edit Var as the new memory string?
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

Re: AHK Write Memory (Pointer)

15 Jul 2018, 22:16

have you tried "address value" with readstring()?
is your "address value" correct?

Code: Select all

BaseAddr := 0x006DD100
offsets :=  [0x0C, 0x300, 0x0C, 0x1C, 0x0]
ReadEOClient1:= EOClient1.readString(BaseAddr, 0, utf-8, offsets*)
msgbox, % ReadEOClient1
if your value correct, try with simple thing

Code: Select all

BaseAddr := 0x006DD100
offsets :=  [0x0C, 0x300, 0x0C, 0x1C, 0x0]
EOClient1.WriteString(BaseAddr, "test", utf-8, offsets*)
ReadEOClient1:= EOClient1.readString(BaseAddr, 0, utf-8, offsets*)
msgbox, % ReadEOClient1
HTH
Stupify
Posts: 8
Joined: 28 May 2018, 03:09

Re: AHK Write Memory (Pointer)

15 Jul 2018, 22:48

Hi there. Thanks for trying to help.
The problem is with WriteString. I need to be able to write my variable with the WriteString function.

The below works and writes to the correct multi level pointer (String).

Code: Select all

EOClient1.WriteString(0x006DD100, "test.ip.net", "UTF-8", 0x0C, 0x300, 0x0C, 0x1C, 0x0)
I need to be able to use a variable instead though, like below.

Code: Select all

EOClient1.WriteString(0x006DD100, %EO1_ClientIP%, "UTF-8", 0x0C, 0x300, 0x0C, 0x1C, 0x0)
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

Re: AHK Write Memory (Pointer)

16 Jul 2018, 00:40

hello,
try delete "%"

Code: Select all

EOClient1.WriteString(0x006DD100, EO1_ClientIP, "UTF-8", 0x0C, 0x300, 0x0C, 0x1C, 0x0)
Stupify
Posts: 8
Joined: 28 May 2018, 03:09

Re: AHK Write Memory (Pointer)

16 Jul 2018, 01:15

Thanks for the suggestion, but I've already tried that and other variations, and it won't work.
It won't accept variables with the WriteString function I posted I guess. Not too sure what I could do. I'll try mess around some more.

EDIT:
After messing around for a while, I finally got it working.
I needed to remove the quotes from my variable with RegexReplace.

Code: Select all

EO1_ClientIP := RegexReplace(EO1_ClientIP, """", "")
EOClient1.WriteString(0x006DD100, EO1_ClientIP, "UTF-8", 0x0C, 0x300, 0x0C, 0x1C, 0x0)
Thanks again for trying to help. :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Nerafius and 70 guests