Page 1 of 1

ReadMemoryText - Something's not quite right

Posted: 11 Oct 2013, 23:08
by Grendahl
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!

Re: ReadMemoryText - Something's not quite right

Posted: 12 Oct 2013, 07:27
by magusneo
You should use StrGet in ReadMemoryText().

Re: ReadMemoryText - Something's not quite right

Posted: 12 Oct 2013, 10:56
by Grendahl
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  
	}
}

Re: ReadMemoryText - Something's not quite right

Posted: 12 Oct 2013, 14:25
by guest3456
why are you only looping 8 times?

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