Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Python cTypes - int too long to convert

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
m1ndgames_AI
How do I cheat?
Reputation: 0

Joined: 07 Mar 2021
Posts: 3

PostPosted: Sun Mar 07, 2021 9:33 am    Post subject: Python cTypes - int too long to convert Reply with quote

Hi,
im trying to access the health values for streetfighter2 in snes emu via python ctypes, and i get an error:

Code:
(skullgirls) C:\skullgirls>serpent play sf2 SerpentryuGameAgent
{'width': 512, 'height': 448, 'x_offset': 8, 'y_offset': 51}
Using TensorFlow backend.
2021-03-07 16:25:08.376221: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
5368709120
<class 'int'>
Traceback (most recent call last):
  File "c:\anaconda3\envs\skullgirls\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\anaconda3\envs\skullgirls\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Anaconda3\envs\skullgirls\Scripts\serpent.exe\__main__.py", line 7, in <module>
  File "c:\anaconda3\envs\skullgirls\lib\site-packages\serpent\serpent.py", line 52, in execute
    command_function_mapping[command](*sys.argv[2:])
  File "c:\anaconda3\envs\skullgirls\lib\site-packages\serpent\serpent.py", line 369, in play
    game.play(game_agent_class_name=game_agent_name, frame_handler=frame_handler)
  File "c:\anaconda3\envs\skullgirls\lib\site-packages\serpent\game.py", line 201, in play
    raise e
  File "c:\anaconda3\envs\skullgirls\lib\site-packages\serpent\game.py", line 192, in play
    game_agent.on_game_frame(game_frame, frame_handler=frame_handler, **kwargs)
  File "c:\anaconda3\envs\skullgirls\lib\site-packages\serpent\game_agent.py", line 115, in on_game_frame
    frame_handler(game_frame, **kwargs)
  File "C:\skullgirls\plugins\SerpentryuGameAgentPlugin\files\serpent_ryu_game_agent.py", line 157, in handle_play
    (self.p1hp, self.p2hp) = readhp()
  File "C:\skullgirls\plugins\SerpentryuGameAgentPlugin\files\helpers\memory.py", line 22, in readhp
    r1 = ReadProcessMemory(processHandle, BaseAddress + 0x008D8E38, byref(player_one), 2, byref(numRead))
ctypes.ArgumentError: argument 2: <class 'OverflowError'>: int too long to convert

(skullgirls) C:\skullgirls>


my code:

Code:
import ctypes
import win32process
import win32ui
from ctypes import *

def readhp():
    ReadProcessMemory = windll.kernel32.ReadProcessMemory

    PROCESS_ALL_ACCESS = 0x1F0FFF
    HWND = win32ui.FindWindow(None, u"StreetFighterIITurbo - Snes9x 1.60").GetSafeHwnd()
    PID = win32process.GetWindowThreadProcessId(HWND)[1]
    processHandle = ctypes.windll.kernel32.OpenProcess(PROCESS_ALL_ACCESS, False, PID)

    BaseAddress = win32process.EnumProcessModules(processHandle)[0]
    numRead = c_int()

    print(str(BaseAddress))
    print(str(type(BaseAddress)))

    # read fighters hp:
    player_one = c_int()
    r1 = ReadProcessMemory(processHandle, BaseAddress + 0x008D8E38, byref(player_one), 2, byref(numRead))
    print(str(r1))
    print(str(type(r1)))

    p1hp = int(player_one.value)
    player_two = c_int()
    ReadProcessMemory(processHandle, BaseAddress + 0x008D8CB8, byref(player_two), 2, byref(numRead))
    p2hp = int(player_two.value)

    # Make sure HP cant be lower than 0
    if p1hp < 0 or p1hp == 255:
        p1hp = 0
    if p2hp < 0 or p2hp == 255:
        p2hp = 0

    # Return HP
    print(str(p1hp))
    print(str(p2hp))

    return p1hp, p2hp


CheatEngine Screenshot that shows the address:
s20.directupload .net/images/210307/gwrf5eah.png

The same code worked for the game Skullgirls, im not sure what the issue is... snesx is 64bit, python is 64bit...

Any guesses?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Tue Mar 09, 2021 2:37 pm    Post subject: This post has 1 review(s) Reply with quote

If you are on 64bit, then this:
Code:
numRead = c_int()


Is technically wrong. ReadProcessMemory expects a SIZE_T which changes size based on which target platform it's compiled for. (4 bytes for 32bit, 8 bytes for 64bit.)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites