python wia and RegisterEvent

gelonida gelonida at gmail.com
Thu Apr 15 17:15:34 EDT 2010


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 autogenerated file contains:
class IDeviceManager(DispatchBaseClass):
	CLSID = IID('{73856D9A-2720-487A-A584-21D5774E9D0F}')
	coclass_clsid = IID('{E1C5D730-7E97-4D8A-9E42-BBAE87C2059F}')

	def RegisterEvent(self, EventID=defaultNamedNotOptArg,
DeviceID=u'*'):
		"""Registers the specified EventID for the specified DeviceID. If
DeviceID is "*" then OnEvent will be called whenever the event
specified occurs for any device. Otherwise, OnEvent will only be
called if the event specified occurs on the device specified."""
		return self._ApplyTypes_(2, 1, (24, 32), ((8, 1), (8, 49)),
u'RegisterEvent', None,EventID
			, DeviceID)



but I do not know where to place (how to create the function)
OnEvent()


Any help would be appreciated.



More information about the Python-list mailing list