OpenProcess (process API)ο
OpenProcess is the unified entry point. Depending on the host OS, it
resolves to:
| Platform | Concrete class |
|---|---|
| πͺ Windows | PyMemoryEditor.win32.process.WindowsProcess |
| π§ Linux | PyMemoryEditor.linux.process.LinuxProcess |
| π macOS | PyMemoryEditor.macos.process.MacProcess |
All three subclass AbstractProcess and share the API documented below.
- class AbstractProcessο
The cross-platform base class every backend implements.
OpenProcessreturns one of its subclasses; the methods documented on this page are the shared, public surface.
Constructionο
- class OpenProcess(*, process_name=None, pid=None, permission=<platform default>, case_sensitive=<platform default>, exact_match=True)ο
Open a target process.
OpenProcessresolves to the concrete backend for the host OS, so thepermissionandcase_sensitivedefaults are platform-specific (see below): on Windowspermissiondefaults to the read+write mask andcase_sensitivetoFalse; on Linux/macOSpermissiondefaults toNone(ignored) andcase_sensitivetoTrue.- Parameters:
process_name (str) β name of the target process.
pid (int) β process ID. Takes precedence over
process_name.permission β Windows only. A
ProcessOperationsEnumvalue (or integer). Defaults to read+write (PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION). On Linux and macOS this argument is accepted for API parity but ignored; passing a non-Nonevalue emitsUserWarning.case_sensitive (bool) β when
False,process_namematching ignores case. Default isFalseon Windows,Trueelsewhere.exact_match (bool) β when
False,process_namematches as a substring ("chrome"matches"chrome.exe").
- Raises:
ProcessNotFoundError β no process matches
process_name.ProcessIDNotExistsError β
piddoesnβt exist.AmbiguousProcessNameError β more than one process matches.
TypeError β neither
process_namenorpidwas provided.PermissionError β the OS denied access.
Examplesο
# By name
with OpenProcess(process_name="game.exe") as process:
...
# By PID
with OpenProcess(pid=1234) as process:
...
# Partial, case-insensitive name match (Windows-friendly)
with OpenProcess(process_name="chrome", case_sensitive=False, exact_match=False) as process:
...
# Read-only handle (Windows only)
from PyMemoryEditor import ProcessOperationsEnum
with OpenProcess(
process_name="game.exe",
permission=ProcessOperationsEnum.PROCESS_VM_READ | ProcessOperationsEnum.PROCESS_QUERY_INFORMATION,
) as process:
...
Attributesο
- pid: int
The PID of the target process.
- main_thread: ThreadInfo | Noneο
The conventional βmain threadβ of the target β by convention, the thread with the smallest
tid. ReturnsNoneif the process has no listable threads (rare).
Methodsο
Read / writeο
- read_process_memory(address, pytype, bufflength=None)ο
Read a value from memory.
- write_process_memory(address, pytype, bufflength=None, value=...)ο
Write a value to memory.
- Parameters:
address (int) β target memory address.
pytype (Type) β one of the five supported types.
bufflength (int) β value size in bytes. Optional (defaults to
None): numeric types fall back to their default width andstr/byteswrite the whole value. Forstr/bytesan explicit value is a maximum width that truncates the value and never pads βstrcounts characters (applied before UTF-8 encoding, so multibyte characters are never split),bytescounts bytes. Because it is optional, passvalueby keyword when omitting it (write_process_memory(addr, int, value=9999)).value β the value to write.
- Returns:
the written value.
Typed shortcutsο
Convenience read_* / write_* pairs with the size and signedness baked into
the name β see :doc:../guide/read-write for examples. Widths are fixed and
identical on every platform.
- read_char(address)
Read / write a signed 8-bit integer (1 byte). Pair:
write_char(address, value).
- read_short(address)
Signed 16-bit integer (2 bytes). Pair:
write_short.
- read_int(address)
Signed 32-bit integer (4 bytes). Pair:
write_int.
- read_long(address)
Signed 32-bit integer (4 bytes, Win32
LONG). Pair:write_long.
- read_longlong(address)
Signed 64-bit integer (8 bytes). Pair:
write_longlong.
- read_uchar(address)
Unsigned variants of the above:
read_uchar/read_ushort/read_uint/read_ulong/read_ulonglong(1 / 2 / 4 / 4 / 8 bytes), each with a matchingwrite_*.
- read_float(address)
32-bit float (4 bytes). Pair:
write_float.
- read_double(address)
64-bit double (8 bytes). Pair:
write_double.
- read_bool(address)
Boolean (1 byte). Pair:
write_bool.
- read_string(address, byte_count)
Read exactly
byte_countbytes (a short read raisesOSError), decode UTF-8, and return the text up to the first NUL β sobyte_countis the field width to read, not an upper bound. Pair:write_string(address, text, *, null_terminator=False).
- read_bytes(address, length)
Read
lengthraw bytes. Pair:write_bytes(address, data).
Searchingο
- search_by_value(pytype, bufflength=None, value=..., scan_type=ScanTypesEnum.EXACT_VALUE, *, progress_information=False, writeable_only=False, memory_regions=None)ο
Yield every address holding
value(compared perscan_type).bufflengthis optional (numeric types use their default width;str/bytesinfer it fromvalue) β passvalueby keyword when omitting it. See Searching memory for a full walkthrough.
- search_by_value_between(pytype, bufflength=None, start=..., end=..., *, not_between=False, progress_information=False, writeable_only=False, memory_regions=None)ο
Yield every address whose value is in
[start, end](or outside, withnot_between=True).
- search_by_addresses(pytype, bufflength=None, addresses=..., *, raise_error=False, memory_regions=None)ο
Read each address in
addressesonce, yielding(address, value). Far faster than looping overread_process_memory().bufflengthis optional for numeric types;str/bytesstill need an explicit size (no value to infer from) β passaddressesby keyword when omitting it.
- search_by_pattern(pattern, *, byte_length=0, progress_information=False, memory_regions=None)ο
Scan memory for a byte pattern β IDA-style hex, raw bytes regex, or a compiled
re.Pattern. See Pattern scan (AOB / regex).
Memory regionsο
- get_memory_regions()ο
Yield a
MemoryRegionper region β an immutable dataclass withaddress,size,is_readable,is_writable,is_executable,is_shared,pathand the platform-specificstruct.
- snapshot_memory_regions()ο
Materialize the region list once as a
MemoryRegionSnapshot(pre-sorted by base address), for reuse across iterative scans.
Modules and threadsο
- get_modules()ο
Yield a
ModuleInfofor every loaded module.
- get_threads()ο
Yield a
ThreadInfofor every thread inside the target.
Pointersο
- resolve_pointer_chain(base_address, offsets, *, ptr_size=None)ο
Walk a multi-level pointer chain and return the final address.
- get_pointer(base_address, offsets=None, *, pytype=int, bufflength=None, ptr_size=None)ο
Build a
RemotePointerbound to this process β a live, re-resolving handle. See Pointers.
- scan_pointer_paths(target_address, *, max_depth=5, max_offset=0x400, ptr_size=None, aligned=True, writable_only=True, static_ranges=None, max_results=None, memory_regions=None, progress_callback=None)ο
Reverse pointer scan β yield
PointerPathrecipes that resolve totarget_address. See Pointer scan (reverse).
- save_pointer_paths(paths, file)ο
Save pointer paths to a JSON file.
- load_pointer_paths(file)ο
Load pointer paths previously saved with
save_pointer_paths().
- rescan_pointer_paths(paths, target_address)ο
Keep only the paths that still resolve to
target_address.
- compare_pointer_scans(*sources)ο
Intersect several saved scans β return the paths present in every one.
Allocationο
- allocate_memory(size, *, permission=None)ο
Reserve
sizebytes inside the target. Windows and macOS only.
- free_memory(address, size=0)ο
Release a previously allocated region.
Lifecycleο
- close()ο
Close the process handle. Subsequent calls raise
ClosedProcess.
- __enter__()ο
- __exit__(exc_type, exc_value, exc_traceback)ο
The process is also a context manager β prefer the
withblock.