def getWindowProcessName(hwnd=None): """ Returns a unicode string containing the process name of the specified application window (executable path). If hwnd parameter is None, frontmost window will be queried. """ if hwnd is None: hwnd = win32gui.GetForegroundWindow() # Get PID so we can get process handle _, process_id = win32process.GetWindowThreadProcessId(hwnd) # Get process handle process_handle = win32api.OpenProcess( win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, process_id) if not process_handle: return None MAX_PATH = 260 MAX_BUFFER = 32767 # Following is preffered way of getting process filename on Windows Vista/7 try: kernel32_dll = windll.LoadLibrary("kernel32") _QueryFullProcessImageName = kernel32_dll.QueryFullProcessImageNameW PDWORD = wintypes.POINTER(wintypes.DWORD) _QueryFullProcessImageName.argtypes = [ wintypes.HANDLE, wintypes.DWORD, wintypes.LPVOID, PDWORD ] _QueryFullProcessImageName.restype = wintypes.BOOL except Exception, e: _QueryFullProcessImageName = None
def get_memory_usage(statics={}): # in bytes if os.name == 'nt': if not statics: from ctypes import wintypes, Structure, c_size_t, sizeof, WinError, windll, byref class PROCESS_MEMORY_COUNTERS(Structure): _fields_ = [("cb", wintypes.DWORD), ("PageFaultCount", wintypes.DWORD), ("PeakWorkingSetSize", c_size_t), ("WorkingSetSize", c_size_t), ("QuotaPeakPagedPoolUsage", c_size_t), ("QuotaPagedPoolUsage", c_size_t), ("QuotaPeakNonPagedPoolUsage", c_size_t), ("QuotaNonPagedPoolUsage", c_size_t), ("PagefileUsage", c_size_t), ("PeakPagefileUsage", c_size_t)] def __init__(self): self.cb = sizeof(self) windll.psapi.GetProcessMemoryInfo.argtypes = ( wintypes.HANDLE, wintypes.POINTER(PROCESS_MEMORY_COUNTERS), wintypes.DWORD) def get_working_set_size(): pmi = PROCESS_MEMORY_COUNTERS() if not windll.psapi.GetProcessMemoryInfo(-1, byref(pmi), sizeof(pmi)): raise WinError() return pmi.WorkingSetSize statics['GetWorkingSetSize'] = get_working_set_size return statics['GetWorkingSetSize']() else: import resource return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * (1 if sys.platform == 'darwin' else 1024) # kilobytes to bytes
def get_process_handle_count(proc_handle): assert isinstance(proc_handle, wintypes.HANDLE) GetProcessHandleCount = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HANDLE, wintypes.POINTER(wintypes.DWORD))(("GetProcessHandleCount", ctypes.windll.kernel32)) hndcnt = wintypes.DWORD() if not GetProcessHandleCount(proc_handle, ctypes.byref(hndcnt)): raise ctypes.WinError() return hndcnt.value
class KEYBDINPUT(ctypes.Structure): _fields_ = ( ('wVk', ctypes.WORD), ('wScan', ctypes.WORD), ('dwFlags', ctypes.DWORD), ('time', ctypes.DWORD), ('dwExtraInfo', ctypes.POINTER(ctypes.c_ulong)), )
class MOUSEINPUT(ctypes.Structure): _fields_ = ( ('dx', ctypes.c_long), ('dy', ctypes.c_long), ('mouseData', ctypes.DWORD), ('dwFlags', ctypes.DWORD), ('time', ctypes.DWORD), ('dwExtraInfo', ctypes.POINTER(ctypes.c_ulong)), )
class PROCESSENTRY32(ctypes.Structure): _fields_ = (('dwSize', wintypes.DWORD), ('cntUsage', wintypes.DWORD), ('th32ProcessID', wintypes.DWORD), ('th32DefaultHeapID', wintypes.POINTER(wintypes.ULONG)), ('th32ModuleID', wintypes.DWORD), ('cntThreads', wintypes.DWORD), ('th32ParentProcessID', wintypes.DWORD), ('pcPriClassBase', wintypes.LONG), ('dwFlags', wintypes.DWORD), ('szExeFile', wintypes.c_char * wintypes.MAX_PATH))
def WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite): """ Writes data to the specified file or input/output (I/O) device. This function is designed for both synchronous and asynchronous operation. For a similar function designed solely for asynchronous operation, see WriteFileEx. BOOL WINAPI WriteFile( _In_ HANDLE hFile, _In_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped ); """ func_ref = ctypes.windll.kernel32.WriteFile func_ref.argtypes = [wintypes.HANDLE, wintypes.LPCVOID, wintypes.DWORD, wintypes.POINTER(wintypes.DWORD), wintypes.LPVOID] func_ref.restype = wintypes.BOOL lpNumberOfBytesWritten = wintypes.DWORD() result = func_ref(hFile, lpBuffer, nNumberOfBytesToWrite, ctypes.byref(lpNumberOfBytesWritten), None) return (result, lpNumberOfBytesWritten.value)
SW_SHOWMINNOACTIVE = 7 SW_MAXIMIZE = 3 SW_MINIMIZE = 6 SW_RESTORE = 9 class RECT(ctypes.Structure): _fields_ = [ ('left', wintypes.LONG), ("top", wintypes.LONG), ("right", wintypes.LONG), ("bottom", wintypes.LONG), ] user32.GetWindowRect.argtypes = [wintypes.HWND, wintypes.POINTER(RECT)] user32.SetWindowPos.argtypes = [ wintypes.HWND, wintypes.HWND, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint ] HWND_TOP = 0 class POINT(ctypes.Structure): _fields_ = [ ("x", wintypes.LONG), ("y", wintypes.LONG), ]
wintypes.LPCTSTR = ctypes.POINTER(ctypes.c_char) wintypes.PHANDLE = ctypes.POINTER(wintypes.HANDLE) class __LUID(ctypes.Structure): """see: http://msdn.microsoft.com/en-us/library/windows/desktop/aa379261(v=vs.85).aspx """ _fields_ = [ ("LowPart", wintypes.DWORD), ("HighPart", wintypes.LONG), ] wintypes.LUID = __LUID wintypes.PLUID = wintypes.POINTER(wintypes.LUID) class __LUID_AND_ATTRIBUTES(ctypes.Structure): """see: http://msdn.microsoft.com/en-us/library/windows/desktop/aa379263(v=vs.85).aspx """ _fields_ = [ ("Luid", wintypes.LUID), ("Attributes", wintypes.DWORD), ] wintypes.LUID_AND_ATTRIBUTES = __LUID_AND_ATTRIBUTES wintypes.PLUID_AND_ATTRIBUTES = wintypes.POINTER(wintypes.LUID_AND_ATTRIBUTES)
#if defined CDECL_CALL_CONV /* Use the '_cdecl' calling convention */ #define PV_DECL __declspec(dllexport) PV_CDECL /* or '__stdcall' calling convention */ #else /* as appropriate. */ #define PV_DECL __declspec(dllexport) __stdcall #endif # /**************************** PVCAM Pointer Types ****************************/ #PV_PTR_DECL = ctw.c_void_p #PV_BUFP_DECL= ctw.c_void_p #/******************************** PVCAM Types ********************************/ PV_FAIL = 0 PV_OK = 1 rs_bool = ctww.USHORT rs_bool_ptr = ctww.POINTER(rs_bool) rs_bool_const_ptr = rs_bool_ptr char = ctw.c_char char_ptr = ctw.c_char_p char_const_ptr = char_ptr int8 = ctw.c_int8 int8_ptr = ctw.POINTER(int8) int8_const_ptr = int8_ptr uns8 = ctw.c_uint8 uns8_ptr = ctw.POINTER(uns8) uns8_const_ptr = uns8_ptr int16 = ctw.c_int16 int16_ptr = ctw.POINTER(int16) int16_const_ptr = int16_ptr uns16 = ctw.c_uint16 uns16_ptr = ctw.POINTER(uns16)