Here is my code at the moment.
Code: Select all
#Include Classmemory.ahk
#SingleInstance force
; Return values:
; Null The process's window couldn't be found.
; 0 The GetWindowLong or GetWindowLongPtr call failed.
; Non-Zero The base address of the process (success).
getProcessBaseAddress(WindowTitle, windowMatchMode := "3") ;WindowTitle can be anything ahk_exe ahk_class etc
{
if (windowMatchMode && A_TitleMatchMode != windowMatchMode)
{
mode := A_TitleMatchMode ; This is a string and will not contain the 0x prefix
StringReplace, windowMatchMode, windowMatchMode, 0x ; remove hex prefix as SetTitleMatchMode will throw a run time error. This will occur if integer mode is set to hex and matchmode param is passed as an number not a string.
SetTitleMatchMode, %windowMatchMode% ;mode 3 is an exact match
}
else windowMatchMode := False
WinGet, hWnd, ID, %WindowTitle%
if windowMatchMode
SetTitleMatchMode, %mode% ; In case executed in autoexec
if !hWnd
return ; return blank failed to find window
else return DllCall( A_PtrSize = 4
? "GetWindowLong"
: "GetWindowLongPtr"
, "Ptr", hWnd
, "Uint", -6
, "Int64") ; Use Int64 to prevent negative overflow when AHK is 32 bit and target process is 64bit
; If DLL call fails, returned value will = 0
}
ReadMemory(MADDRESS,PROGRAM)
{
winget, pid, PID, %PROGRAM%
VarSetCapacity(MVALUE,4,0)
ProcessHandle := DllCall("OpenProcess", "Int", 24, "Char", 0, "UInt", pid, "UInt")
DllCall("ReadProcessMemory", "UInt", ProcessHandle, "Ptr", MADDRESS, "Ptr", &MVALUE, "Uint",4)
Loop 4
result += *(&MVALUE + A_Index-1) << 8*(A_Index-1)
return, result
}
;------------------------------------------------------------
if !A_IsAdmin
{
try Run *RunAs "%A_ScriptFullPath%"
catch
msgbox Please Run this again with admin rights.
ExitApp
}
GameIdentifier := ahk_exe Game.exe
BaseAddress := getProcessBaseAddress("Diablo II")
pointer10 := BaseAddress + 0x10502C
address10 := ReadMemory(Pointer10, "Diablo II")
pointer0 := Address10 + 0x14
address1 := ReadMemory(Pointer0, "Diablo II")
pointer1 := address1 + 0x3ac
address2 := ReadMemory(Pointer1, "Diablo II")
pointer2 := address2 + 0x24
address3 := ReadMemory(Pointer2, "Diablo II")
ArrowP := address3 + 0x4
Arrow := ReadMemory(arrowP, "Diablo II")
msgbox %BaseAddress%
msgbox %Address10%
return
/*
if (_ClassMemory.__Class != "_ClassMemory")
msgbox class memory not correctly installed. Or the (global class) variable "_ClassMemory" has been overwritten
mem := new _ClassMemory("ahk_exe Game.exe", "", hProcessCopy)
if !isObject(mem)
msgbox failed to open a handle
if !hProcessCopy
msgbox failed to open a handle. Error Code = %hProcessCopy%
msgbox % mem.BaseAddress
gamesPlayed := mem.read(mem.BaseAddress + 0x10502C, "UInt", 0x14, 0x3ac, 0x24, 0x4)
msgbox % gamesPlayed
*/
Do I need to convert this into Hex? This is just a test project so I can utilize memory reading and data management in my other scripts. I really want to understand this as step by step as possible.
Thank you.