win32api and readprocessmemory problem.. sojo, james, tracer? HELP!

Blitz3D Forums/Blitz3D Programming/win32api and readprocessmemory problem.. sojo, james, tracer? HELP!

Ok then,

Download the below "trainme.exe" in zip form, run it, and rack up some number on there.

Run the BB program below and then try decreasing using the dwn button.

Const MAX_PATH = 264 
Const TH32CS_SNAPHEAPLIST = $1 
Const TH32CS_SNAPPROCESS = $2 
Const TH32CS_SNAPTHREAD = $4 
Const TH32CS_SNAPMODULE = $8 
Const TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE 
Const TH32CS_INHERIT = $80000000 
Const INVALID_HANDLE_VALUE = -1 
Const SizeOf_PE32 = 296 
Const STANDARD_RIGHTS_REQUIRED = $F0000 
Const SYNCHRONIZE = $100000 
Const PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or $FFF) 

snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) 

If snap 
	Proc32 = CreateBank(SizeOf_PE32) 
	PokeInt(Proc32,0,BankSize(Proc32))

	If Process32First(snap,Proc32)
		While Process32Next(snap,Proc32)
			dwSize              = PeekInt(Proc32,0) 
			cntUsage            = PeekInt(Proc32,4) 
			th32ProcessID       = PeekInt(Proc32,8) 
			th32DefaultHeapID   = PeekInt(Proc32,12) 
			th32ModuleID        = PeekInt(Proc32,16) 
			cntThreads          = PeekInt(Proc32,20) 
			th32ParentProcessID = PeekInt(Proc32,24) 
			pcPriClassBase      = PeekInt(Proc32,28) 
			dwFlags             = PeekInt(Proc32,32) 

			offset = 36 
			
			Repeat 
				char = PeekByte(Proc32,offset) 
				offset = offset + 1 
				szExeFile$ = szExeFile$ + Chr$(char) 
			Until char = 0 
			
			szExeFile$ = Left$(szExeFile$,Len(szExeFile$) - 1) 
			
			If szExeFile$ = "trainme.exe"
				program = th32ProcessID
			EndIf
			
			szExeFile$="" 
		Wend 
	EndIf 
	CloseHandle (snap) 
EndIf 

; Open Process with full access
app = OpenProcess(PROCESS_ALL_ACCESS,x,program)

; Create a little bank
buffer = CreateBank(10)

; To READ from memory.. not used in this case, but the below works.
;result = ReadProcessMemory(app,4207112,buffer,2,x)


; Train Me has the command for decreasing the number in the window
; at address 401429 in hex, to make it easy, we'll use dec to
; do our process writing, which is 4199465.

; *NOTE*
; The Hex down below is what it showed when _I_ ran Train Me
; It doesn't mean that it will be that on your computer!
; Keep in mind, CODE is static in memory, DATA is _NOT_

; The address contains the following:
; 89 1D 08 32 40 00
; The address where the value is held is: 403208
; 08 32 40, seem em above? reverse em.. 403208  HEY! whaddaya know :)
; So, ReadProcessMemory CAN be used to find the amount in memory
; even if it changes address! (mind you, in my little Train Me, it's very
; straightforward, it make be VERY different in games.

; So, what to do to stop it from decreasing (but, we want to make sure
; it CAN increase!)

; 4199465 is the address of the command that decreases the number.. so,
; all we have to do is NOP (No OPeration) it out. The command is six
; bytes in length, so we need to put six NOP's in it's place.

PokeByte buffer,0,144 ; <- 144 dec = 90 hex = NOP
PokeByte buffer,1,144 ; <- 144 dec = 90 hex = NOP
PokeByte buffer,2,144 ; <- 144 dec = 90 hex = NOP
PokeByte buffer,3,144 ; <- 144 dec = 90 hex = NOP
PokeByte buffer,4,144 ; <- 144 dec = 90 hex = NOP
PokeByte buffer,5,144 ; <- 144 dec = 90 hex = NOP

; We poked 6 bytes into the bank, each a 144, which is 90 (NOP) in hex.

; Now we write the six bytes to Train Me's memory and remove the
; decreasing command.
result = WriteProcessMemory(app,4199465,buffer,6,x)

; Now try to use the "dwn" button! :)
; Note how the up button still works!, this is because we only removed
; the code that decreases the number!


The little prog i made to demonstrate (PureBasic):

http://www.bannerpacking.com/trainme.zip

And it's source code:
win = OpenWindow(1,10,10,132,50,#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered,"Train Me")
If CreateGadgetList(WindowID(1))
  ButtonGadget(1,10,10,24,24,"Up")
  TextGadget(2,60,15,40,24,"0")
  ButtonGadget(3,100,10,24,24,"Dn")
EndIf

num = 0
SetGadgetText(2,Str(num))
Repeat
  EventID.l = WaitWindowEvent()

  If EventID = #PB_EventGadget

      Select EventGadgetID()
        Case 1
          num = num + 1
          
          If num > 200
            num = 200
          EndIf
          
          SetGadgetText(2,Str(num))

        Case 3
          num = num - 1
          
          If num < 0
            num = 0
          EndIf
          
          SetGadgetText(2,Str(num))
          
      EndSelect
  EndIf
  
  If EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button
    Quit = 1
  EndIf

Until Quit = 1


There ya go :)

Tracer