Skip to content

Commit 24d2ac2

Browse files
Merge pull request #12 from cromachina/fix-ctype-conversions
Fix ctype conversions
2 parents 657ffc4 + 4f2825c commit 24d2ac2

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

PyMemoryEditor/linux/functions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ def read_process_memory(
6767
pid, (iovec * 1)(iovec(addressof(data), sizeof(data))),
6868
1, (iovec * 1)(iovec(address, sizeof(data))), 1, 0
6969
)
70-
return data.value.decode() if pytype is str else data.value
70+
71+
if pytype is str:
72+
return bytes(data).decode()
73+
elif pytype is bytes:
74+
return bytes(data)
75+
else:
76+
return data.value
7177

7278

7379
def search_addresses_by_value(

PyMemoryEditor/util/convert.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ def get_c_type_of(pytype: Type, length) -> ctypes._SimpleCData:
3232
if length <= 4: return ctypes.c_int32() # 4 Bytes
3333
return ctypes.c_int64() # 8 Bytes
3434

35-
# Float values lose their precision when converted to c_float. For that reason,
36-
# any float value will be converted to double.
37-
elif pytype is float: return ctypes.c_double() # 8 Bytes
35+
elif pytype is float:
36+
37+
if length == 4: return ctypes.c_float() # 4 Bytes
38+
return ctypes.c_double() # 8 Bytes
3839

3940
elif pytype is bool: return ctypes.c_bool()
4041

PyMemoryEditor/win32/functions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ def ReadProcessMemory(
127127
data = get_c_type_of(pytype, bufflength)
128128
kernel32.ReadProcessMemory(process_handle, ctypes.c_void_p(address), ctypes.byref(data), bufflength, None)
129129

130-
return data.value.decode() if pytype is str else data.value
130+
if pytype is str:
131+
return bytes(data).decode()
132+
elif pytype is bytes:
133+
return bytes(data)
134+
else:
135+
return data.value
131136

132137

133138
def SearchAddressesByValue(

0 commit comments

Comments
 (0)