def windows_memory_usage(): """Return physical memory usage (float) Works on Windows platforms only""" from ctypes import windll, wintypes class MemoryStatus(wintypes.Structure): _fields_ = [ ("dwLength", wintypes.DWORD), ("dwMemoryLoad", wintypes.DWORD), ("ullTotalPhys", wintypes.c_uint64), ("ullAvailPhys", wintypes.c_uint64), ("ullTotalPageFile", wintypes.c_uint64), ("ullAvailPageFile", wintypes.c_uint64), ("ullTotalVirtual", wintypes.c_uint64), ("ullAvailVirtual", wintypes.c_uint64), ("ullAvailExtendedVirtual", wintypes.c_uint64), ] memorystatus = MemoryStatus() # MSDN documetation states that dwLength must be set to MemoryStatus # size before calling GlobalMemoryStatusEx # http://msdn.microsoft.com/en-us/library/aa366770(v=vs.85) memorystatus.dwLength = wintypes.sizeof(memorystatus) windll.kernel32.GlobalMemoryStatusEx(wintypes.byref(memorystatus)) return float(memorystatus.dwMemoryLoad)
def windows_memory_usage(): """Return physical memory usage (float) Works on Windows platforms only""" from ctypes import windll, wintypes class MemoryStatus(wintypes.Structure): _fields_ = [ ('dwLength', wintypes.DWORD), ('dwMemoryLoad', wintypes.DWORD), ('ullTotalPhys', wintypes.c_uint64), ('ullAvailPhys', wintypes.c_uint64), ('ullTotalPageFile', wintypes.c_uint64), ('ullAvailPageFile', wintypes.c_uint64), ('ullTotalVirtual', wintypes.c_uint64), ('ullAvailVirtual', wintypes.c_uint64), ('ullAvailExtendedVirtual', wintypes.c_uint64), ] memorystatus = MemoryStatus() # MSDN documetation states that dwLength must be set to MemoryStatus # size before calling GlobalMemoryStatusEx # http://msdn.microsoft.com/en-us/library/aa366770(v=vs.85) memorystatus.dwLength = wintypes.sizeof(memorystatus) windll.kernel32.GlobalMemoryStatusEx(wintypes.byref(memorystatus)) return float(memorystatus.dwMemoryLoad)
def getFileVersion(self,filename): from ctypes.wintypes import ( windll, sizeof, WinError, byref, POINTER, cast, c_char, Structure, c_uint, pointer, BOOL, DWORD, LPVOID, LPCVOID, LPCWSTR, ) class VS_FIXEDFILEINFO(Structure): _fields_ = [ ("dwSignature", DWORD), # will be 0xFEEF04BD ("dwStrucVersion", DWORD), ("dwFileVersionMS", DWORD), ("dwFileVersionLS", DWORD), ("dwProductVersionMS", DWORD), ("dwProductVersionLS", DWORD), ("dwFileFlagsMask", DWORD), ("dwFileFlags", DWORD), ("dwFileOS", DWORD), ("dwFileType", DWORD), ("dwFileSubtype", DWORD), ("dwFileDateMS", DWORD), ("dwFileDateLS", DWORD) ] PUINT = POINTER(c_uint) LPDWORD = POINTER(DWORD) GetFileVersionInfoSizeW = windll.version.GetFileVersionInfoSizeW GetFileVersionInfoSizeW.restype = DWORD GetFileVersionInfoSizeW.argtypes = [LPCWSTR, LPDWORD] GetFileVersionInfoSize = GetFileVersionInfoSizeW # alias GetFileVersionInfoW = windll.version.GetFileVersionInfoW GetFileVersionInfoW.restype = BOOL GetFileVersionInfoW.argtypes = [LPCWSTR, DWORD, DWORD, LPVOID] GetFileVersionInfo = GetFileVersionInfoW # alias VerQueryValueW = windll.version.VerQueryValueW VerQueryValueW.restype = BOOL VerQueryValueW.argtypes = [LPCVOID, LPCWSTR, POINTER(LPVOID), PUINT] VerQueryValue = VerQueryValueW # alias filename = unicode(filename) dwLen = GetFileVersionInfoSize(filename, None) if not dwLen : raise WinError() lpData = (c_char * dwLen)() if not GetFileVersionInfo(filename, 0, sizeof(lpData), lpData): raise WinError() uLen = c_uint() lpffi = POINTER(VS_FIXEDFILEINFO)() lplpBuffer = cast(pointer(lpffi), POINTER(LPVOID)) if not VerQueryValue(lpData, u"\\", lplpBuffer, byref(uLen)): raise WinError() ffi = lpffi.contents return [int(ffi.dwFileVersionMS >> 16), int(ffi.dwFileVersionMS & 0xFFFF), int(ffi.dwFileVersionLS >> 16), int(ffi.dwFileVersionLS & 0xFFFF)]
def send_key(vk=None, scan=None, extended=False, pressed=True): i = INPUT() i.union.ki.wVk = vk if scan: i.union.ki.wScan = scan else: #No scancode provided, try to get one i.union.ki.wScan = ctypes.windll.user32.MapVirtualKeyW(vk, MAPVK_VK_TO_VSC) if not pressed: i.union.ki.dwFlags |= KEYEVENTF_KEYUP if extended: i.union.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY i.type = INPUT_KEYBOARD ctypes.windll.user32.SendInput(1, ctypes.byref(i), ctypes.sizeof(INPUT))
def send_key(vk=None, scan=None, extended=False, pressed=True): i = INPUT() i.union.ki.wVk = vk if scan: i.union.ki.wScan = scan else: #No scancode provided, try to get one i.union.ki.wScan = ctypes.windll.user32.MapVirtualKeyW( vk, MAPVK_VK_TO_VSC) if not pressed: i.union.ki.dwFlags |= KEYEVENTF_KEYUP if extended: i.union.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY i.type = INPUT_KEYBOARD ctypes.windll.user32.SendInput(1, ctypes.byref(i), ctypes.sizeof(INPUT))
def get_base_address(pid): me32 = MODULEENTRY32() me32.dwSize = sizeof(MODULEENTRY32) snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid) next_module = Module32First(snapshot, pointer(me32)) while next_module and me32.th32ProcessID != pid: print me32.th32ProcessID print me32.szExePath next_module = Module32Next(snapshot, pointer(me32)) CloseHandle(snapshot) print hex(addressof(me32.modBaseAddr.contents)) # Convert base address from LP_c_byte to long return addressof(me32.modBaseAddr.contents)
def GetFileVersion(filename): dwLen = GetFileVersionInfoSize(filename, None) if not dwLen: raise WinError() lpData = (c_char * dwLen)() if not GetFileVersionInfo(filename, 0, sizeof(lpData), lpData): raise WinError() uLen = c_uint() lpffi = POINTER(VS_FIXEDFILEINFO)() lplpBuffer = cast(pointer(lpffi), POINTER(LPVOID)) if not VerQueryValue(lpData, u"\\", lplpBuffer, byref(uLen)): raise WinError() ffi = lpffi.contents return ( ffi.dwFileVersionMS >> 16, ffi.dwFileVersionMS & 0xFFFF, ffi.dwFileVersionLS >> 16, ffi.dwFileVersionLS & 0xFFFF, )
def mouse_move(delta_x, delta_y, center_x, center_y, sensitivity): mouse_move_x = delta_x * 4.0/ sensitivity mouse_move_y = delta_y * 4.0/ sensitivity if mouse_move_x == 0 and mouse_move_y == 0: return (0, 0) fScreenWidth = windll.user32.GetSystemMetrics(0) - 1.0 # SM_CXSCREEN fScreenHeight = windll.user32.GetSystemMetrics(1) - 1.0 # SM_CYSCREEN dx = 65535.0 / fScreenWidth dy = 65535.0 / fScreenHeight fx = (center_x + mouse_move_x) * dx if not MOUSE_INVERSION: fy = (center_y + mouse_move_y) * dy else: fy = (center_y - mouse_move_y) * dy input = INPUT() input.type = INPUT_MOUSE input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE # using ceil() as recommended here: http://msdn.microsoft.com/en-us/library/ms646273%28v=VS.85%29.aspx input.mi.dx = int(ceil(fx)) input.mi.dy = int(ceil(fy)) windll.User32.SendInput(1, byref(input), sizeof(input)) return (mouse_move_x, mouse_move_y)
def GlobalMemoryStatusEx(): x = MEMORYSTATUSEX() x.dwLength = sizeof(x) windll.kernel32.GlobalMemoryStatusEx(byref(x)) return x
def dump_obj(o): s = string_at(byref(o), sizeof(o)) return dump(s)