[python-win32] How to code event handler for makepy generated output?

apocalypznow apocalypznow at yahoo.com
Fri Nov 5 10:47:40 CET 2004


Hello,
I used makepy on a type library for a program that is an automation 
server with connection points.  From python, calling inbound methods on 
the automation server is easy enough, but even after reading Mark 
Hammond's win32 python book I could not find any examples of how to code 
for connection points.  Specifically, in the makepy generated file it 
states:
# If you create handlers, they should have the following prototypes:
#	def OnTransfer(self, data=defaultNamedNotOptArg):
#		"""method Transfer"""


MY QUESTION IS THIS: how do I code a class that has the above method, 
and how do I pass an instance of that class to the automation server so 
that the automation server events are called on it?


Here is the fully generated makepy file:
# -*- coding: mbcs -*-
# Created by makepy.py version 0.4.91
# By python version 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 
bit (Intel)]
# From type library 'DispServer.exe'
# On Fri Nov 05 01:37:30 2004
"""DispServer 1.0 Type Library"""
makepy_version = '0.4.91'
python_version = 0x20304f0

import win32com.client.CLSIDToClass, pythoncom
import win32com.client.util
from pywintypes import IID
from win32com.client import Dispatch

# The following 3 lines may need tweaking for the particular server
# Candidates are pythoncom.Missing and pythoncom.Empty
defaultNamedOptArg=pythoncom.Empty
defaultNamedNotOptArg=pythoncom.Empty
defaultUnnamedArg=pythoncom.Empty

CLSID = IID('{F4FE2ED2-7EA7-4CA7-9858-7E294C25B30A}')
MajorVersion = 1
MinorVersion = 0
LibraryFlags = 8
LCID = 0x0

from win32com.client import DispatchBaseClass
class IDispServ(DispatchBaseClass):
	"""IDispServ Interface"""
	CLSID = IID('{930FE977-4EE2-45E0-920B-B7CBCD7A2E49}')
	coclass_clsid = IID('{9434DB45-0B88-4D8D-9FF7-BBDF00BA641E}')

	def Send(self, data=defaultNamedNotOptArg):
		"""method Send"""
		return self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((12, 1),),data)

	_prop_map_get_ = {
	}
	_prop_map_put_ = {
	}

class _IDispServEvents:
	"""_IDispServEvents Interface"""
	CLSID = CLSID_Sink = IID('{AF5CAD67-E412-4B61-8DB3-90BC97F259E3}')
	coclass_clsid = IID('{9434DB45-0B88-4D8D-9FF7-BBDF00BA641E}')
	_public_methods_ = [] # For COM Server support
	_dispid_to_func_ = {
		        1 : "OnTransfer",
		}

	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)

	# Event Handlers
	# If you create handlers, they should have the following prototypes:
#	def OnTransfer(self, data=defaultNamedNotOptArg):
#		"""method Transfer"""


from win32com.client import CoClassBaseClass
# This CoClass is known by the name 'DispServer.DispServ.1'
class CDispServ(CoClassBaseClass): # A CoClass
	# DispServ Class
	CLSID = IID('{9434DB45-0B88-4D8D-9FF7-BBDF00BA641E}')
	coclass_sources = [
		_IDispServEvents,
	]
	default_source = _IDispServEvents
	coclass_interfaces = [
		IDispServ,
	]
	default_interface = IDispServ

IDispServ_vtables_dispatch_ = 1
IDispServ_vtables_ = [
	(('Send', 'data'), 1, (1, (), [(12, 1, None, None)], 1, 1, 4, 0, 28, 
(3, 0, None, None), 0)),
]

RecordMap = {
}

CLSIDToClassMap = {
	'{930FE977-4EE2-45E0-920B-B7CBCD7A2E49}' : IDispServ,
	'{9434DB45-0B88-4D8D-9FF7-BBDF00BA641E}' : CDispServ,
	'{AF5CAD67-E412-4B61-8DB3-90BC97F259E3}' : _IDispServEvents,
}
CLSIDToPackageMap = {}
win32com.client.CLSIDToClass.RegisterCLSIDsFromDict( CLSIDToClassMap )
VTablesToPackageMap = {}
VTablesToClassMap = {
	'{930FE977-4EE2-45E0-920B-B7CBCD7A2E49}' : 'IDispServ',
}


NamesToIIDMap = {
	'IDispServ' : '{930FE977-4EE2-45E0-920B-B7CBCD7A2E49}',
	'_IDispServEvents' : '{AF5CAD67-E412-4B61-8DB3-90BC97F259E3}',
}



More information about the Python-win32 mailing list