[python-win32] Trying to implement an third-party interface in COM for events

Kapil fadnis kfadnis at gmail.com
Mon Apr 13 18:13:06 CEST 2009


Thanks Mark. It did not make a difference. Also, when i wrap a class using
win32.server.util.wrap what does it do,internally ? Is there somewhere I can
read about this ?
Here is the *.py generated file. Please let me know if the implementation is
correct based on this file.
----------------------------------------------------------------------------------------------------------------------------------------------------
# -*- coding: mbcs -*-
# Created by makepy.py version 0.4.98
# By python version 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32
bit (Intel)]
# From type library 'MbtQuote.dll'
# On Fri Apr 10 11:15:24 2009
"""MbtQuote 2.0 Type Library"""
makepy_version = '0.4.98'
python_version = 0x20504f0
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, .Empty and .ArgNotFound
defaultNamedOptArg=pythoncom.Empty
defaultNamedNotOptArg=pythoncom.Empty
defaultUnnamedArg=pythoncom.Empty
CLSID = IID('{13F76618-D199-4485-8C95-DC524958686C}')
MajorVersion = 2
MinorVersion = 0
LibraryFlags = 8
LCID = 0x0
class constants:
 csConnected                   =0x2        # from enum enumConnectionState
 csConnecting                  =0x1        # from enum enumConnectionState
 csDisconnected                =0x0        # from enum enumConnectionState
 csILG                         =-1         # from enum enumConnectionState
 csIndeterminate               =0x5        # from enum enumConnectionState
 csLoggedIn                    =0x4        # from enum enumConnectionState
 csLoggingIn                   =0x3        # from enum enumConnectionState
 csMAX                         =0x6        # from enum enumConnectionState
 msAsk                         =0x2        # from enum enumMarketSide
 msBid                         =0x1        # from enum enumMarketSide
 pcBoth                        =0x2        # from enum enumPutCall
 pcCall                        =0x0        # from enum enumPutCall
 pcPut                         =0x1        # from enum enumPutCall
 qsfLevelOne                   =0x1        # from enum enumQuoteServiceFlags
 qsfLevelTwo                   =0x2        # from enum enumQuoteServiceFlags
 qsfOptions                    =0x8        # from enum enumQuoteServiceFlags
 qsfTimeAndSales               =0x4        # from enum enumQuoteServiceFlags
 tickDown                      =0x1        # from enum enumTick
 tickEvenUp                    =0x0        # from enum enumTick
 tcQuote_Closing               =0x69       # from enum enumTickCondition
 tcQuote_DueToRelated          =0x70       # from enum enumTickCondition
 tcQuote_FastMarket            =0x6b       # from enum enumTickCondition
 tcQuote_Halted                =0x80       # from enum enumTickCondition
 tcQuote_NewsDistribution      =0x6a       # from enum enumTickCondition
 tcQuote_NewsPending           =0x6f       # from enum enumTickCondition
 tcQuote_NoOpenResume          =0x73       # from enum enumTickCondition
 tcQuote_NonFirm               =0x6e       # from enum enumTickCondition
 tcQuote_OfferDepth            =0x65       # from enum enumTickCondition
 tcQuote_Opening               =0x68       # from enum enumTickCondition
 tcQuote_OrderImbalance        =0x72       # from enum enumTickCondition
 tcQuote_OutOfSequence         =0x89       # from enum enumTickCondition
 tcQuote_QuoteRequest          =0x95       # from enum enumTickCondition
 tcQuote_QuoteResume           =0x96       # from enum enumTickCondition
 tcQuote_RegularQuote          =0x64       # from enum enumTickCondition
 tcQuote_Restricted            =0x8a       # from enum enumTickCondition
 tcQuote_Suspended             =0x88       # from enum enumTickCondition
 tcQuote_TradeResume           =0x97       # from enum enumTickCondition
 tcTrade_Acquistion            =0x1        # from enum enumTickCondition
 tcTrade_AutoExecute           =0x1f       # from enum enumTickCondition
 tcTrade_AveragePrice          =0x35       # from enum enumTickCondition
 tcTrade_BasketIndexClose      =0x37       # from enum enumTickCondition
 tcTrade_Bunched               =0x16       # from enum enumTickCondition
 tcTrade_BunchedSold           =0x17       # from enum enumTickCondition
 tcTrade_CapElectionTrade      =0x2e       # from enum enumTickCondition
 tcTrade_Cash_Sale             =0x5        # from enum enumTickCondition
 tcTrade_ClosePrice            =0x2f       # from enum enumTickCondition
 tcTrade_DerivativelyPriced    =0x2d       # from enum enumTickCondition
 tcTrade_DirectPlus            =0x2a       # from enum enumTickCondition
 tcTrade_Distribution          =0x2        # from enum enumTickCondition
 tcTrade_FormT                 =0x1d       # from enum enumTickCondition
 tcTrade_IntermarketSweep      =0x2c       # from enum enumTickCondition
 tcTrade_IntradayDetail        =0x20       # from enum enumTickCondition
 tcTrade_LateOpen              =0x1e       # from enum enumTickCondition
 tcTrade_MidPrice              =0x1b       # from enum enumTickCondition
 tcTrade_NextDaySale           =0x6        # from enum enumTickCondition
 tcTrade_OpenSale              =0xa        # from enum enumTickCondition
 tcTrade_OpeningDetail         =0x21       # from enum enumTickCondition
 tcTrade_OutOfSequence         =0xb        # from enum enumTickCondition
 tcTrade_PriorReferencePrice   =0x29       # from enum enumTickCondition
 tcTrade_RegularSale           =0x0        # from enum enumTickCondition
 tcTrade_Reopen                =0x11       # from enum enumTickCondition
 tcTrade_Rule127               =0x22       # from enum enumTickCondition
 tcTrade_Rule155               =0x8        # from enum enumTickCondition
 tcTrade_SellerSale            =0x7        # from enum enumTickCondition
 tcTrade_Settlement            =0x1c       # from enum enumTickCondition
 tcTrade_SoldLastSale          =0x9        # from enum enumTickCondition
 tcTrade_Split                 =0x3        # from enum enumTickCondition
 tcTrade_Stopped               =0x15       # from enum enumTickCondition
 tcTrade_YellowFlag            =0x2b       # from enum enumTickCondition
 tsDeleted                     =0x3        # from enum enumTickStatus
 tsFiltered                    =0x1        # from enum enumTickStatus
 tsNormal                      =0x0        # from enum enumTickStatus
 tsOutOfSeq                    =0x2        # from enum enumTickStatus
 ttAskTick                     =0x2        # from enum enumTickType
 ttBidTick                     =0x1        # from enum enumTickType
 ttTradeTick                   =0x0        # from enum enumTickType
from win32com.client import DispatchBaseClass
class IMbtQuotes(DispatchBaseClass):
 """IMbtQuotes Interface"""
 CLSID = IID('{7DEAC3B3-CA70-4E48-8552-E5CCA5493B71}')
 coclass_clsid = IID('{A58A9CE8-7681-46FB-8064-35D6DF9CEA0C}')
 def AdviseSymbol(self, pNotify=defaultNamedNotOptArg,
bstrSymbol=defaultNamedNotOptArg, lServiceFlags=defaultNamedNotOptArg):
  """Adds given symbol to interest list and registers callback interface to
receive its quotes."""
  return self._oleobj_.InvokeTypes(2, LCID, 1, (24, 0), ((13, 1), (8, 0),
(3, 0)),pNotify
   , bstrSymbol, lServiceFlags)
 def Connect(self):
  """Forces a reconnection to quote servers."""
  return self._oleobj_.InvokeTypes(1, LCID, 1, (3, 0), (),)
 def Disconnect(self):
  """Disconnects from quote servers."""
  return self._oleobj_.InvokeTypes(5, LCID, 1, (24, 0), (),)
 def GetCachedAggregateLevel2Size(self, bstrSymbol=defaultNamedNotOptArg,
dPrice=defaultNamedNotOptArg, side=defaultNamedNotOptArg):
  """Returns the cached aggregate Level II size at the given price/side."""
  return self._oleobj_.InvokeTypes(13, LCID, 1, (3, 0), ((8, 1), (5, 0), (3,
0)),bstrSymbol
   , dPrice, side)
 def GetCachedOptionsRecord(self, bstrSymbol=defaultNamedNotOptArg):
  """Returns a cached options record for the given options symbol if one
exists."""
  return self._ApplyTypes_(12, 1, (36, 0), ((8, 1),),
u'GetCachedOptionsRecord', None,bstrSymbol
   )
 def GetCachedQuote(self, bstrSymbol=defaultNamedNotOptArg):
  """Returns a cached level 1/fundamental quote for the given symbol if one
exists."""
  return self._ApplyTypes_(6, 1, (36, 0), ((8, 1),), u'GetCachedQuote',
None,bstrSymbol
   )
 def GetSingleQuote(self, bstrSymbol=defaultNamedNotOptArg,
lTimeout=defaultNamedNotOptArg):
  """Blocks and returns a level one quote for the given symbol subject to
timeout."""
  return self._ApplyTypes_(14, 1, (36, 0), ((8, 1), (3, 0)),
u'GetSingleQuote', None,bstrSymbol
   , lTimeout)
 def UnadviseAll(self, pNotify=defaultNamedNotOptArg):
  """Removes interest for ALL symbols that were registered to the given
callback interface."""
  return self._oleobj_.InvokeTypes(4, LCID, 1, (24, 0), ((13, 1),),pNotify
   )
 def UnadviseSymbol(self, pNotify=defaultNamedNotOptArg,
bstrSymbol=defaultNamedNotOptArg, lServiceFlags=defaultNamedNotOptArg):
  """Removes given symbol from interest list (for the given callback
interface only)."""
  return self._oleobj_.InvokeTypes(3, LCID, 1, (24, 0), ((13, 1), (8, 0),
(3, 0)),pNotify
   , bstrSymbol, lServiceFlags)
 _prop_map_get_ = {
  "ConnectionState": (11, 2, (3, 0), (), "ConnectionState", None),
  "RemoteAddress": (17, 2, (8, 0), (), "RemoteAddress", None),
  "ServerName": (16, 2, (8, 0), (), "ServerName", None),
 }
 _prop_map_put_ = {
 }
class _IMbtQuotesEvents:
 """_IMbtQuotesEvents Interface"""
 CLSID = CLSID_Sink = IID('{C13BD25A-B87F-4EFF-924C-F7109261A394}')
 coclass_clsid = IID('{A58A9CE8-7681-46FB-8064-35D6DF9CEA0C}')
 _public_methods_ = [] # For COM Server support
 _dispid_to_func_ = {
          2 : "OnClose",
          3 : "OnLogonSucceed",
          1 : "OnConnect",
  }
 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 OnClose(self, lErrorCode=defaultNamedNotOptArg):
#  """Event raised when a quote server connection has been dropped."""
# def OnLogonSucceed(self):
#  """Event raised when successful login to a quote server has occurred."""
# def OnConnect(self, lErrorCode=defaultNamedNotOptArg):
#  """Event raised when a new connection has been made to a quote server."""

from win32com.client import CoClassBaseClass
# This CoClass is known by the name 'MBTrading.MbtQuotes.1'
class MbtQuotes(CoClassBaseClass): # A CoClass
 # Provides realtime quote services.
 CLSID = IID('{A58A9CE8-7681-46FB-8064-35D6DF9CEA0C}')
 coclass_sources = [
  _IMbtQuotesEvents,
 ]
 default_source = _IMbtQuotesEvents
 coclass_interfaces = [
  IMbtQuotes,
 ]
 default_interface = IMbtQuotes
IMbtQuotes_vtables_dispatch_ = 1
IMbtQuotes_vtables_ = [
 (( u'Connect' , u'plRetVal' , ), 1, (1, (), [ (16387, 10, None, None) , ],
1 , 1 , 4 , 0 , 28 , (3, 0, None, None) , 0 , )),
 (( u'AdviseSymbol' , u'pNotify' , u'bstrSymbol' , u'lServiceFlags' , ), 2,
(2, (), [
   (13, 1, None, "IID('{C8323838-B094-4E3C-B9BE-D08B5C06E6BE}')") , (8, 0,
None, None) , (3, 0, None, None) , ], 1 , 1 , 4 , 0 , 32 , (3, 0, None,
None) , 0 , )),
 (( u'UnadviseSymbol' , u'pNotify' , u'bstrSymbol' , u'lServiceFlags' , ),
3, (3, (), [
   (13, 1, None, "IID('{C8323838-B094-4E3C-B9BE-D08B5C06E6BE}')") , (8, 0,
None, None) , (3, 0, None, None) , ], 1 , 1 , 4 , 0 , 36 , (3, 0, None,
None) , 0 , )),
 (( u'UnadviseAll' , u'pNotify' , ), 4, (4, (), [ (13, 1, None,
"IID('{C8323838-B094-4E3C-B9BE-D08B5C06E6BE}')") , ], 1 , 1 , 4 , 0 , 40 ,
(3, 0, None, None) , 0 , )),
 (( u'Disconnect' , ), 5, (5, (), [ ], 1 , 1 , 4 , 0 , 44 , (3, 0, None,
None) , 0 , )),
 (( u'GetCachedQuote' , u'bstrSymbol' , u'pRec' , ), 6, (6, (), [ (8, 1,
None, None) ,
   (36, 10, None, None) , ], 1 , 1 , 4 , 0 , 48 , (3, 0, None, None) , 0 ,
)),
 (( u'ConnectionState' , u'pVal' , ), 11, (11, (), [ (16387, 10, None, None)
, ], 1 , 2 , 4 , 0 , 52 , (3, 0, None, None) , 0 , )),
 (( u'GetCachedOptionsRecord' , u'bstrSymbol' , u'pRec' , ), 12, (12, (), [
(8, 1, None, None) ,
   (36, 10, None, None) , ], 1 , 1 , 4 , 0 , 56 , (3, 0, None, None) , 0 ,
)),
 (( u'GetCachedAggregateLevel2Size' , u'bstrSymbol' , u'dPrice' , u'side' ,
u'pVal' ,
   ), 13, (13, (), [ (8, 1, None, None) , (5, 0, None, None) , (3, 0, None,
None) , (16387, 10, None, None) , ], 1 , 1 , 4 , 0 , 60 , (3, 0, None, None)
, 0 , )),
 (( u'GetSingleQuote' , u'bstrSymbol' , u'lTimeout' , u'pRec' , ), 14, (14,
(), [
   (8, 1, None, None) , (3, 0, None, None) , (36, 10, None, None) , ], 1 , 1
, 4 , 0 , 64 , (3, 0, None, None) , 0 , )),
 (( u'ServerName' , u'pVal' , ), 16, (16, (), [ (16392, 10, None, None) , ],
1 , 2 , 4 , 0 , 68 , (3, 0, None, None) , 0 , )),
 (( u'RemoteAddress' , u'pVal' , ), 17, (17, (), [ (16392, 10, None, None) ,
], 1 , 2 , 4 , 0 , 72 , (3, 0, None, None) , 0 , )),
]
IMbtQuotesNotify_vtables_dispatch_ = 0
IMbtQuotesNotify_vtables_ = [
 (( u'OnQuoteData' , u'pRec' , ), 1610678272, (1610678272, (), [ (36, 1,
None, None) , ], 1 , 1 , 4 , 0 , 12 , (3, 0, None, None) , 0 , )),
 (( u'OnLevel2Data' , u'pRec' , ), 1610678273, (1610678273, (), [ (36, 1,
None, None) , ], 1 , 1 , 4 , 0 , 16 , (3, 0, None, None) , 0 , )),
 (( u'OnTSData' , u'pRec' , ), 1610678274, (1610678274, (), [ (36, 1, None,
None) , ], 1 , 1 , 4 , 0 , 20 , (3, 0, None, None) , 0 , )),
 (( u'OnOptionsData' , u'pRec' , ), 1610678275, (1610678275, (), [ (36, 1,
None, None) , ], 1 , 1 , 4 , 0 , 24 , (3, 0, None, None) , 0 , )),
]
RecordMap = {
 u'LEVEL2RECORD': '{6CE8D5AD-1352-4ECC-B9E3-145E796FE4D1}',
 u'TSRECORD': '{9B55F875-A3A7-476E-8D18-55961A7AF0ED}',
 u'OPTIONSRECORD': '{345495FA-527D-4A50-A8C4-4A1C4A9C2D46}',
 u'QUOTERECORD': '{9BE86BB5-99D4-4F03-AC5A-C115128573C0}',
}
CLSIDToClassMap = {
 '{A58A9CE8-7681-46FB-8064-35D6DF9CEA0C}' : MbtQuotes,
 '{7DEAC3B3-CA70-4E48-8552-E5CCA5493B71}' : IMbtQuotes,
 '{C13BD25A-B87F-4EFF-924C-F7109261A394}' : _IMbtQuotesEvents,
}
CLSIDToPackageMap = {}
win32com.client.CLSIDToClass.RegisterCLSIDsFromDict( CLSIDToClassMap )
VTablesToPackageMap = {}
VTablesToClassMap = {
 '{C8323838-B094-4E3C-B9BE-D08B5C06E6BE}' : 'IMbtQuotesNotify',
 '{7DEAC3B3-CA70-4E48-8552-E5CCA5493B71}' : 'IMbtQuotes',
}

NamesToIIDMap = {
 'IMbtQuotesNotify' : '{C8323838-B094-4E3C-B9BE-D08B5C06E6BE}',
 'IMbtQuotes' : '{7DEAC3B3-CA70-4E48-8552-E5CCA5493B71}',
 '_IMbtQuotesEvents' : '{C13BD25A-B87F-4EFF-924C-F7109261A394}',
}
win32com.client.constants.__dicts__.append(constants.__dict__)

----------------------------------------------------------------------------------------------------------------------------------------------------

On Mon, Apr 13, 2009 at 3:09 AM, Mark Hammond <skippy.hammond at gmail.com>wrote:

>  On 11/04/2009 4:26 AM, Kapil fadnis wrote:
>
>> I think this has been talked about on a previous thread but its an old
>> thread here but I am hoping to get some feedback that will help me
>> understand the issue,
>>
>> http://mail.python.org/pipermail/python-win32/2006-September/005044.html
>>
>>
>> I have an interface
>> MBTQUOTELib.IMbtQuotesNotify
>> which has four methods to be implemented, each of which is an event on
>> the server side i.e. my broker.
>> One of the methods is "OnQuoteData(QUOTERECORD). The way I have seen it
>> being implemented (in the thread above) is
>> run makepy - i on the interface definition, i use PythonWin for this
>> which gives me a .py file.
>> 13F76618-D199-4485-8C95-DC524958686Cx0x2x0.py
>>
>> The implementation is
>>
>> -------------------------------------------------------------------------------------------
>> from win32com import universal
>>
>> universal.RegisterInterfaces('{13F76618-D199-4485-8C95-DC524958686C}',0,2,0,["IMbtQuotesNotify"])
>> class Q:
>>     _com_interfaces_ = ['IMbtQuotesNotify']
>>     _typelib_guid_ = '{13F76618-D199-4485-8C95-DC524958686C}'
>>     _typelib_version_ = 2, 0
>>
>>     def OnQuoteData(self,pQuote):
>>         print "got onquote!"
>>         print type(pQuote)
>>         print "%s" % pQuote
>>
>> import win32com.server.util
>> qnotify = win32com.server.util.wrap(Q(),useDispatcher=1)
>>
>> import win32com.client as com
>> mbtCom = com.Dispatch("MBTrading.MbtComMgr")
>> mbtCom.DoLogin(9,"myusername","mypassword","")
>> quote = mbtCom.Quotes
>> quote.AdviseSymbol(qnotify,'AAPL',1)
>>
>
> See if adding a call to pythoncom.PumpMessages() at the very end makes a
> difference...
>
> Cheers,
>
> Mark
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20090413/341ff396/attachment-0001.htm>


More information about the python-win32 mailing list