Okay all you ctypes gurus out there...
I've got a python script that reads a memory address a hundred times a second. The value stored at this memory address represents an unsigned integer. This value increases over time. Unfortunately, when it passes 2^8, 2^16, or 2^24, the ReadProcessMemory call does not read enough bytes to get the larger integer. In fact, it appears to not read the correct amount of bytes after the first read.
Here is the code snippet:
from time import sleep
from ctypes import *
from struct import *
pid = 0x0D50
op = windll.kernel32.OpenProcess
rpm = windll.kernel32.ReadProcessMemory
ch = windll.kernel32.CloseHandle
PAA = 0x1F0FFF
addy = 0x38D53368
ph = op(PAA,False,int(pid)) #program handle
lastvalue = 0
while True:
datadummy = b'.'*4
buff = c_char_p(datadummy)
bufferSize = (len(buff.value))
bytesRead = c_ulong(0)
if rpm(ph,addy,buff,bufferSize,byref(bytesRead)):
value = unpack('I',datadummy)[0]
if lastvalue != value:
print value
print bytesRead
lastvalue = value
sleep(.01)
And the output might be something like:
191
c_ulong(4L) ////Here it got 4 bytes like I expected
211
c_ulong(1L) ////But here it only got 1 byte.?? It should be reading 4 bytes everytime
231
c_ulong(1L)
251
c_ulong(1L)
15 ////This value is incorrect, because it only reads 1 byte. (should be 271)
c_ulong(1L)
What it seems to me is that it is only reading the number of bytes that the previous call needed to read...
How can I fix this?