def __call__(self, *args): callbacks = self.handler._listeners.get(self.name, []) #print "event", self.name, callbacks for fn in callbacks: try: fn(self.handler._sender, Dispatch(args[0]), True) except: sys.stderr.write(traceback.print_exc()) sys.stderr.flush()
def __excel_app_from_hwnd(window_h): """ comtypes library is used to search windows handles for Excel application, then converts that pointer to a pywin object thru __comtype_to_pywin_obj() sometimes, non-Excel applications are running under the same window_h as an Excel process, like "print driver host for applications" these will fail to return a valid excel7_wnd for FindWindowEx, but killing these processes will also bring down the Excel application, which is not neccessarily corrupt """ global corrupt_hwnds if window_h in corrupt_hwnds: return None desk_wnd = FindWindowEx(window_h, None, xl_desk_class, None) excel7_wnd = FindWindowEx(desk_wnd, None, xl_excel7_class, None) if excel7_wnd == 0: corrupt_hwnds.add(window_h) if __is_excel_process(window_h): __kill_task(window_h) return None cls_id = GUID.from_progid(xl_clsid) obj_ptr = ctypes.POINTER(IDispatch)() AccessibleObjectFromWindow(excel7_wnd, native_om, byref(cls_id), byref(obj_ptr)) window = Dispatch(obj_ptr) try: com_obj = window.application excel_app = __comtype_to_pywin_obj(com_obj, IDispatch) excel_app = pywin_dispatch(excel_app) excel_app.Visible = True return excel_app except (COMError, com_error, NameError) as e: raise ChildProcessError( 'remote procedure call to Excel application rejected\n' '(check if cursor is still active within a cell somewhere, ' 'Excel will reject automation calls while waiting on ' 'user input)') from e
def getDOMParser(self): o = comtypes.client.CreateObject('MSXML.DOMDocument') return Dispatch(o)
def getXmlHttpRequest(self): #print "getXMLHttpRequest" o = comtypes.client.CreateObject('MSXML2.XMLHTTP.3.0') #print "getXMLHttpRequest", o return Dispatch(o)
def getDomDocument(self): return Dispatch(self.pBrowser.Document)
def _get_value(self, dynamic=False): vt = self.vt if vt in (VT_EMPTY, VT_NULL): return None elif vt == VT_I1: return self._.VT_I1 elif vt == VT_I2: return self._.VT_I2 elif vt == VT_I4: return self._.VT_I4 elif vt == VT_I8: return self._.VT_I8 elif vt == VT_UI8: return self._.VT_UI8 elif vt == VT_INT: return self._.VT_INT elif vt == VT_UI1: return self._.VT_UI1 elif vt == VT_UI2: return self._.VT_UI2 elif vt == VT_UI4: return self._.VT_UI4 elif vt == VT_UINT: return self._.VT_UINT elif vt == VT_R4: return self._.VT_R4 elif vt == VT_R8: return self._.VT_R8 elif vt == VT_BOOL: return self._.VT_BOOL elif vt == VT_BSTR: return self._.bstrVal elif vt == VT_DATE: days = self._.VT_R8 return datetime.timedelta(days=days) + _com_null_date elif vt == VT_CY: return self._.VT_CY / decimal.Decimal("10000") elif vt == VT_UNKNOWN: val = self._.c_void_p if not val: # We should/could return a NULL COM pointer. # But the code generation must be able to construct one # from the __repr__ of it. return None # XXX? ptr = cast(val, POINTER(IUnknown)) # cast doesn't call AddRef (it should, imo!) ptr.AddRef() return ptr.__ctypes_from_outparam__() elif vt == VT_DECIMAL: return self.decVal.as_decimal() elif vt == VT_DISPATCH: val = self._.c_void_p if not val: # See above. return None # XXX? ptr = cast(val, POINTER(IDispatch)) # cast doesn't call AddRef (it should, imo!) ptr.AddRef() if not dynamic: return ptr.__ctypes_from_outparam__() else: from comtypes.client.dynamic import Dispatch return Dispatch(ptr) # see also c:/sf/pywin32/com/win32com/src/oleargs.cpp elif self.vt & VT_BYREF: return self elif vt == VT_RECORD: from comtypes.client import GetModule from comtypes.typeinfo import IRecordInfo # Retrieving a COM pointer from a structure field does NOT # call AddRef(), have to call it manually: punk = self._.pRecInfo punk.AddRef() ri = punk.QueryInterface(IRecordInfo) # find typelib tlib = ri.GetTypeInfo().GetContainingTypeLib()[0] # load typelib wrapper module mod = GetModule(tlib) # retrive the type and create an instance value = getattr(mod, ri.GetName())() # copy data into the instance ri.RecordCopy(self._.pvRecord, byref(value)) return value elif self.vt & VT_ARRAY: typ = _vartype_to_ctype[self.vt & ~VT_ARRAY] return cast(self._.pparray, _midlSAFEARRAY(typ)).unpack() else: raise NotImplementedError("typecode %d = 0x%x)" % (vt, vt))