python wia and RegisterEvent

Mark Hammond skippy.hammond at gmail.com
Fri Apr 16 03:16:44 CEST 2010


On 16/04/2010 7:15 AM, gelonida wrote:
> Hi,
>
> I'd like to register an event in order to be informed, whenever a
> Windows WIA device is connected or disconnected.
>
> In python I can use WIA devices, but I don't know how to register
> events
>
> I have existing C# code, which works and looks like.
>
>     class MgrHandlerClass
>      {
>          public void devManager_OnEvent(string eventID, string
> deviceID, string itemID)
>          {    } // her emy handler code
>
>          public void addhandler(DeviceManager manager)
>          {
>              Console.WriteLine("adding handler!");
>              manager.OnEvent += new
> WIA._IDeviceManagerEvents_OnEventEventHandler(
>              devManager_OnEvent);
>          }
>
>          static void Main(string[] args)
>          {
>
>              MgrHandlerClass mgrhndlr;
>              mgrhndlr = new MgrClass();
>              DeviceManager manager;
>
>               manager = new DeviceManagerClass();
>
> manager.RegisterEvent(WIA.EventID.wiaEventDeviceConnected, "*");
>
> manager.RegisterEvent(WIA.EventID.wiaEventDeviceDisconnected, "*");
>               mgrhndlr.addhandler(manager);
>              // wait for callbacks or do other stuff
>          }
>
>
> Now in python I'm stuck
>
> import win32com.client
> import wia_defs # created with makepy.py
> manager = win32com.client.Dispatch("WIA.DeviceManager")
> mgrhndlr = MgrClass()
> manager.RegisterEvent(EventID=constants.wiaEventDeviceConnected,DeviceID=u'*')
> manager.RegisterEvent(EventID=constants.wiaEventDeviceDisconnected,DeviceID=u'*')
> mgrhndlr.addhandler(manager)
>
>
> class MgrHandlerClass:
>      def OnEvent(self, EventID=defaultNamedNotOptArg,
> DeviceID=defaultNamedNotOptArg, ItemID=defaultNamedNotOptArg):
>          print "Called back with (%s) (%s) (%s)" %
> (EventID,DeviceID,ItemID)
>
>      def addhandler(self,manager):
>          # here I'm stuck :-(
>          # the following line doesn't work as it seems, that manager
> does not have attr
>          # onevent
>
> manager.OnEvent.append( WIA._IDeviceManagerEvents_OnEventEventHandler(self.OnEvent) )

The model used by pywin32 is more "low level" than that exposed by some 
of the MS languages.  You probably need something closer to:


  class MgrHandlerClass:
       def OnEvent(self, EventID=defaultNamedNotOptArg, ...):
           print "Called back with ..."


manager = win32com.client.DispatchWithEvents("WIA.DeviceManager", 
MgrHandlerClass)
manager.RegisterEvent(EventID=constants.wiaEventDeviceConnected,DeviceID=u'*')
manager.RegisterEvent(EventID=constants.wiaEventDeviceDisconnected,DeviceID=u'*')

And magically your OnEvent should be called when the event happens. 
Googling for 'DispatchWithEvents' might find some good hits for more 
information.

HTH,

Mark



More information about the Python-list mailing list