ReadMemoryText - Something's not quite right

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Grendahl
Posts: 159
Joined: 30 Sep 2013, 08:21

ReadMemoryText - Something's not quite right

11 Oct 2013, 23:08

I'm using the following ReadMemory() function with great success:

Code: Select all

ReadMemory(MADDRESS=0,PROGRAM="",BYTES=4)
{
	Static OLDPROC, ProcessHandle
	VarSetCapacity(MVALUE, BYTES,0)
	If PROGRAM != %OLDPROC%
	{
		WinGet, pid, pid, % OLDPROC := PROGRAM
		EnableDebugPrivileges()
		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 : "Fail"
}
However, the following function doesn't seem to work properly for me:

Code: Select all

ReadMemoryText(MADDRESS,PROGRAM)
{
	winget, pid, PID, %PROGRAM%
	VarSetCapacity(MVALUE,22,22)
	EnableDebugPrivileges()
	ProcessHandle := DllCall("OpenProcess", "Int", 24, "Char", 0, "UInt", pid, "UInt")
	DllCall("ReadProcessMemory","UInt",ProcessHandle,"UInt",MADDRESS,"Str",MVALUE,"UInt",22,"UInt *",22)
	Loop 8
	result += *(&MVALUE + A_Index-1) << 8*(A_Index-1)
	SetFormat, Integer, Hex
	result+=0
	count=1
	loop 8
	{
		count+=2
		StringMid, c, result, %count%, 2
		c=0x%c%
		c:=chr(c)
		final=%c%%final%
	}
	return, final 
}
See Below:
Image

I'd appreciate any and all help in getting the ReadMemoryText() function working. :)

Thanks!
magusneo
Posts: 36
Joined: 30 Sep 2013, 06:34

Re: ReadMemoryText - Something's not quite right

12 Oct 2013, 07:27

You should use StrGet in ReadMemoryText().
User avatar
Grendahl
Posts: 159
Joined: 30 Sep 2013, 08:21

Re: ReadMemoryText - Something's not quite right

12 Oct 2013, 10:56

Got it sorted with some searching:

Code: Select all

ReadMemoryStr(MADDRESS=0, pOffset = 0, PROGRAM = "", length = 0 , terminator = "") 
{ 
   Static OLDPROC, ProcessHandle
   VarSetCapacity(MVALUE,4,0)
   If PROGRAM != %OLDPROC%
   {
      WinGet, PID, PID, % OLDPROC := PROGRAM
	  EnableDebugPrivileges()
      ProcessHandle := ( ProcessHandle ? 0*(closed:=DllCall("CloseHandle"
      ,"UInt",ProcessHandle)) : 0 )+(PID ? DllCall("OpenProcess"
      ,"Int",16,"Int",0,"UInt",PID) : 0) ;PID is stored in value PID
   }
	If (MADDRESS = 0) 
		closed:=DllCall("CloseHandle","UInt",ProcessHandle)
	If ( length = 0) ; read until terminator found
	{
		textstr = 
        Loop
        { 
            Output := "x"  ; Put exactly one character in as a placeholder. used to break loop on null 
            tempVar := DllCall("ReadProcessMemory", "UInt", ProcessHandle, "UInt", MADDRESS+pOffset, "str", Output, "Uint", 1, "Uint *", 0) 
            if (ErrorLevel or !tempVar) 
               return textstr 
            if (Output = terminator)
              break 
            textstr .= Output 
            MADDRESS++ 
		} 
        return, textstr  
		}		
	Else ; will read until X length
	{
		 textstr = 
         Loop % length
         { 
            Output := "x"  ; Put exactly one character in as a memory placeholder. 
            tempVar := DllCall("ReadProcessMemory", "UInt", ProcessHandle, "UInt", MADDRESS+pOffset, "str", Output, "Uint", 1, "Uint *", 0) 
            if (ErrorLevel or !tempVar) 
              return textstr 
            textstr .= Output
            MADDRESS++ 
         } 
          return, textstr  
	}
}
Last edited by Grendahl on 12 Oct 2013, 14:42, edited 1 time in total.
guest3456
Posts: 2546
Joined: 09 Oct 2013, 10:31

Re: ReadMemoryText - Something's not quite right

12 Oct 2013, 14:25

why are you only looping 8 times?

and then why would you expect to get more than 8 characters as a result?


Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], bourdin07, IlanSh, MannyKSoSo, SplatCatAU, virgil27 and 170 guests