Question concerning makepy.py for COM (long, code included)

Scott Anderson sanderson at ttm.com
Wed Feb 9 10:19:57 EST 2000


Hi all,

I have a 32-bit ActiveX OCX control that I wish to use in Python. 
Please forgive any obvious ignorance on COM... I have, however, read the
QuickStarts and other related materials.

I ran the makepy.py script that comes with the win32com distribution
(the 128 release).  From the file produced, it is not entirely obvious
how to instantiate and use the objects.

The code produced is included below; I changed the names because I'm not
sure of the confidentiality, and I removed code that doesn't appear
germane to the question.

TSHSWAPI is the COM object that is actually used in, say, VB.  However,
_DTSHSWAPI is the one that appears to export the methods for that class.

So, the question is, how do I use the _DTSHSWAPI functionality from this
class?  Also, how is the event interface implemented?

Thanks muchly in advance,

-scott



# Created by makepy.py version 0.3.1 from xxxxx.ocx
# On date: Tue Feb 08 16:14:15 2000
makepy_version = '0.3.1'

[...]

class _DTSHSWAPI(DispatchBaseClass):
	CLSID = pythoncom.MakeIID('{the class ID 3}')

	def AboutBox(self):
		return self._ApplyTypes_(0xfffffdd8, 1, (24, 0), (), 'AboutBox', None)

	def AckHost(self):
		return self._ApplyTypes_(0x9, 1, (2, 0), (), 'AckHost', None)

	def CloseSession(self):
		return self._ApplyTypes_(0x4, 1, (2, 0), (), 'CloseSession', None)

	def EnableHost(self):
		return self._ApplyTypes_(0xb, 1, (2, 0), (), 'EnableHost', None)

	def GetCommStatus(self, CommType=defaultNamedNotOptArg):
		return self._ApplyTypes_(0x5, 1, (2, 0), ((2, 0),), 'GetCommStatus',
None, CommType)

	def OpenSession(self, HostType=defaultNamedNotOptArg,
DeviceType=defaultNamedNotOptArg, PortType=defaultNamedNotOptArg):
		return self._ApplyTypes_(0x2, 1, (2, 0), ((2, 0), (2, 0), (8, 0)),
'OpenSession', None, HostType, DeviceType, PortType)

	def RetrieveAvailableIATAs(self, ReturnString=defaultNamedNotOptArg,
ReturnStringLength=defaultNamedNotOptArg,
HostType=defaultNamedNotOptArg, DeviceType=defaultNamedNotOptArg,
PortType=defaultNamedNotOptArg, ReturnIATACount=defaultNamedNotOptArg):
		return self._ApplyTypes_(0xa, 1, (2, 0), (((26, 8), 0), (2, 0), (2,
0), (2, 0), (8, 0), ((26, 2), 0)), 'RetrieveAvailableIATAs', None,
ReturnString, ReturnStringLength, HostType, DeviceType, PortType,
ReturnIATACount)

	def RetrieveIATAString(self, ReturnString=defaultNamedNotOptArg,
ReturnStringLength=defaultNamedNotOptArg):
		return self._ApplyTypes_(0x7, 1, (2, 0), (((26, 8), 0), (2, 0)),
'RetrieveIATAString', None, ReturnString, ReturnStringLength)

	def RetrievePrintDeviceInfo(self,
WorkstationName=defaultNamedNotOptArg,
ReturnString=defaultNamedNotOptArg,
ReturnStringLength=defaultNamedNotOptArg):
		return self._ApplyTypes_(0x8, 1, (2, 0), ((8, 0), ((26, 8), 0), (2,
0)), 'RetrievePrintDeviceInfo', None, WorkstationName, ReturnString,
ReturnStringLength)

	def RetrieveProtocolType(self, ReturnString=defaultNamedNotOptArg,
ReturnStringLength=defaultNamedNotOptArg):
		return self._ApplyTypes_(0x6, 1, (2, 0), (((26, 8), 0), (2, 0)),
'RetrieveProtocolType', None, ReturnString, ReturnStringLength)

	def Send(self, Data=defaultNamedNotOptArg,
DataLength=defaultNamedNotOptArg):
		return self._ApplyTypes_(0x3, 1, (2, 0), ((8, 0), (3, 0)), 'Send',
None, Data, DataLength)

	_prop_map_get_ = {
		"ApplicationName": (1, 2, (8, 0), (), "ApplicationName", None),
	}
	_prop_map_put_ = {
		"ApplicationName" : ((1, LCID, 4, 0),()),
	}

class _DTSHSWAPIEvents:
	CLSID = CLSID_Sink = pythoncom.MakeIID('{the class ID 2}')
	_public_methods_ = [] # For COM Server support
	_arg_transformer_ = arg_transformer
	_dispid_to_func_ = {
		        1 : "OnSessionStatus",
		        2 : "OnHostStatus",
		        5 : "OnLanStatus",
		        4 : "OnDataAvailable",
		        3 : "OnMessageError",
		}

	def __init__(self, oobj = None):
		if oobj is None:
			self._olecp = None
		else:
			import win32com.server.util
		
cpc=oobj._oleobj_.QueryInterface(pythoncom.IID_IConnectionPointContainer)
			cp=cpc.FindConnectionPoint(self.CLSID_Sink)
			cookie=cp.Advise(win32com.server.util.wrap(self))
			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 OnSessionStatus(self, SubCondition=defaultNamedNotOptArg):
#	def OnHostStatus(self, SubCondition=defaultNamedNotOptArg):
#	def OnLanStatus(self, SubCondition=defaultNamedNotOptArg):
#	def OnDataAvailable(self, SubCondition=defaultNamedNotOptArg,
Data=defaultNamedNotOptArg, DataLength=defaultNamedNotOptArg,
EraseWrite=defaultNamedNotOptArg, LineAddress=defaultNamedNotOptArg):
#	def OnMessageError(self, SubCondition=defaultNamedNotOptArg):

[...]

# This CoClass is known by the name 'XXXXX.TSHSWAPICtrl.1'
class TSHSWAPI(CoClassBaseClass): # A CoClass
	CLSID = pythoncom.MakeIID("{the class ID 1}")
	coclass_sources = [
		_DTSHSWAPIEvents,
	]
	default_source = _DTSHSWAPIEvents
	coclass_interfaces = [
		_DTSHSWAPI,
	]
	default_interface = _DTSHSWAPI


[...]

CLSIDToClassMap = {
	'{the class ID 1}' : TSHSWAPI,
	'{the class ID 2}' : _DTSHSWAPIEvents,
	'{the class ID 3}' : _DTSHSWAPI,
}




More information about the Python-list mailing list