[Python] How to mirror a struct that uses a 'std::string' with python ctypes?

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I used to mirror a cpp application struct exactly, in order to 'hack' in some memory and read/access their normally unaccessible values (due to poor API support for some area of their app).

    I used to do this kind of technique:

    import ctypes

    class AppStructFoo(ctypes.Structure):
    _fields_ = [
    ("next", ctypes.c_void_p),
    ("prev", ctypes.c_void_p),
    ("name", ctypes.c_char * 64),
    ("identifier", ctypes.c_int32),
    ("flag", ctypes.c_int),
    ("idname", ctypes.c_char * 64),
    ("typeinfo", ctypes.c_void_p),
    ("type_legacy", ctypes.c_int16),
    ("ui_order", ctypes.c_int16),
    ("custom1", ctypes.c_int16),
    ("custom2", ctypes.c_int16), #Goal was to read this int let say..
    #ect..
    ]

    node_struct = AppStructFoo.from_address(memory_adress)
    print(node_struct.custom2)
    >>> 12 #YES! it was working perfectly!!



    But, now the application struct that i'm mirroring recently changed it's char name[64]; and char idname[64]; property to a std::string name; type of character chain.

    Therefore i wonder if there's a pythonic/ctypes solution to match a std::string memory layout? or perhaps if there is a way to skip it? The string is not boudn to 64 char anymore.. the limit is now unknown.

    This std::string element is similar to an unknown obstacle that i cannot seem to bypass in order to reach my objective

    Any idea?

    Continue reading...

Compartilhe esta Página