COM Question: How to respond to Windows events?

Steve Goldman steve_g at ix.netcom.com
Wed Sep 12 20:06:02 EDT 2001


Hi,

I'm trying to use a COM object that contains an object called OPCGroup with
an event called DataChange.  I can use the objects, call their methods, read
their properties, etc., but I have no idea how to respond to events.

In Visual Basic, I would create a subroutine called OPCGroup_DataChange and
put my event handler there.  Note:  I can make it work in VB.

I used makepy.py to create a .py file for the object library.  It has the
following event object in it.

****CODE STARTS*****

class DIOPCGroupEvent:
 "OPC Group Events"
 CLSID = CLSID_Sink =
pythoncom.MakeIID('{28E68F97-8D75-11D1-8DC3-3C302A000000}')
 _public_methods_ = [] # For COM Server support
 _dispid_to_func_ = {
          4 : "OnAsyncCancelComplete",
          1 : "OnDataChange",
          2 : "OnAsyncReadComplete",
          3 : "OnAsyncWriteComplete",
  }

 def __init__(self, oobj = None):
  if oobj is None:
   self._olecp = None
  else:
   import win32com.server.util
   from win32com.server.policy import EventHandlerPolicy
   cpc=oobj._oleobj_.QueryInterface(pythoncom.IID_IConnectionPointContainer)
   cp=cpc.FindConnectionPoint(self.CLSID_Sink)
   cookie=cp.Advise(win32com.server.util.wrap(self,
usePolicy=EventHandlerPolicy))
   self._olecp,self._olecp_cookie = cp,cookie
 def __del__(self):
  try:
   self.close()
  except pythoncom.com_error:
   pass
 def close(self):
  if self._olecp is not None:
   cp,cookie,self._olecp,self._olecp_cookie =
self._olecp,self._olecp_cookie,None,None
   cp.Unadvise(cookie)
 def _query_interface_(self, iid):
  import win32com.server.util
  if iid==self.CLSID_Sink: return win32com.server.util.wrap(self)

# Handlers for the control
# If you create handlers, they should have the following prototypes:
# def OnAsyncCancelComplete(self, CancelID=defaultNamedNotOptArg):
# def OnDataChange(self, TransactionID=defaultNamedNotOptArg,
NumItems=defaultNamedNotOptArg, ClientHandles=defaultNamedNotOptArg,
ItemValues=defaultNamedNotOptArg, Qualities=defaultNamedNotOptArg,
TimeStamps=defaultNamedNotOptArg):
# def OnAsyncReadComplete(self, TransactionID=defaultNamedNotOptArg,
NumItems=defaultNamedNotOptArg, ClientHandles=defaultNamedNotOptArg,
ItemValues=defaultNamedNotOptArg, Qualities=defaultNamedNotOptArg,
TimeStamps=defaultNamedNotOptArg, Errors=defaultNamedNotOptArg):
# def OnAsyncWriteComplete(self, TransactionID=defaultNamedNotOptArg,
NumItems=defaultNamedNotOptArg, ClientHandles=defaultNamedNotOptArg,
Errors=defaultNamedNotOptArg):

***CODE ENDS***

Can anyone tell me how to get my code to recognize the DataChange event?

This is the working code that dispatches the COM object

***CODE STARTS***

import win32com.client
s=win32com.client.Dispatch('OPC.Automation.1')
s.Connect('KEPware.KEPServerEx.V4')
groups=s.OPCGroups
group=groups.Add('Group1')
items=group.OPCItems
item=items.AddItem('Test_Group.Test_Device.TI27101',1)

***CODE ENDS***

Thanks.  Sorry for the length of the post.





More information about the Python-list mailing list