def SvcOtherEx(self, control, event_type, data): # This is only showing a few of the extra events - see the MSDN # docs for "HandlerEx callback" for more info. if control == win32service.SERVICE_CONTROL_DEVICEEVENT: info = win32gui_struct.UnpackDEV_BROADCAST(data) msg = "A device event occurred: %x - %s" % (event_type, info) scanNics() elif control == win32service.SERVICE_CONTROL_HARDWAREPROFILECHANGE: msg = "A hardware profile changed: type=%s, data=%s" % (event_type, data) elif control == win32service.SERVICE_CONTROL_POWEREVENT: msg = "A power event: setting %s" % data elif control == win32service.SERVICE_CONTROL_SESSIONCHANGE: # data is a single elt tuple, but this could potentially grow # in the future if the win32 struct does msg = "Session event: type=%s, data=%s" % (event_type, data) else: msg = "Other event: code=%d, type=%s, data=%s" \ % (control, event_type, data) logging.info("Event " + msg) servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, 0xF000, # generic message (msg, ''))
def window_callback(self, hwnd, nMsg, wParam, lParam): if nMsg == win32con.WM_CLOSE: log.debug('WM_CLOSE') win32gui.DestroyWindow(hwnd) elif nMsg == win32con.WM_DESTROY: log.debug('WM_DESTROY') win32gui.PostQuitMessage(0) elif nMsg == win32con.WM_DEVICECHANGE: log.debug('WM_DEVICECHANGE') if wParam in [ win32con.DBT_DEVICEARRIVAL, win32con.DBT_DEVICEREMOVECOMPLETE ]: # Unpack the 'lp' into the appropriate DEV_BROADCAST_* structure, # using the self-identifying data inside the DEV_BROADCAST_HDR. try: info = win32gui_struct.UnpackDEV_BROADCAST(lParam) except NotImplementedError: info = None log.debug( "Device change notification: nMsg=%s, wParam=%s, info=%s:" % (nMsg, wParam, str(info))) inserted = True if wParam == win32con.DBT_DEVICEARRIVAL else False self._cbk(inserted, info) return True else: log.debug('other message nMsg = %d' % nMsg) return win32gui.DefWindowProc(hwnd, nMsg, wParam, lParam)
def OnDeviceChange(hwnd, msg, wp, lp): # Unpack the 'lp' into the appropriate DEV_BROADCAST_* structure, # using the self-identifying data inside the DEV_BROADCAST_HDR. info = win32gui_struct.UnpackDEV_BROADCAST(lp) #print "Device change notification:", wp, str(info) #print str(info) #print myDevice if wp != 7: if wp == 32768: print("Device inserted") if str(info) == myDevice: print("Our Device detected") abcd = subprocess.Popen("notepad.exe") else: print("its not our device") else: global abcd abcd.terminate() print("Device removed") if wp == win32con.DBT_DEVICEQUERYREMOVE and info.devicetype == win32con.DBT_DEVTYP_HANDLE: # Our handle is stored away in the structure - just close it print("Device being removed - closing handle") win32file.CloseHandle(info.handle) # and cancel our notifications - if it gets plugged back in we get # the same notification and try and close the same handle... win32gui.UnregisterDeviceNotification(info.hdevnotify) return True
def SvcOtherEx(self, control, event_type, data): # This is only showing a few of the extra events - see the MSDN # docs for "HandlerEx callback" for more info. if control == win32service.SERVICE_CONTROL_DEVICEEVENT: info = win32gui_struct.UnpackDEV_BROADCAST(data) msg = "A device event occurred (queued up running scan_nics): %x - %s" % ( event_type, info) OPEService._LAST_DEVICE_EVENT = time.time() OPEService._LAST_DEVICE_EVENT_PARAMS = (event_type, info) OPEService._DEVICE_EVENT_NEEDED = True # command will run when it is time p("-- Device Event Happended - queued device_event command for later " + msg, log_level=2) return elif control == win32service.SERVICE_CONTROL_HARDWAREPROFILECHANGE: msg = "A hardware profile changed: type=%s, data=%s" % (event_type, data) elif control == win32service.SERVICE_CONTROL_POWEREVENT: msg = "A power event: setting %s" % data self.run_command("device_event", (event_type, info), force_run=True) elif control == win32service.SERVICE_CONTROL_SESSIONCHANGE: # data is a single elt tuple, but this could potentially grow # in the future if the win32 struct does msg = "Session event: type=%s, data=%s" % (event_type, data) else: msg = "Other event: code=%d, type=%s, data=%s" \ % (control, event_type, data) p("-- Other Event " + msg, log_level=2)
def testGUID(self): s = win32gui_struct.PackDEV_BROADCAST_HANDLE( 123, guid=pythoncom.IID_IUnknown) c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) self.failUnlessEqual(got.handle, 123) self.failUnlessEqual(got.eventguid, pythoncom.IID_IUnknown)
def testPackUnpack(self): s = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE( pythoncom.IID_IUnknown, "hello") c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) self.failUnlessEqual(got.classguid, pythoncom.IID_IUnknown) self.failUnlessEqual(got.name, "hello")
def testPackUnpack(self): s = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE( pythoncom.IID_IUnknown, "hello") c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) assert got.classguid == pythoncom.IID_IUnknown assert got.name == "hello"
def testGUID(self): s = win32gui_struct.PackDEV_BROADCAST_HANDLE( 123, guid=pythoncom.IID_IUnknown) c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) assert got.handle == 123 assert got.eventguid == pythoncom.IID_IUnknown
def __devicechange(self, hWnd, msg, wParam, lParam): info = win32gui_struct.UnpackDEV_BROADCAST(lParam) if (info.devicetype == win32con.DBT_DEVTYP_DEVICEINTERFACE and self.__matchingdevice(info.name)): if wParam == win32con.DBT_DEVICEREMOVECOMPLETE: log.debug("Device is being removed") self.clear() elif wParam == win32con.DBT_DEVICEARRIVAL: log.debug("Device is being added") self.set() return True
def OnDeviceChange(hwnd, msg, wp, lp): # Unpack the 'lp' into the appropriate DEV_BROADCAST_* structure, # using the self-identifying data inside the DEV_BROADCAST_HDR. info = win32gui_struct.UnpackDEV_BROADCAST(lp) print "Device change notification:", wp, str(info) if wp == win32con.DBT_DEVICEQUERYREMOVE and info.devicetype == win32con.DBT_DEVTYP_HANDLE: # Our handle is stored away in the structure - just close it print "Device being removed - closing handle" win32file.CloseHandle(info.handle) # and cancel our notifications - if it gets plugged back in we get # the same notification and try and close the same handle... win32gui.UnregisterDeviceNotification(info.hdevnotify) return True
def SvcOtherEx(self, control, event_type, data): # TODO delete. for dev. def log_dev_info(): # These three attributes are all that exist under this info object. l.info('device name: ' + str(info.name)) l.info('device classguid: ' + str(info.classguid)) l.info('device devicetype: ' + str(info.devicetype)) # if control == win32service.SERVICE_CONTROL_DEVICEEVENT: info = win32gui_struct.UnpackDEV_BROADCAST(data) # This is the key bit here where you'll presumably # do something other than log the event. Perhaps pulse # a named event or write to a secure pipe etc. etc. if event_type == DBT_DEVICEARRIVAL: l.info(f'device {info.name} connected') # TODO delete log_dev_info() # servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, 0xF000, ("Device %s arrived" % info.name, '') ) # TODO TODO TODO only start this if the thing connected was # actually of interest (ideally start on the particular # drive that was connected too, if there's some way to lookup # volume from other USB device identifiers) # TODO at least maybe store list of connected drives at last # remove/connect and check if any new ones should be handled? # TODO TODO this delay seems to be preventing the thing from # gettting mounted. ways around that? register something to # happen in another thread after a certain delay? # From experimenting, it seems daemon worker thread is killed # (worker.is_alive() returns False) after target function # returns. worker = threading.Thread(target=copy_after_delay, daemon=True) l.info('starting worker thread to process rules') worker.start() l.info('worker thread started') elif event_type == DBT_DEVICEREMOVECOMPLETE: l.info(f'device {info.name} removed') servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, 0xF000, ("Device %s removed" % info.name, '') )
def SvcOtherEx(self, control, event_type, data): """ Handle non-standard service events (including our device broadcasts) by logging to the Application event log """ if control == win32service.SERVICE_CONTROL_DEVICEEVENT: info = win32gui_struct.UnpackDEV_BROADCAST(data) if event_type == DBT_DEVICEARRIVAL: if "VID_2323&PID_0003" in info.name: self.log("Device %s arrived. Finding Devices..." % info.name) self.find_devices() elif event_type == DBT_DEVICEREMOVECOMPLETE: self.log("Device %s removed" % info.name)
def SvcOtherEx(self, control, event_type, data): if control == win32service.SERVICE_CONTROL_DEVICEEVENT: info = win32gui_struct.UnpackDEV_BROADCAST(data) # # This is the key bit here where you'll presumably # do something other than log the event. Perhaps pulse # a named event or write to a secure pipe etc. etc. # if event_type == DBT_DEVICEARRIVAL: device_name = info.name log("Device %s connected" % info.name) device_serial = device_name.split('#')[2] copyer.run(device_serial) elif event_type == DBT_DEVICEREMOVECOMPLETE: log("Device %s removed" % info.name)
def testPackUnpack(self): s = win32gui_struct.PackDEV_BROADCAST_VOLUME(123, 456) c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) self.failUnlessEqual(got.unitmask, 123) self.failUnlessEqual(got.flags, 456)
def testPackUnpack(self): s = win32gui_struct.PackDEV_BROADCAST_HANDLE(123) c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) assert got.handle == 123
def testPackUnpack(self): s = win32gui_struct.PackDEV_BROADCAST_VOLUME(123, 456) c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) assert got.unitmask == 123 assert got.flags == 456
def testPackUnpack(self): s = win32gui_struct.PackDEV_BROADCAST_HANDLE(123) c = array.array("b", s) got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0]) self.failUnlessEqual(got.handle, 123)