> > did the example work PB ?
>
> Yep, for Windows 2000 -- but not on Windows 98 SE
Okay, I worked out what I was doing wrong -- I was converting the
search/replace strings to Unicode on 98, when you're only supposed
to do that on NT-based Windows. So, here's my/your amended code,
which I can confirm works great on 98, ME, 2000, and XP (not on 95
or NT because this example replaces "Backspace" in Calc, and Calc
on those versions is called "Back").
Again, thank you, Rings, for helping port this to PureBasic!
[Edited 20 Jan 2007 to work with PureBasic v4.02]
Code: Select all
; simple Trainer, ported from vb to Pure
; search in a other process's memory for a keyword (here wideansi string)
; and replace that with our own
; another hack brought to you by Codeguru
; 2003 Siegfried Rings
; Modified a bit by PB to run on 98/ME as well as 2000/XP. :)
Global ProcessID.l
Procedure.l RunProgramEx(Filename.s,Parameter.s,Directory.s,ShowFlag.l)
If Left(Parameter,1)<>" " : Parameter=" "+Parameter : EndIf
Info.STARTUPINFO : Info\cb=SizeOf(STARTUPINFO) : Info\dwFlags=1
Info\wShowWindow=ShowFlag : ProcessInfo.PROCESS_INFORMATION
ProcessPriority=#NORMAL_PRIORITY_CLASS
If CreateProcess_(@Filename,@Parameter,0,0,0,ProcessPriority,0,@Directory,@Info,@ProcessInfo)
ProcessID.l=ProcessInfo\dwProcessId
Repeat
win=FindWindow_(0,0)
While win<>0
GetWindowThreadProcessId_(win,@pid.l)
If pid=ProcessID : WinHandle=win : Break : EndIf
win=GetWindow_(win,#GW_HWNDNEXT)
Wend
Until WinHandle
EndIf
ProcedureReturn WinHandle
EndProcedure
Procedure CompareMemorybuffers(Bufferadress1,Bufferadress2,Len1,len2)
L=0
L2=len1-Len2 -1
Repeat
B2.b=PeekB(Bufferadress2)
B1.b=PeekB(Bufferadress1+L)
If B1=B2
t=0
For L3=1 To Len2-1
B2.b=PeekB(Bufferadress2+L3)
B1.b=PeekB(Bufferadress1+L+L3)
If B2=B1
t+1
Else
Break
EndIf
Next L3
If t=len2 -1
ProcedureReturn l
EndIf
EndIf
L+1
Until L=L2
EndProcedure
sSearchString.s = "Backspace"
sReplaceString.s = "PurePower"
si.SYSTEM_INFO
If OSVersion()>#PB_OS_Windows_ME ; 2000 and XP.
If OSVersion()=#PB_OS_Windows_2000
hWin=RunProgramEx("c:\winnt\system32\calc.exe","","c:\winnt\system32\",#SW_SHOW)
Else ; XP.
hWin=RunProgramEx("c:\windows\system32\calc.exe","","c:\windows\system32\",#SW_SHOW)
EndIf
aSearchString.s=Space(2*Len(sSearchString.s))
MultiByteToWideChar_(#CP_ACP,0,@sSearchString.s,Len(sSearchString.s),@aSearchString.s,Len(aSearchString.s))
aReplaceString.s=Space(2*Len(sReplaceString.s))
MultiByteToWideChar_(#CP_ACP,0,@sReplaceString.s,Len(sReplaceString.s),@aReplaceString.s,Len(aReplaceString.s))
Else ; Windows 98/ME.
hWin=RunProgramEx("c:\windows\calc.exe","","c:\windows\",#SW_SHOW)
EndIf
hProcess = OpenProcess_(#PROCESS_ALL_ACCESS,0,ProcessID)
lLenMBI = SizeOf(MEMORY_BASIC_INFORMATION )
GetSystemInfo_(si)
lpMem = si\lpMinimumApplicationAddress
*mbi.MEMORY_BASIC_INFORMATION
adr=AllocateMemory(28)
*mbi=adr
While lpMem < si\lpMaximumApplicationAddress
ret = VirtualQueryEx_(hProcess, lpMem, adr,lLenMBI )
If ret = lLenMBI
If ((*mbi\lType = #MEM_PRIVATE) And (*mbi\State = #MEM_COMMIT))
If *mbi\RegionSize > 0
sBuffer = AllocateMemory(*mbi\RegionSize)
Res=ReadProcessMemory_(hProcess, *mbi\BaseAddress, sBuffer, *mbi\RegionSize, @lWritten)
If lWritten>0 And res>0
If OSVersion()>#PB_OS_Windows_ME ; 2000 and XP.
l=CompareMemorybuffers(sBuffer,@aSearchString.s ,*mbi\RegionSize,Len(sSearchstring.s) )
Else ; 98/ME.
l=CompareMemorybuffers(sBuffer,@sSearchString.s ,*mbi\RegionSize,Len(sSearchstring.s) )
EndIf
If l>0
;Debug "found at Pos. "+Str(l)+ " : "+PeekS(sBuffer+L,1) +PeekS(sBuffer+L+2,1)+PeekS(sBuffer+L+4,1)+PeekS(sBuffer+L+6,1)
CalcAddress = *mbi\BaseAddress + l
If OSVersion()>#PB_OS_Windows_ME ; 2000 and XP.
result=WriteProcessMemory_(hProcess, CalcAddress , @aReplaceString.s, Len(sReplaceString.s)*2, @lWritten)
Else ; 98/ME.
result=WriteProcessMemory_(hProcess, CalcAddress , @sReplaceString.s, Len(sReplaceString.s)*2, @lWritten)
EndIf
InvalidateRect_(hWin,0,1) ; Redraw Calc to show the change.
EndIf
EndIf
FreeMemory(sBuffer)
EndIf
EndIf
lpMem = *mbi\BaseAddress + *mbi\RegionSize
Else
Break
EndIf
Wend
If hProcess
CloseHandle_(hProcess)
EndIf
FreeMemory(adr)