Re: [Tutor] ctypes wintypes
eryk sun <[email protected]> Fri, 6 Oct 2017 22:38:01 +0100
| Newsgroups | gmane.comp.python.ctypes,gmane.comp.python.tutor |
|---|---|
| Message-ID | <CACL+1au7OskMPN7_DbO+cKFh6D_ap9f_W20Q71UPbhEeAUtJPw@mail.gmail.com> |
On Fri, Oct 6, 2017 at 10:06 PM, Michael C <[email protected]> wrote: > like this? > > buffer = ctypes.byref(ctypes.create_string_buffer(4)) No, the buffer is the array created by create_string_buffer, which you pass byref(). In the following example I create a `test` buffer that contains "spam", and I use the pseudo-handle from GetCurrentProcess with ReadProcessMemory to read this buffer into a target `buffer`. It's silly to do this in the current process, but it's just an example. import ctypes from ctypes.wintypes import HANDLE, LPVOID kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) SIZE_T = ctypes.c_size_t LPSIZE_T = ctypes.POINTER(SIZE_T) kernel32.GetCurrentProcess.restype = HANDLE kernel32.ReadProcessMemory.argtypes = (HANDLE, LPVOID, LPVOID, SIZE_T, LPSIZE_T) hProcess = kernel32.GetCurrentProcess() test = ctypes.create_string_buffer(b'spam') address = ctypes.addressof(test) buffer = ctypes.create_string_buffer(4) nread = SIZE_T() success = kernel32.ReadProcessMemory(hProcess, address, ctypes.byref(buffer), ctypes.sizeof(buffer), ctypes.byref(nread)) if not success: raise ctypes.WinError(ctypes.get_last_error()) print(buffer[:]) ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot