('base', MAIDDataInfo), ('fileDataType', ctypes.c_uint), ('totalLength', ctypes.c_uint), ('start', ctypes.c_uint), ('length', ctypes.c_uint), ('diskFile', ctypes.c_ubyte), ('removeObject', ctypes.c_ubyte), ] CCompletionProc = ctypes.WINFUNCTYPE( None, ctypes. c_void_p, # LPNkMAIDObject pObject, // module, source, item, or data object ctypes.c_ulong, # ULONG ulCommand, // Command, one of eNkMAIDCommand ctypes.c_ulong, # ULONG ulParam, // parameter for the command ctypes. c_ulong, # ULONG ulDataType, // Data type, one of eNkMAIDDataType ctypes.c_void_p, # NKPARAM data, // Pointer or long integer ctypes.c_void_p, # NKREF ref, // Reference set by client ctypes.c_long, # NKERROR nResult ) // One of eNkMAIDResult) ) def CompletionProc(f=None): def pyCompletionProc(obj, command, param, dataType, data, reference, result): print('COMPLETION', (obj, command, param, dataType, data, reference, result)) if f is not None: return f(obj=obj, command=command,
# # This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit # Source Code License included in this distribution package. See LICENSE. # By accessing, using, copying or modifying this work you indicate your # agreement to the Shotgun Pipeline Toolkit Source Code License. All rights # not expressly granted therein are reserved by Shotgun Software Inc. """ minimal set of win32 functions used by photoshop engine to manage toolkit UI under windows """ import ctypes from ctypes import wintypes # user32.dll EnumWindows = ctypes.windll.user32.EnumWindows EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)) GetWindowText = ctypes.windll.user32.GetWindowTextW GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW SendMessage = ctypes.windll.user32.SendMessageW SendMessageTimeout = ctypes.windll.user32.SendMessageTimeoutW GetWindowThreadProcessId = ctypes.windll.user32.GetWindowThreadProcessId SetParent = ctypes.windll.user32.SetParent RealGetWindowClass = ctypes.windll.user32.RealGetWindowClassW EnableWindow = ctypes.windll.user32.EnableWindow IsWindowEnabled = ctypes.windll.user32.IsWindowEnabled GetWindowLong = ctypes.windll.user32.GetWindowLongW SetWindowLong = ctypes.windll.user32.SetWindowLongW # kernal32.dll CloseHandle = ctypes.windll.kernel32.CloseHandle
("dwVolumeSerialNumber", ctypes.c_ulong), ("nFileSizeHigh", ctypes.c_ulong), ("nFileSizeLow", ctypes.c_ulong), ("nNumberOfLinks", ctypes.c_ulong), ("nFileIndexHigh", ctypes.c_ulong), ("nFileIndexLow", ctypes.c_ulong)] BY_HANDLE_FILE_INFORMATION = _BY_HANDLE_FILE_INFORMATION PBY_HANDLE_FILE_INFORMATION = ctypes.POINTER(_BY_HANDLE_FILE_INFORMATION) PSECURITY_INFORMATION = ctypes.POINTER(ctypes.c_ulong) PSECURITY_DESCRIPTOR = ctypes.c_void_p PFillFindData = ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.POINTER(wintypes.WIN32_FIND_DATAW), PDOKAN_FILE_INFO ) class dokan_operations(ctypes.Structure): _fields_ = [( "createFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, PDOKAN_FILE_INFO ) ), ( "openDirectory", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "createDirectory", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO)
HMODULE = HANDLE HWND = HANDLE HINSTANCE = HANDLE HHOOK = HANDLE ULONG_PTR = ctypes.POINTER(ctypes.c_ulong) LONG_PTR = ctypes.POINTER(ctypes.c_long) UINT_PTR = ctypes.POINTER(ctypes.c_uint) LONG = ctypes.c_long LPSTR = ctypes.c_char_p LRESULT = LONG_PTR WPARAM = UINT_PTR LPARAM = LONG_PTR SIZE_T = ULONG_PTR LPVOID = ctypes.c_void_p HOOKPROC = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, WPARAM, LPARAM) WNDENUMPROC = ctypes.WINFUNCTYPE(BOOL, HWND, LPARAM) LPTHREAD_START_ROUTINE = ctypes.WINFUNCTYPE(DWORD, LPVOID) class SECURITY_ATTRIBUTES(ctypes.Structure): _fields_ = [ ("nLength", DWORD), ("lpSecurityDescriptor", LPVOID), ("bInheritHandle", BOOL) ] LPSECURITY_ATTRIBUTES = ctypes.POINTER(SECURITY_ATTRIBUTES) class POINT(ctypes.Structure): _fields_ = [ ("x", LONG),
front = ida_idaapi._qvector_front back = ida_idaapi._qvector_back __iter__ = ida_idaapi._bounded_getitem_iterator idc_values_t_swigregister = _ida_expr.idc_values_t_swigregister idc_values_t_swigregister(idc_values_t) #<pycode(py_expr)> try: import types import ctypes # Callback for IDC func callback (On Windows, we use stdcall) # typedef error_t idaapi idc_func_t(idc_value_t *argv,idc_value_t *r); try: _IDCFUNC_CB_T = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p) except: _IDCFUNC_CB_T = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p) # A trampoline function that is called from idcfunc_t that will # call the Python callback with the argv and r properly serialized to python call_idc_func__ = ctypes.CFUNCTYPE(ctypes.c_long, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p)(_ida_expr.py_get_call_idc_func()) except: def call_idc_func__(*args): warning("IDC extensions need ctypes library in order to work") return 0 _IDCFUNC_CB_T = None # -------------------------------------------------------------------------- EXTFUN_BASE = 0x0001 """
GetMessageA = ctypes.windll.user32.GetMessageA GetMessageW = ctypes.windll.user32.GetMessageW GetModuleHandleA = ctypes.windll.kernel32.GetModuleHandleA GetModuleHandleW = ctypes.windll.kernel32.GetModuleHandleW CreateWindowExA = ctypes.windll.user32.CreateWindowExA CreateWindowExW = ctypes.windll.user32.CreateWindowExW RegisterClassExA = ctypes.windll.user32.RegisterClassExA RegisterClassExW = ctypes.windll.user32.RegisterClassExW DispatchMessageA = ctypes.windll.user32.DispatchMessageA DispatchMessageW = ctypes.windll.user32.DispatchMessageW CreateThread = ctypes.windll.kernel32.CreateThread SetLayeredWindowAttributes = ctypes.windll.user32.SetLayeredWindowAttributes LPTHREAD_START_ROUTINE = ctypes.WINFUNCTYPE(ctypes.wintypes.DWORD, ctypes.wintypes.LPVOID) WNDPROCTYPE = ctypes.WINFUNCTYPE(ctypes.c_int, wintypes.HWND, ctypes.c_uint, wintypes.WPARAM, wintypes.LPARAM) class WNDCLASSEX(ctypes.Structure): # tagWNDCLASSEXA _fields_ = [("cbSize" , ctypes.c_uint), ("style" , ctypes.c_uint), ("lpfnWndProc" , WNDPROCTYPE), ("cbClsExtra" , ctypes.c_int), ("cbWndExtra" , ctypes.c_int), ("hInstance" , wintypes.HANDLE), ("hIcon" , wintypes.HICON), ("hCursor" , wintypes.HANDLE), ("hbrBackground", wintypes.HBRUSH), ("lpszMenuName" , wintypes.LPCWSTR), ("lpszClassName", wintypes.LPCWSTR), ("hIconSm" , wintypes.HICON),]
def get_field(self): return ctypes.WINFUNCTYPE(self.restype, *self.argtypes)
def SetWindowsHookEx(idHook, lpFunc, hMod, dwThreadId): WINFUNC = ctypes.WINFUNCTYPE(ctypes.c_long, ctypes.c_long, ctypes.c_long, ctypes.c_long) return ctypes.windll.user32.SetWindowsHookExA(idHook, WINFUNC(lpFunc), hMod, dwThreadId)
def hook_dropfiles(tkwindow_or_winfoid, func=_func): """ # this func to deal drag icon & drop to load in windows *args: hwnd **kw: func = _func # default func just use path list print each one. # default each full_path_file_name type: bytes; in py3 # default each full_path_file_name type: str ; in py2 #=================== def _func(ls): for i in ls: print(i) #=================== test evironment: py2, py3 work on win7 32bit & 64bit. if you use tk, you can hook like this: ================================================= > import windnd > > def my_func(ls): for idx,i in enumerate(ls): print(idx,i) > > import tk > tk = tk.Tk() > hwnd = tk.winfo_id() > > # you don't have to write "hwnd = tk.winfo_id()" in tkinter > # because you can put "tk" in this function like: > # "windnd.hook_dropfiles(tk,func = my_func)" > # the reason for this is to expand interface > > windnd.hook_dropfiles(hwnd,func = my_func) > > tk.mainloop() ================================================= """ # this place just for expand interface # because may anther window tools need use hwnd to hook hwnd = tkwindow_or_winfoid.winfo_id()\ if getattr(tkwindow_or_winfoid,"winfo_id")\ else tkwindow_or_winfoid import platform import ctypes from ctypes.wintypes import DWORD prototype = ctypes.WINFUNCTYPE(DWORD, DWORD, DWORD, DWORD, DWORD) WM_DROPFILES = 0x233 GWL_WNDPROC = -4 def py_drop_func(hwnd, msg, wp, lp): global files if msg == WM_DROPFILES: count = ctypes.windll.shell32.DragQueryFile(wp, -1, None, None) szFile = ctypes.c_buffer(260) files = [] for i in range(count): ctypes.windll.shell32.DragQueryFile(wp, i, szFile, ctypes.sizeof(szFile)) dropname = szFile.value files.append(dropname) func(files) ctypes.windll.shell32.DragFinish(wp) return ctypes.windll.user32.CallWindowProcW(globals()[old], hwnd, msg, wp, lp) # for limit hook number, protect computer. limit_num = 200 for i in range(limit_num): if i + 1 == limit_num: raise "over hook limit number 200, for protect computer." if "old_wndproc_%d" % i not in globals(): old, new = "old_wndproc_%d" % i, "new_wndproc_%d" % i break globals()[old] = None globals()[new] = prototype(py_drop_func) if platform.architecture()[0] == "32bit": GetWindowLong = ctypes.windll.user32.GetWindowLongW SetWindowLong = ctypes.windll.user32.SetWindowLongW elif platform.architecture()[0] == "64bit": GetWindowLong = ctypes.windll.user32.GetWindowLongPtrW SetWindowLong = ctypes.windll.user32.SetWindowLongPtrW ctypes.windll.shell32.DragAcceptFiles(hwnd, True) globals()[old] = GetWindowLong(hwnd, GWL_WNDPROC) SetWindowLong(hwnd, GWL_WNDPROC, globals()[new])
from ctypes import wintypes, windll from time import sleep #################### # Constants #################### HWND = wintypes.HWND #window handler WPARAM = wintypes.WPARAM #message parameter LPARAM = wintypes.LPARAM #message parameter LRESULT = wintypes.DWORD #message result(signed type) LPCWSTR = wintypes.LPCWSTR #null-terminated string of 16-bit Unicode characters. UINT = wintypes.UINT POINTER = ctypes.POINTER INT = wintypes.INT BOOL = wintypes.BOOL WNDENUMPROC = ctypes.WINFUNCTYPE(BOOL, HWND, LPARAM) SMTO_BLOCK = 0x0001 WM_SYSCOMMAND = 0x0112 WM_GETTEXT = 0x000D WM_GETTEXTLENGTH = 0x000E WM_SETTEXT = 0x000C BM_CLICK = 0x00F5 SC_MINIMIZE = 0xF020 #################### # Win32 functions #################### """ FindWindow
def cwrap(name, restype, *args, **kw): params = (restype,) + tuple(x.typ for x in args) paramflags = tuple(x.spec for x in args) func = ctypes.WINFUNCTYPE(*params)((name, kw.get('lib', advapi32)), paramflags) func.errcheck = kw.get('errcheck', default_errcheck) return func
# All right reserved. # # This file is open source software distributed according to the terms in # LICENSE.txt # from __future__ import absolute_import import ctypes from ctypes.wintypes import ( BOOL, DWORD, HANDLE, HMODULE, LPCWSTR, WORD, HRSRC, HGLOBAL, LPVOID) from ._common import LONG_PTR, IS_INTRESOURCE from ._util import check_null, check_zero, check_false, function_factory, dlls _ENUMRESTYPEPROC = ctypes.WINFUNCTYPE(BOOL, HMODULE, LPVOID, LONG_PTR) _ENUMRESNAMEPROC = ctypes.WINFUNCTYPE(BOOL, HMODULE, LPVOID, LPVOID, LONG_PTR) _ENUMRESLANGPROC = ctypes.WINFUNCTYPE( BOOL, HMODULE, LPVOID, LPVOID, WORD, LONG_PTR) def ENUMRESTYPEPROC(callback): def wrapped(handle, type_, param): if IS_INTRESOURCE(type_): type_ = int(type_) else: type_ = ctypes.cast(type_, LPCWSTR).value return callback(handle, type_, param) return _ENUMRESTYPEPROC(wrapped)
gdi32 = ctypes.windll.gdi32 kernel32 = ctypes.windll.kernel32 from MessageNumbers import msgs, sgsm import ScintillaCallable import XiteMenu scintillaDirectory = ".." scintillaIncludeDirectory = os.path.join(scintillaDirectory, "include") scintillaScriptsDirectory = os.path.join(scintillaDirectory, "scripts") sys.path.append(scintillaScriptsDirectory) import Face scintillaBinDirectory = os.path.join(scintillaDirectory, "bin") WFUNC = ctypes.WINFUNCTYPE(c_int, HWND, c_uint, WPARAM, LPARAM) WS_CHILD = 0x40000000 WS_CLIPCHILDREN = 0x2000000 WS_OVERLAPPEDWINDOW = 0xcf0000 WS_VISIBLE = 0x10000000 WS_HSCROLL = 0x100000 WS_VSCROLL = 0x200000 WA_INACTIVE = 0 MF_POPUP = 16 MF_SEPARATOR = 0x800 IDYES = 6 OFN_HIDEREADONLY = 4 MB_OK = 0 MB_YESNOCANCEL = 3 MF_CHECKED = 8
if os.name == 'nt': """ Allow Windows to intecept KeyboardInterrupt http://stackoverflow.com/questions/15457786/ctrl-c-crashes-python-after-importing-scipy-stats """ basepath = imp.find_module('numpy')[1] try: lib1 = ctypes.CDLL(os.path.join(basepath, 'core', 'libmmd.dll')) lib2 = ctypes.CDLL(os.path.join(basepath, 'core', 'libifcoremd.dll')) def handler(sig, hook=thread.interrupt_main): hook() return 1 routine = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint)(handler) ctypes.windll.kernel32.SetConsoleCtrlHandler(routine, 1) except Exception: pass from .core import Oct2Py, Oct2PyError from .utils import Struct, get_log from .demo import demo from .speed_check import speed_check from .thread_check import thread_check try: octave = Oct2Py() except Oct2PyError as e: print(e)
import ctypes import ctypes_helper def errcheck(checker): def wrapped(result, func, args): print("result =", result) if checker(result): raise ctypes.WinError(ctypes.get_last_error()) return result return wrapped HOOKPROC = ctypes.WINFUNCTYPE( ctypes.wintypes.LRESULT, ctypes.c_int32, ctypes.wintypes.WPARAM, ctypes.wintypes.LPARAM) # https://msdn.microsoft.com/pt-br/library/windows/desktop/ms644990(v=vs.85).aspx _SetWindowsHookExW = ctypes.windll.user32.SetWindowsHookExW _SetWindowsHookExW.restype = ctypes.wintypes.HHOOK _SetWindowsHookExW.errcheck = errcheck(lambda x: x is None) _SetWindowsHookExW.argtypes = [ ctypes.c_int, # idHook HOOKPROC, # lpfn ctypes.wintypes.HINSTANCE, # hMod ctypes.wintypes.DWORD] # dwThreadId
def listen_forever(self): # This is to fix a problem with ascii encoding (windows with Unicode in # their titles) if six.PY2: reload(sys) sys.setdefaultencoding('utf8') # Look here for DWORD event constants: # http://stackoverflow.com/questions/15927262/convert-dword-event-constant-from-wineventproc-to-name-in-c-sharp # Don't worry, they work for python too. EVENT_SYSTEM_DIALOGSTART = 0x0010 WINEVENT_OUTOFCONTEXT = 0x0000 EVENT_SYSTEM_FOREGROUND = 0x0003 WINEVENT_SKIPOWNPROCESS = 0x0002 user32 = ctypes.windll.user32 ole32 = ctypes.windll.ole32 EnumWindows = ctypes.windll.user32.EnumWindows EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)) GetWindowText = ctypes.windll.user32.GetWindowTextW GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW IsWindowVisible = ctypes.windll.user32.IsWindowVisible ole32.CoInitialize(0) WinEventProcType = ctypes.WINFUNCTYPE( None, ctypes.wintypes.HANDLE, ctypes.wintypes.DWORD, ctypes.wintypes.HWND, ctypes.wintypes.LONG, ctypes.wintypes.LONG, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD ) def callback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime): length = GetWindowTextLength(hwnd) buff = ctypes.create_unicode_buffer(length + 1) GetWindowText(hwnd, buff, length + 1) # Notify observers self.observable.notify_observers(buff.value) WinEventProc = WinEventProcType(callback) user32.SetWinEventHook.restype = ctypes.wintypes.HANDLE hook = user32.SetWinEventHook( EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, 0, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS ) if hook == 0: print('SetWinEventHook failed') exit(1) msg = ctypes.wintypes.MSG() while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0: user32.TranslateMessageW(msg) user32.DispatchMessageW(msg) # Stopped receiving events, so clear up the winevent hook and uninitialise. print('Stopped receiving new window change events. Exiting...') user32.UnhookWinEvent(hook) ole32.CoUninitialize()
if os_windows: # By default, Ctrl+C does not work on Windows if we spawn subprocesses. # Here we fix that using WinApi. See https://stackoverflow.com/a/43095532 import signal import ctypes from ctypes import wintypes kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) # BOOL WINAPI HandlerRoutine( # _In_ DWORD dwCtrlType # ); PHANDLER_ROUTINE = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD) win_ignore_ctrl_c = PHANDLER_ROUTINE() # = NULL def _errcheck_bool(result, _, args): if not result: raise ctypes.WinError(ctypes.get_last_error()) return args # BOOL WINAPI SetConsoleCtrlHandler( # _In_opt_ PHANDLER_ROUTINE HandlerRoutine, # _In_ BOOL Add # ); kernel32.SetConsoleCtrlHandler.errcheck = _errcheck_bool kernel32.SetConsoleCtrlHandler.argtypes = (PHANDLER_ROUTINE, wintypes.BOOL)
def activar(): dgt = None for path in ( "", "C:/Program Files (x86)/DGT Projects/", "C:/Program Files (x86)/Common Files/DGT Projects/", "C:/Program Files/DGT Projects/" "C:/Program Files/Common Files/DGT Projects/", ): try: dgt = ctypes.WinDLL(path + "DGTEBDLL.dll") break except: pass if dgt is None: return False VarGen.dgt = dgt cmpfunc = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_char_p) st = cmpfunc(registerStatusFunc) dgt._DGTDLL_RegisterStatusFunc.argtype = [st] dgt._DGTDLL_RegisterStatusFunc.restype = ctypes.c_int dgt._DGTDLL_RegisterStatusFunc(st) cmpfunc = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_char_p) st = cmpfunc(registerScanFunc) dgt._DGTDLL_RegisterScanFunc.argtype = [st] dgt._DGTDLL_RegisterScanFunc.restype = ctypes.c_int dgt._DGTDLL_RegisterScanFunc(st) cmpfunc = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_char_p) st = cmpfunc(registerWhiteMoveInputFunc) dgt._DGTDLL_RegisterWhiteMoveInputFunc.argtype = [st] dgt._DGTDLL_RegisterWhiteMoveInputFunc.restype = ctypes.c_int dgt._DGTDLL_RegisterWhiteMoveInputFunc(st) cmpfunc = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_char_p) st = cmpfunc(registerBlackMoveInputFunc) dgt._DGTDLL_RegisterBlackMoveInputFunc.argtype = [st] dgt._DGTDLL_RegisterBlackMoveInputFunc.restype = ctypes.c_int dgt._DGTDLL_RegisterBlackMoveInputFunc(st) dgt._DGTDLL_WritePosition.argtype = [ctypes.c_char_p] dgt._DGTDLL_WritePosition.restype = ctypes.c_int dgt._DGTDLL_ShowDialog.argtype = [ctypes.c_int] dgt._DGTDLL_ShowDialog.restype = ctypes.c_int dgt._DGTDLL_HideDialog.argtype = [ctypes.c_int] dgt._DGTDLL_HideDialog.restype = ctypes.c_int dgt._DGTDLL_WriteDebug.argtype = [ctypes.c_bool] dgt._DGTDLL_WriteDebug.restype = ctypes.c_int dgt._DGTDLL_SetNRun.argtype = [ ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int ] dgt._DGTDLL_SetNRun.restype = ctypes.c_int dgt._DGTDLL_Exit.argtype = [] dgt._DGTDLL_Exit.restype = ctypes.c_int return True
import os from ctypes import POINTER, c_double, c_int, c_uint, c_void_p #### typedefs AT_H = ctypes.c_int AT_BOOL = ctypes.c_int AT_64 = ctypes.c_int64 AT_U8 = ctypes.c_uint8 AT_WC = ctypes.c_wchar _stdcall_libraries = {} if os.name in ("nt", "ce"): _stdcall_libraries["ATCORE"] = ctypes.WinDLL("atcore") _stdcall_libraries["ATUTIL"] = ctypes.WinDLL("atutility") CALLBACKTYPE = ctypes.WINFUNCTYPE(c_int, AT_H, POINTER(AT_WC), c_void_p) else: _stdcall_libraries["ATCORE"] = ctypes.CDLL("atcore.so") _stdcall_libraries["ATUTIL"] = ctypes.CDLL("atutility.so") CALLBACKTYPE = ctypes.CFUNCTYPE(c_int, AT_H, POINTER(AT_WC), c_void_p) #### Defines errorCodes = {} def errCode(name, value): errorCodes[value] = name AT_INFINITE = 0xFFFFFFFF AT_CALLBACK_SUCCESS = 0
def initlib(self): if self._initialized: return arch = platform.architecture()[0] winarch = "64bit" if platform.machine().endswith("64") else "32bit" if arch == "32bit" and winarch == "64bit": solis_path = r"C:\Program Files (x86)\Andor SOLIS" else: solis_path = r"C:\Program Files\Andor SOLIS" error_message = ( "The library is supplied with Andor Solis software, or {};\n" "Additional required libraries: atblkbx.dll, atcl_bitflow.dll, atdevapogee.dll, atdevregcam.dll, atusb_libusb.dll, atusb_libusb10.dll;\n" "{}").format(default_source_message, default_placing_message) self.lib = load_lib("atcore.dll", locations=(solis_path, "local", "global"), call_conv="stdcall", locally=True, error_message=error_message) lib = self.lib AT_H = ctypes.c_int AT_pWC = ctypes.c_wchar_p AT_BOOL = ctypes.c_int AT_64 = ctypes.c_int64 AT_U8p = ctypes.POINTER(ctypes.c_uint8) self.AndorSDK3_statuscodes = AndorSDK3_statuscodes wrapper = ctypes_wrap.CTypesWrapper(restype=ctypes.c_uint32, errcheck=errcheck()) def strprep(*args): return ctypes.cast(ctypes.create_unicode_buffer(512), AT_pWC) self.AT_InitialiseLibrary = wrapper(lib.AT_InitialiseLibrary) self.AT_FinaliseLibrary = wrapper(lib.AT_FinaliseLibrary) self.AT_Open = wrapper(lib.AT_Open, [ctypes.c_int, AT_H], ["idx", None]) self.AT_Close = wrapper(lib.AT_Close, [AT_H], ["handle"]) self.AT_IsImplemented = wrapper(lib.AT_IsImplemented, [AT_H, AT_pWC, AT_BOOL], ["handle", "feature", None]) self.AT_IsReadable = wrapper(lib.AT_IsReadable, [AT_H, AT_pWC, AT_BOOL], ["handle", "feature", None]) self.AT_IsWritable = wrapper(lib.AT_IsWritable, [AT_H, AT_pWC, AT_BOOL], ["handle", "feature", None]) self.AT_IsReadOnly = wrapper(lib.AT_IsReadOnly, [AT_H, AT_pWC, AT_BOOL], ["handle", "feature", None]) self.AT_SetInt = wrapper(lib.AT_SetInt, [AT_H, AT_pWC, AT_64], ["handle", "feature", "value"]) self.AT_GetInt = wrapper(lib.AT_GetInt, [AT_H, AT_pWC, AT_64], ["handle", "feature", None]) self.AT_GetIntMax = wrapper(lib.AT_GetIntMax, [AT_H, AT_pWC, AT_64], ["handle", "feature", None]) self.AT_GetIntMin = wrapper(lib.AT_GetIntMin, [AT_H, AT_pWC, AT_64], ["handle", "feature", None]) self.AT_SetFloat = wrapper(lib.AT_SetFloat, [AT_H, AT_pWC, ctypes.c_double], ["handle", "feature", "value"]) self.AT_GetFloat = wrapper(lib.AT_GetFloat, [AT_H, AT_pWC, ctypes.c_double], ["handle", "feature", None]) self.AT_GetFloatMax = wrapper(lib.AT_GetFloatMax, [AT_H, AT_pWC, ctypes.c_double], ["handle", "feature", None]) self.AT_GetFloatMin = wrapper(lib.AT_GetFloatMin, [AT_H, AT_pWC, ctypes.c_double], ["handle", "feature", None]) self.AT_SetBool = wrapper(lib.AT_SetBool, [AT_H, AT_pWC, AT_BOOL], ["handle", "feature", "value"]) self.AT_GetBool = wrapper(lib.AT_GetBool, [AT_H, AT_pWC, AT_BOOL], ["handle", "feature", None]) self.AT_SetEnumIndex = wrapper(lib.AT_SetEnumIndex, [AT_H, AT_pWC, ctypes.c_int], ["handle", "feature", "value"]) self.AT_SetEnumString = wrapper(lib.AT_SetEnumString, [AT_H, AT_pWC, AT_pWC], ["handle", "feature", "value"]) self.AT_GetEnumIndex = wrapper(lib.AT_GetEnumIndex, [AT_H, AT_pWC, ctypes.c_int], ["handle", "feature", None]) self.AT_GetEnumStringByIndex = wrapper( lib.AT_GetEnumStringByIndex, [AT_H, AT_pWC, ctypes.c_int, AT_pWC, ctypes.c_int], ["handle", "feature", "index", None, "length"], rvprep=[strprep], rvref=[False]) self.AT_IsEnumIndexAvailable = wrapper( lib.AT_IsEnumIndexAvailable, [AT_H, AT_pWC, ctypes.c_int, AT_BOOL], ["handle", "feature", "value", None]) self.AT_IsEnumIndexImplemented = wrapper( lib.AT_IsEnumIndexImplemented, [AT_H, AT_pWC, ctypes.c_int, AT_BOOL], ["handle", "feature", "value", None]) self.AT_GetEnumCount = wrapper(lib.AT_GetEnumCount, [AT_H, AT_pWC, ctypes.c_int], ["handle", "feature", None]) self.AT_SetString = wrapper(lib.AT_SetString, [AT_H, AT_pWC, AT_pWC], ["handle", "feature", "value"]) self.AT_GetString = wrapper(lib.AT_GetString, [AT_H, AT_pWC, AT_pWC, ctypes.c_int], ["handle", "feature", None, "length"], rvprep=[strprep], rvref=[False]) self.AT_GetStringMaxLength = wrapper(lib.AT_GetStringMaxLength, [AT_H, AT_pWC, ctypes.c_int], ["handle", "feature", None]) self.AT_Command = wrapper(lib.AT_Command, [AT_H, AT_pWC], ["handle", "command"]) self.AT_QueueBuffer = wrapper(lib.AT_QueueBuffer, [AT_H, AT_U8p, ctypes.c_int], ["handle", "buffer", "size"]) self.AT_WaitBuffer = wrapper( lib.AT_WaitBuffer, [AT_H, AT_U8p, ctypes.c_int, ctypes.c_uint], ["handle", None, None, "timeout"]) self.AT_Flush = wrapper(lib.AT_Flush, [AT_H], ["handle"]) self.c_callback = ctypes.WINFUNCTYPE(ctypes.c_int, AT_H, AT_pWC, ctypes.c_void_p) lib.AT_RegisterFeatureCallback.argtypes = [ AT_H, AT_pWC, self.c_callback, ctypes.c_voidp ] lib.AT_RegisterFeatureCallback.restype = ctypes.c_uint32 lib.AT_RegisterFeatureCallback.errcheck = errcheck() def AT_RegisterFeatureCallback(handle, feature, callback): def wrapped_callback(*args): try: callback(*args) return 0 except: return 1 cb = self.c_callback(wrapped_callback) lib.AT_RegisterFeatureCallback(handle, feature, cb, 0) return cb self.AT_RegisterFeatureCallback = AT_RegisterFeatureCallback lib.AT_UnregisterFeatureCallback.argtypes = [ AT_H, AT_pWC, self.c_callback, ctypes.c_voidp ] lib.AT_UnregisterFeatureCallback.restype = ctypes.c_uint32 lib.AT_UnregisterFeatureCallback.errcheck = errcheck() def AT_UnregisterFeatureCallback(handle, feature, cb): lib.AT_UnregisterFeatureCallback(handle, feature, cb, 0) self.AT_UnregisterFeatureCallback = AT_UnregisterFeatureCallback self._initialized = True
# # A small example how to use basic_dsp in a different language. # class VecResult(ctypes.Structure): _fields_ = [("resultCode", ctypes.c_int), ("result", ctypes.c_void_p)] lib = ctypes.WinDLL('basic_dsp.dll') new64Proto = ctypes.WINFUNCTYPE( ctypes.c_void_p, # Return type. ctypes.c_int, ctypes.c_int, ctypes.c_double, ctypes.c_ulong, ctypes.c_double) new64 = new64Proto(("new64", lib)) getValue64Proto = ctypes.WINFUNCTYPE( ctypes.c_double, # Return type. ctypes.c_void_p, ctypes.c_ulong) getValue64 = getValue64Proto(("get_value64", lib)) offset64Proto = ctypes.WINFUNCTYPE( VecResult, # Return type.
def get_process_hwnds(): # https://stackoverflow.com/questions/37501191/how-to-get-windows-window-names-with-ctypes-in-python user32 = ctypes.WinDLL('user32', use_last_error=True) def check_zero(result, func, args): if not result: err = ctypes.get_last_error() if err: raise ctypes.WinError(err) return args if not hasattr(wintypes, 'LPDWORD'): # PY2 wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD) WindowInfo = namedtuple('WindowInfo', 'title hwnd') WNDENUMPROC = ctypes.WINFUNCTYPE( wintypes.BOOL, wintypes.HWND, # _In_ hWnd wintypes.LPARAM, ) # _In_ lParam user32.EnumWindows.errcheck = check_zero user32.EnumWindows.argtypes = ( WNDENUMPROC, # _In_ lpEnumFunc wintypes.LPARAM, ) # _In_ lParam user32.IsWindowVisible.argtypes = (wintypes.HWND, ) # _In_ hWnd user32.GetWindowThreadProcessId.restype = wintypes.DWORD user32.GetWindowThreadProcessId.argtypes = ( wintypes.HWND, # _In_ hWnd wintypes.LPDWORD, ) # _Out_opt_ lpdwProcessId user32.GetWindowTextLengthW.errcheck = check_zero user32.GetWindowTextLengthW.argtypes = (wintypes.HWND, ) # _In_ hWnd user32.GetWindowTextW.errcheck = check_zero user32.GetWindowTextW.argtypes = ( wintypes.HWND, # _In_ hWnd wintypes.LPWSTR, # _Out_ lpString ctypes.c_int, ) # _In_ nMaxCount def list_windows(): '''Return a sorted list of visible windows.''' result = [] @WNDENUMPROC def enum_proc(hWnd, lParam): if user32.IsWindowVisible(hWnd): pid = wintypes.DWORD() tid = user32.GetWindowThreadProcessId(hWnd, ctypes.byref(pid)) length = user32.GetWindowTextLengthW(hWnd) + 1 title = ctypes.create_unicode_buffer(length) user32.GetWindowTextW(hWnd, title, length) current_pid = os.getpid() if pid.value == current_pid: result.append(WindowInfo(title.value, hWnd)) return True user32.EnumWindows(enum_proc, 0) return sorted(result) return list_windows()
#Embedded file name: c:\depot\games\branches\release\EVE-TRANQUILITY\carbon\common\lib\minime.py import sys import ctypes import blue import geo2 import math import time import log try: import trinity except (ImportError, RuntimeError): pass WNDPROC = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_long, ctypes.c_int, ctypes.c_int, ctypes.c_int) def LOWORD(dword): return dword & 65535 def HIWORD(dword): return dword >> 16 hwnd = None wndclass = None OnKeyDown = None OnKeyUp = None OnChar = None OnMouse = None
('ntmAvgWidth', wintypes.UINT)] class NEWTEXTMETRICEX(ctypes.Structure): # physical font attributes for True Type fonts # needed for FONTENUMPROC callback function _fields_ = [('ntmTm', NEWTEXTMETRIC), ('ntmFontSig', FONTSIGNATURE)] # type for a function that is called by the system for # each font during execution of EnumFontFamiliesEx FONTENUMPROC = ctypes.WINFUNCTYPE( ctypes.c_int, # return non-0 to continue enumeration, 0 to stop ctypes.POINTER(LOGFONT), ctypes.POINTER(NEWTEXTMETRICEX), wintypes.DWORD, # font type, a combination of # DEVICE_FONTTYPE # RASTER_FONTTYPE # TRUETYPE_FONTTYPE wintypes.LPARAM) # When running 64 bit windows, some types are not 32 bit, so Python/ctypes guesses wrong gdi32.EnumFontFamiliesExA.argtypes = [ wintypes.HDC, ctypes.POINTER(LOGFONT), FONTENUMPROC, wintypes.LPARAM, wintypes.DWORD ] def _enum_font_names(logfont, textmetricex, fonttype, param): """callback function to be executed during EnumFontFamiliesEx call for each font name. it stores names in global variable
''' Moudle for window management using only ctypes ''' import ctypes from ctypes import wintypes from collections import namedtuple USER32 = ctypes.WinDLL('user32', use_last_error=True) WindowInfo = namedtuple('WindowInfo', 'pid title') WNDENUMPROC = ctypes.WINFUNCTYPE( wintypes.BOOL, wintypes.HWND, # _In_ hWnd wintypes.LPARAM, ) # _In_ lParam def get_windows(): '''Return a sorted list of visible windows.''' result = [] @WNDENUMPROC def enum_proc_cb(hwnd, l_param): ''' Callback for enum windows ''' try: if USER32.IsWindowVisible(hwnd): pid = wintypes.DWORD() _ = USER32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid)) length = USER32.GetWindowTextLengthW(hwnd) + 1 title = ctypes.create_unicode_buffer(length) USER32.GetWindowTextW(hwnd, title, length) result.append(WindowInfo(pid.value, title.value))
def onleave(self, event=None): if config.getint('theme') > 1 and event.widget==self.w: self.w.attributes("-transparentcolor", 'grey4') self.theme_menubar.grid_remove() self.blank_menubar.grid(row=0, columnspan=2, sticky=tk.NSEW) # Run the app if __name__ == "__main__": # Ensure only one copy of the app is running under this user account. OSX does this automatically. Linux TODO. if platform == 'win32': import ctypes from ctypes.wintypes import * EnumWindows = ctypes.windll.user32.EnumWindows EnumWindowsProc = ctypes.WINFUNCTYPE(BOOL, HWND, LPARAM) GetClassName = ctypes.windll.user32.GetClassNameW GetClassName.argtypes = [HWND, LPWSTR, ctypes.c_int] GetWindowText = ctypes.windll.user32.GetWindowTextW GetWindowText.argtypes = [HWND, LPWSTR, ctypes.c_int] GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW GetProcessHandleFromHwnd = ctypes.windll.oleacc.GetProcessHandleFromHwnd SetForegroundWindow = ctypes.windll.user32.SetForegroundWindow ShowWindowAsync = ctypes.windll.user32.ShowWindowAsync def WindowTitle(h): if h: l = GetWindowTextLength(h) + 1 buf = ctypes.create_unicode_buffer(l) if GetWindowText(h, buf, l): return buf.value
import ctypes from ctypes import wintypes _kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) def _check_bool(result, func, args): if not result: raise ctypes.WinError(ctypes.get_last_error()) # else build final result from result, args, outmask, and # inoutmask. Typically it's just result, unless you specify # out/inout parameters in the prototype return args _HandlerRoutine = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD) _kernel32.SetConsoleCtrlHandler.errcheck = _check_bool _kernel32.SetConsoleCtrlHandler.argtypes = (_HandlerRoutine, wintypes.BOOL) _console_ctrl_handlers = {} def set_console_ctrl_handler(handler): if handler not in _console_ctrl_handlers: h = _HandlerRoutine(handler) _kernel32.SetConsoleCtrlHandler(h, True) _console_ctrl_handlers[handler] = h
_kernel32.GetModuleFileNameA.argtypes = [_HANDLE, ctypes.c_void_p, _DWORD] _kernel32.GetModuleFileNameA.restype = _DWORD _kernel32.CreateProcessA.argtypes = [ _LPCSTR, _LPCSTR, ctypes.c_void_p, ctypes.c_void_p, _BOOL, _DWORD, ctypes.c_void_p, _LPCSTR, ctypes.c_void_p, ctypes.c_void_p ] _kernel32.CreateProcessA.restype = _BOOL _kernel32.ExitProcess.argtypes = [_UINT] _kernel32.ExitProcess.restype = None _kernel32.GetCurrentProcessId.argtypes = [] _kernel32.GetCurrentProcessId.restype = _DWORD _SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD) _kernel32.SetConsoleCtrlHandler.argtypes = [_SIGNAL_HANDLER, _BOOL] _kernel32.SetConsoleCtrlHandler.restype = _BOOL _kernel32.GetStdHandle.argtypes = [_DWORD] _kernel32.GetStdHandle.restype = _HANDLE _kernel32.GetConsoleScreenBufferInfo.argtypes = [_HANDLE, ctypes.c_void_p] _kernel32.GetConsoleScreenBufferInfo.restype = _BOOL _advapi32.RegOpenKeyExA.argtypes = [ _HANDLE, _LPCSTR, _DWORD, _DWORD, ctypes.c_void_p ] _advapi32.RegOpenKeyExA.restype = _LONG _advapi32.RegQueryValueExA.argtypes = [
class dokan_operations(ctypes.Structure): _fields_ = [( "createFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong, PDOKAN_FILE_INFO ) ), ( "openDirectory", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "createDirectory", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "cleanup", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "closeFile", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "readFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.c_longlong, PDOKAN_FILE_INFO ) ), ( "writeFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.c_longlong, PDOKAN_FILE_INFO ) ), ( "flushFileBuffers", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "getFileInformation", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, PBY_HANDLE_FILE_INFORMATION, PDOKAN_FILE_INFO ) ), ( "findFiles", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, PFillFindData, PDOKAN_FILE_INFO ) ), ( "findFilesWithPattern", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_wchar_p, PFillFindData, PDOKAN_FILE_INFO ) ), ( "setFileAttributes", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_ulong, PDOKAN_FILE_INFO ) ), ( "setFileTime", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.POINTER(wintypes.FILETIME), ctypes.POINTER(wintypes.FILETIME), ctypes.POINTER(wintypes.FILETIME), PDOKAN_FILE_INFO ) ), ( "deleteFile", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "deleteDirectory", ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_wchar_p, PDOKAN_FILE_INFO) ), ( "moveFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_bool, PDOKAN_FILE_INFO ) ), ( "setEndOfFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_longlong, PDOKAN_FILE_INFO ) ), ( "setAllocationSize", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_longlong, PDOKAN_FILE_INFO ) ), ( "lockFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_longlong, ctypes.c_longlong, PDOKAN_FILE_INFO ) ), ( "unlockFile", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, ctypes.c_longlong, ctypes.c_longlong, PDOKAN_FILE_INFO ) ), ( "getDiskFreeSpace", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, PDOKAN_FILE_INFO ) ), ( "getVolumeInformation", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong), ctypes.c_void_p, ctypes.c_ulong, PDOKAN_FILE_INFO ) ), ("unmount", ctypes.WINFUNCTYPE(ctypes.c_int, PDOKAN_FILE_INFO)), ( "getFileSecurity", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, PSECURITY_INFORMATION, PSECURITY_DESCRIPTOR, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), PDOKAN_FILE_INFO ) ), ( "setFileSecurity", ctypes.WINFUNCTYPE( ctypes.c_int, ctypes.c_wchar_p, PSECURITY_INFORMATION, PSECURITY_DESCRIPTOR, ctypes.c_ulong, PDOKAN_FILE_INFO ) )]
# Starting with version 3.8, CPython does not consider the PATH environment # variable any more when resolving DLL paths. The following works with the default # installation of the Beckhoff TwinCAT ADS DLL. dll_path = os.environ["TWINCAT3DIR"] + "\\..\\AdsApi\\TcAdsDll" if platform.architecture()[0] == "64bit": dll_path += "\\x64" dlldir_handle = os.add_dll_directory(dll_path) try: _adsDLL = ctypes.WinDLL("TcAdsDll.dll") # type: ignore finally: if dlldir_handle: # Do not clobber the load path for other modules dlldir_handle.close() NOTEFUNC = ctypes.WINFUNCTYPE( # type: ignore ctypes.c_void_p, ctypes.POINTER(SAmsAddr), ctypes.POINTER(SAdsNotificationHeader), ctypes.c_ulong, ) elif platform_is_linux(): # try to load local adslib.so in favor to global one local_adslib = os.path.join(os.path.dirname(__file__), "adslib.so") if os.path.isfile(local_adslib): adslib = local_adslib else: adslib = "adslib.so" _adsDLL = ctypes.CDLL(adslib) NOTEFUNC = ctypes.CFUNCTYPE( None,