def UnpackDEV_BROADCAST(lparam): if lparam == 0: return None hdr_format = "iii" hdr_size = struct.calcsize(hdr_format) hdr_buf = win32gui.PyGetMemory(lparam, hdr_size) size, devtype, reserved = struct.unpack("iii", hdr_buf) # Due to x64 alignment issues, we need to use the full format string over # the entire buffer. ie, on x64: # calcsize('iiiP') != calcsize('iii')+calcsize('P') buf = win32gui.PyGetMemory(lparam, size) extra = x = {} if devtype == win32con.DBT_DEVTYP_HANDLE: # 2 handles, a GUID, a LONG and possibly an array following... fmt = hdr_format + "PP16sl" _, _, _, x['handle'], x['hdevnotify'], guid_bytes, x['nameoffset'] = \ struct.unpack(fmt, buf[:struct.calcsize(fmt)]) x['eventguid'] = pywintypes.IID(guid_bytes, True) elif devtype == win32con.DBT_DEVTYP_DEVICEINTERFACE: fmt = hdr_format + "16s" _, _, _, guid_bytes = struct.unpack(fmt, buf[:struct.calcsize(fmt)]) x['classguid'] = pywintypes.IID(guid_bytes, True) x['name'] = win32gui.PyGetString(lparam + struct.calcsize(fmt)) elif devtype == win32con.DBT_DEVTYP_VOLUME: # int mask and flags fmt = hdr_format + "II" _, _, _, x['unitmask'], x['flags'] = struct.unpack( fmt, buf[:struct.calcsize(fmt)]) else: raise NotImplementedError("unknown device type %d" % (devtype, )) return DEV_BROADCAST_INFO(devtype, **extra)
def _UnpackDEV_BROADCAST(lparam): """ Cut-down clone of UnpackDEV_BROADCAST from win32gui_struct, to be used for monkey-patching said module with correct handling of the "name" param of DBT_DEVTYPE_DEVICEINTERFACE """ import ctypes if lparam == 0: return None hdr_format = "iii" hdr_size = struct.calcsize(hdr_format) hdr_buf = win32gui.PyGetMemory(lparam, hdr_size) size, devtype, reserved = struct.unpack("iii", hdr_buf) # Due to x64 alignment issues, we need to use the full format string over # the entire buffer. ie, on x64: # calcsize('iiiP') != calcsize('iii')+calcsize('P') buf = win32gui.PyGetMemory(lparam, size) extra = {} if devtype == win32con.DBT_DEVTYP_DEVICEINTERFACE: fmt = hdr_format + "16s" _, _, _, guid_bytes = struct.unpack(fmt, buf[:struct.calcsize(fmt)]) extra['classguid'] = pywintypes.IID(guid_bytes, True) extra['name'] = ctypes.wstring_at(lparam + struct.calcsize(fmt)) else: raise NotImplementedError("unknown device type %d" % (devtype,)) return win32gui_struct.DEV_BROADCAST_INFO(devtype, **extra)
def test_memory_not_writable(self): # Check the buffer object fetched by PyGetMemory isn't writable. test_data = b"\0\1\2\3\4\5\6" c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) self.assertRaises(TypeError, operator.setitem, got, 0, 1)
def test_memory_slice(self): # Check we can slice the buffer object returned by PyGetMemory test_data = b"\0\1\2\3\4\5\6" c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) self.assertEqual(list(got[0:3]), [0, 1, 2])
def test_memory_index(self): # Check we can index into the buffer object returned by PyGetMemory test_data = b"\0\1\2\3\4\5\6" c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) self.assertEqual(got[0], 0)
def test_memory_index(self): # Check we can index into the buffer object returned by PyGetMemory test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6") c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) self.failUnlessEqual(got[0], pywin32_testutil.str2bytes('\0'))
def UnpackLVNOTIFY(lparam): format = "3i8i" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, item, subitem, newstate, oldstate, \ changed, pt_x, pt_y, lparam = struct.unpack(format, buf) return hwndFrom, id, code, item, subitem, newstate, oldstate, \ changed, (pt_x, pt_y), lparam
def UnpackLVNOTIFY(lparam): format = _nmhdr_fmt + _nmhdr_align_padding + "7i" if is64bit: format = format + "xxxx" # point needs padding. format = format + "P" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) ( hwndFrom, id, code, item, subitem, newstate, oldstate, changed, pt_x, pt_y, lparam, ) = struct.unpack(format, buf) return _MakeResult( "UnpackLVNOTIFY hwndFrom id code item subitem " "newstate oldstate changed pt lparam", ( hwndFrom, id, code, item, subitem, newstate, oldstate, changed, (pt_x, pt_y), lparam, ), )
def UnpackTVDISPINFO(lparam): item_size = struct.calcsize(_tvitem_fmt) format = "PPi%ds" % (item_size,) buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, buf_item = struct.unpack(format, buf) item = UnpackTVITEM(buf_item) return _MakeResult("TVDISPINFO hwndFrom id code item", (hwndFrom, id, code, item))
def UnpackLVDISPINFO(lparam): item_size = struct.calcsize(_lvitem_fmt) format = _nmhdr_fmt + _nmhdr_align_padding + ("%ds" % (item_size,)) buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, buf_item = struct.unpack(format, buf) item = UnpackLVITEM(buf_item) return _MakeResult("LVDISPINFO hwndFrom id code item", (hwndFrom, id, code, item))
def test_memory_slice(self): # Check we can slice the buffer object returned by PyGetMemory test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6") c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) assert got[0:3] == pywin32_testutil.str2bytes('\0\1\2')
def UnpackTVNOTIFY(lparam): format = "iiii40s40s" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, action, buf_old, buf_new \ = struct.unpack(format, buf) item_old = UnpackTVITEM(buf_old) item_new = UnpackTVITEM(buf_new) return hwndFrom, id, code, action, item_old, item_new
def test_ob(self): # Check the PyGetMemory result and a bytes string can be compared test_data = b"\0\1\2\3\4\5\6" c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) self.assertEqual(len(got), len(test_data)) self.assertEqual(bytes(got), test_data)
def test_ob(self): # Check the PyGetMemory result and a bytes string can be compared test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6") c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) assert len(got) == len(test_data) assert ob2bytes(got) == test_data
def test_memory_not_writable(self): # Check the buffer object fetched by PyGetMemory isn't writable. test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6") c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) new = pywin32_testutil.str2bytes('\1') self.failUnlessRaises(TypeError, operator.setitem, got, 0, new)
def test_real_view(self): # Do the PyGetMemory, then change the original memory, then ensure # the initial object we fetched sees the new value. test_data = b"\0\1\2\3\4\5\6" c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) self.assertEqual(got[0], 0) c[0] = 1 self.assertEqual(got[0], 1)
def test_real_view(self): # Do the PyGetMemory, then change the original memory, then ensure # the initial object we fetched sees the new value. test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6") c = array.array("b", test_data) addr, buflen = c.buffer_info() got = win32gui.PyGetMemory(addr, buflen) assert got[0] == pywin32_testutil.str2bytes('\0') new = pywin32_testutil.str2bytes('\1') c[0] = 1 assert got[0] == new
def UnpackTVNOTIFY(lparam): item_size = struct.calcsize(_tvitem_fmt) format = _nmhdr_fmt + _nmhdr_align_padding if is64bit: format = format + "ixxxx" else: format = format + "i" format = format + "%ds%ds" % (item_size, item_size) buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, action, buf_old, buf_new \ = struct.unpack(format, buf) item_old = UnpackTVITEM(buf_old) item_new = UnpackTVITEM(buf_new) return _MakeResult("TVNOTIFY hwndFrom id code action item_old item_new", (hwndFrom, id, code, action, item_old, item_new))
def UnpackLVDISPINFO(lparam): format = "iii40s" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, id, code, buf_item = struct.unpack(format, buf) item = UnpackLVITEM(buf_item) return hwndFrom, id, code, item
def UnpackWMNOTIFY(lparam): format = "PPi" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) return _MakeResult("WMNOTIFY hwndFrom idFrom code", struct.unpack(format, buf))
def UnpackWMNOTIFY(lparam): format = "iii" buf = win32gui.PyGetMemory(lparam, struct.calcsize(format)) hwndFrom, idFrom, code = struct.unpack(format, buf) return hwndFrom, idFrom, code
def ReadMappedMemory(virt_addr, size): return win32gui.PyGetMemory(virt_addr, size)