我目前有第三方ActiveX控件,旨在用于VB6。我想将控件迁移到python。我得到了win32com,然后我跑了'makepy.py -i'在类型库文件上。我了解如何使用win32com.client.Dispatch()方法调度可执行容器,然后调用子类。容器被称为Sterling,并且在调用对象时将运行,但我不会在VB代码中专门提到容器,所以我认为这里也不需要它。我从那些COMM对象获得的只是类型库,但是,它似乎都适用于VB6。这是我添加对Sterling.tlb类型库的引用后工作和访问COM对象的VB6代码:
' Declare ActiveX type library class
Public WithEvents m_STIQuote As STIQuote
Private Sub Form_Load()
......................................
' Create an instance of type library class
Set m_STIQuote = New STIQuote
......................................
End Sub
Private Sub Form_Unload(Cancel As Integer)
......................................
' destroy type library class
Set m_STIQuote = Nothing
......................................
End Sub
Private Sub btnMDX_Click()
' structSTIMdxRegEx is a structure that comes with ActiveX type library
Dim inMDXInfo As structSTIMdxRegEx
inMDXInfo.bstrExchanges = "N"
inMDXInfo.bstrMsgTypes = ""
inMDXInfo.bReg = True
inMDXInfo.bAllSyms = True
inMDXInfo.bNewOnly = False
'Call method in ActiveX class with the structure provided as an input
m_STIQuote.RegisterMdxEx inMDXInfo
End Sub
' Event handler provided by the library that receives class with data as an argument.
Private Sub m_STIQuote_OnSTIQuoteUpdate(structQuoteUpdate As SterlingLib.structSTIQuoteUpdate)
LogMsg "MDX|" & structQuoteUpdate.bstrSymbol & "|" & structQuoteUpdate.nMdxMsgType
End Sub
这是我到目前为止在python中所拥有的,它无法从Record map中找到_ISTIQuoteEvents类和结构:
import win32com.client
from win32com.client import gencache
gencache.EnsureModule('{C418ADDA-61CC-4854-B69E-87F8BF6D7A0E}', 0, 1, 0)
class myQuoteEvents(_ISTIQuoteEvents):
def OnSTIQuoteUpdate(self, structQuoteUpdate=defaultNamedNotOptArg):
print_str = "MDX|" + structQuoteUpdate.bstrSymbol + "|" + structQuoteUpdate.nMdxMsgType
print print_str
myQuote = win32com.client.DispatchWithEvents("STIQuote.STIQuote_ctrl.1", myQuoteEvents)
inMDXInfo = structSTIMdxRegEx()
inMDXInfo.bstrExchanges = "N"
inMDXInfo.bstrMsgTypes = ""
inMDXInfo.bReg = True
inMDXInfo.bAllSyms = True
inMDXInfo.bNewOnly = False
myQuote.RegisterMdxEx(inMDXInfo)
_ISTIQuoteEvents 是makepy生成包装器中的一个类,但由于某些原因,它在脚本中不可见。我打电话给gencache,所以我觉得我应该可以看到它,但这个名字不被识别。 ISTIQuote 是一个派生自DispatchBaseClass的类,是一个接口类。 STIQuote 是包装器中的CoClassBaseClass派生类,它包含ISTIQuote作为接口& _ISTIQuoteEvents作为事件。容器应用程序是在计算机上运行的第三方程序,因此此代码仅在容器运行时执行。在COMM术语中它的英镑。应该是英镑。所以STIQuote是Sterling的子类。 structQuoteUpdate 和 structSTIMdxRegEx 都只有带有CLSID的RecordMap条目,但我知道它们包含在VB6环境中的字段。我想知道如何从包装器中提取它们以访问它们的数据结构。
这是python包装器,只有makepy.py中的相关信息:
# -*- coding: mbcs -*-
# Created by makepy.py version 0.5.01
# By python version 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]
# From type library 'Sterling.tlb'
# On Tue Jun 02 17:08:01 2015
'Sterling 1.0 Type Library'
makepy_version = '0.5.01'
python_version = 0x2070af0
import win32com.client.CLSIDToClass, pythoncom, pywintypes
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('{C418ADDA-61CC-4854-B69E-87F8BF6D7A0E}')
MajorVersion = 1
MinorVersion = 0
LibraryFlags = 8
LCID = 0x0
.......................................................
from win32com.client import DispatchBaseClass
.......................................................
class ISTIQuote(DispatchBaseClass):
'ISTIQuote Interface'
CLSID = IID('{F8D282CC-FEE7-45EC-A080-4276E43E8214}')
coclass_clsid = IID('{8EB74552-C174-4D51-819B-9E65A8916273}')
.......................................................
def RegisterMdxEx(self, pMdx=defaultNamedNotOptArg):
'method RegisterMdxEx'
return self._oleobj_.InvokeTypes(27, LCID, 1, (24, 0), ((36, 1),),pMdx
)
def RegisterQuote(self, bstrSymbol=defaultNamedNotOptArg, bstrExch=defaultNamedNotOptArg):
'method RegisterQuote'
return self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((8, 1), (8, 1)),bstrSymbol
, bstrExch)
..........................................................................
_prop_map_get_ = {
"AskPrice": (11, 2, (5, 0), (), "AskPrice", None),
"AskSize": (13, 2, (3, 0), (), "AskSize", None),
"BidPrice": (10, 2, (5, 0), (), "BidPrice", None),
"BidSize": (12, 2, (3, 0), (), "BidSize", None),
"DefaultAcct": (17, 2, (8, 0), (), "DefaultAcct", None),
"DefaultDest": (19, 2, (8, 0), (), "DefaultDest", None),
"DefaultQty": (16, 2, (3, 0), (), "DefaultQty", None),
"Exch": (9, 2, (8, 0), (), "Exch", None),
"LastPrice": (14, 2, (5, 0), (), "LastPrice", None),
"LastSize": (20, 2, (3, 0), (), "LastSize", None),
"Level": (21, 2, (3, 0), (), "Level", None),
"Symbol": (8, 2, (8, 0), (), "Symbol", None),
"UpdateID": (26, 2, (3, 0), (), "UpdateID", None),
}
_prop_map_put_ = {
"AskPrice": ((11, LCID, 4, 0),()),
"AskSize": ((13, LCID, 4, 0),()),
"BidPrice": ((10, LCID, 4, 0),()),
"BidSize": ((12, LCID, 4, 0),()),
"DefaultAcct": ((17, LCID, 4, 0),()),
"DefaultDest": ((19, LCID, 4, 0),()),
"DefaultQty": ((16, LCID, 4, 0),()),
"Exch": ((9, LCID, 4, 0),()),
"LastPrice": ((14, LCID, 4, 0),()),
"LastSize": ((20, LCID, 4, 0),()),
"Level": ((21, LCID, 4, 0),()),
"Symbol": ((8, LCID, 4, 0),()),
"UpdateID": ((26, LCID, 4, 0),()),
}
def __iter__(self):
"Return a Python iterator for this object"
try:
ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
except pythoncom.error:
raise TypeError("This object does not support enumeration")
return win32com.client.util.Iterator(ob, None)
..........................................................................
class _ISTIQuoteEvents:
'_ISTIQuoteEvents Interface'
CLSID = CLSID_Sink = IID('{7482011F-7414-460B-92A8-693F1FDB8C27}')
coclass_clsid = IID('{8EB74552-C174-4D51-819B-9E65A8916273}')
_public_methods_ = [] # For COM Server support
_dispid_to_func_ = {
14 : "OnSTIL2Reply",
1 : "OnSTIQuoteUpdate",
9 : "OnSTIQuoteRqstXML",
15 : "OnSTIL2ReplyXML",
7 : "OnSTIQuoteUpdateXML",
13 : "OnSTIL2UpdateXML",
4 : "OnSTIQuoteRqst",
12 : "OnSTIL2Update",
3 : "OnSTIShutdown",
8 : "OnSTIQuoteSnapXML",
11 : "OnSTINewsUpdateXML",
10 : "OnSTIGreeksUpdateXML",
5 : "OnSTIGreeksUpdate",
2 : "OnSTIQuoteSnap",
6 : "OnSTINewsUpdate",
}
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 OnSTIL2Reply(self, arrayL2Update=defaultNamedNotOptArg):
# 'method OnSTIL2ReplyStruct'
# def OnSTIQuoteUpdate(self, structQuoteUpdate=defaultNamedNotOptArg):
# 'method OnSTIQuoteUpdateStruct'
# def OnSTIQuoteRqstXML(self, bstrQuote=defaultNamedNotOptArg):
# 'method OnSTIQuoteRqstXML'
# def OnSTIL2ReplyXML(self, bstrQuote=defaultNamedNotOptArg):
# 'method OnSTIL2ReplyXML'
# def OnSTIQuoteUpdateXML(self, bstrQuote=defaultNamedNotOptArg):
# 'method OnSTIQuoteUpdateXML'
# def OnSTIL2UpdateXML(self, bstrQuote=defaultNamedNotOptArg):
# 'method OnSTIL2UpdateXML'
# def OnSTIQuoteRqst(self, structQuoteRqst=defaultNamedNotOptArg):
# 'method OnSTIQuoteRqstStruct'
# def OnSTIL2Update(self, structL2Update=defaultNamedNotOptArg):
# 'method OnSTIL2UpdateStruct'
# def OnSTIShutdown(self):
# 'method OnSTIShutdownStruct'
# def OnSTIQuoteSnapXML(self, bstrQuote=defaultNamedNotOptArg):
# 'method OnSTIQuoteSnapXML'
# def OnSTINewsUpdateXML(self, bstrQuote=defaultNamedNotOptArg):
# 'method OnSTINewsUpdateXML'
# def OnSTIGreeksUpdateXML(self, bstrQuote=defaultNamedNotOptArg):
# 'method OnSTIGreeksUpdateXML'
# def OnSTIGreeksUpdate(self, structGreeksUpdate=defaultNamedNotOptArg):
# 'method OnSTIGreeksUpdateStruct'
# def OnSTIQuoteSnap(self, structQuoteSnap=defaultNamedNotOptArg):
# 'method OnSTIQuoteSnapStruct'
# def OnSTINewsUpdate(self, structNewsUpdate=defaultNamedNotOptArg):
# 'method OnSTINewsUpdateStruct'
..........................................................................
from win32com.client import CoClassBaseClass
..........................................................................
class STIQuote(CoClassBaseClass): # A CoClass
# STIQuote Class
CLSID = IID('{8EB74552-C174-4D51-819B-9E65A8916273}')
coclass_sources = [
_ISTIQuoteEvents,
]
default_source = _ISTIQuoteEvents
coclass_interfaces = [
ISTIQuote,
]
default_interface = ISTIQuote
..........................................................................
ISTIQuote_vtables_dispatch_ = 1
ISTIQuote_vtables_ = [
(( u'RegisterQuote' , u'bstrSymbol' , u'bstrExch' , ), 1, (1, (), [ (8, 1, None, None) ,
(8, 1, None, None) , ], 1 , 1 , 4 , 0 , 28 , (3, 0, None, None) , 0 , )),
(( u'DeRegisterQuote' , u'bstrSymbol' , u'bstrExch' , ), 2, (2, (), [ (8, 1, None, None) ,
(8, 1, None, None) , ], 1 , 1 , 4 , 0 , 32 , (3, 0, None, None) , 0 , )),
(( u'DeRegisterAllQuotes' , ), 3, (3, (), [ ], 1 , 1 , 4 , 0 , 36 , (3, 0, None, None) , 0 , )),
(( u'RegisterForMdx' , u'bMdx' , ), 4, (4, (), [ (11, 1, None, None) , ], 1 , 1 , 4 , 0 , 40 , (3, 0, None, None) , 0 , )),
(( u'RegisterForAllMdx' , u'bMdx' , ), 5, (5, (), [ (11, 1, None, None) , ], 1 , 1 , 4 , 0 , 44 , (3, 0, None, None) , 0 , )),
(( u'GetQueueCount' , u'lCount' , ), 6, (6, (), [ (16387, 10, None, None) , ], 1 , 1 , 4 , 0 , 48 , (3, 0, None, None) , 0 , )),
(( u'SubmitQuote' , u'bstrKey' , u'RetVal' , ), 7, (7, (), [ (8, 1, None, None) ,
(16387, 10, None, None) , ], 1 , 1 , 4 , 0 , 52 , (3, 0, None, None) , 0 , )),
(( u'Symbol' , u'pVal' , ), 8, (8, (), [ (16392, 10, None, None) , ], 1 , 2 , 4 , 0 , 56 , (3, 0, None, None) , 0 , )),
(( u'Symbol' , u'pVal' , ), 8, (8, (), [ (8, 1, None, None) , ], 1 , 4 , 4 , 0 , 60 , (3, 0, None, None) , 0 , )),
(( u'Exch' , u'pVal' , ), 9, (9, (), [ (16392, 10, None, None) , ], 1 , 2 , 4 , 0 , 64 , (3, 0, None, None) , 0 , )),
(( u'Exch' , u'pVal' , ), 9, (9, (), [ (8, 1, None, None) , ], 1 , 4 , 4 , 0 , 68 , (3, 0, None, None) , 0 , )),
(( u'BidPrice' , u'pVal' , ), 10, (10, (), [ (16389, 10, None, None) , ], 1 , 2 , 4 , 0 , 72 , (3, 0, None, None) , 0 , )),
(( u'BidPrice' , u'pVal' , ), 10, (10, (), [ (5, 1, None, None) , ], 1 , 4 , 4 , 0 , 76 , (3, 0, None, None) , 0 , )),
(( u'AskPrice' , u'pVal' , ), 11, (11, (), [ (16389, 10, None, None) , ], 1 , 2 , 4 , 0 , 80 , (3, 0, None, None) , 0 , )),
(( u'AskPrice' , u'pVal' , ), 11, (11, (), [ (5, 1, None, None) , ], 1 , 4 , 4 , 0 , 84 , (3, 0, None, None) , 0 , )),
(( u'BidSize' , u'pVal' , ), 12, (12, (), [ (16387, 10, None, None) , ], 1 , 2 , 4 , 0 , 88 , (3, 0, None, None) , 0 , )),
(( u'BidSize' , u'pVal' , ), 12, (12, (), [ (3, 1, None, None) , ], 1 , 4 , 4 , 0 , 92 , (3, 0, None, None) , 0 , )),
(( u'AskSize' , u'pVal' , ), 13, (13, (), [ (16387, 10, None, None) , ], 1 , 2 , 4 , 0 , 96 , (3, 0, None, None) , 0 , )),
(( u'AskSize' , u'pVal' , ), 13, (13, (), [ (3, 1, None, None) , ], 1 , 4 , 4 , 0 , 100 , (3, 0, None, None) , 0 , )),
(( u'LastPrice' , u'pVal' , ), 14, (14, (), [ (16389, 10, None, None) , ], 1 , 2 , 4 , 0 , 104 , (3, 0, None, None) , 0 , )),
(( u'LastPrice' , u'pVal' , ), 14, (14, (), [ (5, 1, None, None) , ], 1 , 4 , 4 , 0 , 108 , (3, 0, None, None) , 0 , )),
(( u'Destroy' , ), 15, (15, (), [ ], 1 , 1 , 4 , 0 , 112 , (3, 0, None, None) , 0 , )),
(( u'DefaultQty' , u'pVal' , ), 16, (16, (), [ (16387, 10, None, None) , ], 1 , 2 , 4 , 0 , 116 , (3, 0, None, None) , 0 , )),
(( u'DefaultQty' , u'pVal' , ), 16, (16, (), [ (3, 1, None, None) , ], 1 , 4 , 4 , 0 , 120 , (3, 0, None, None) , 0 , )),
(( u'DefaultAcct' , u'pVal' , ), 17, (17, (), [ (16392, 10, None, None) , ], 1 , 2 , 4 , 0 , 124 , (3, 0, None, None) , 0 , )),
(( u'DefaultAcct' , u'pVal' , ), 17, (17, (), [ (8, 1, None, None) , ], 1 , 4 , 4 , 0 , 128 , (3, 0, None, None) , 0 , )),
(( u'SetTradesOnly' , u'bTrades' , ), 18, (18, (), [ (11, 1, None, None) , ], 1 , 1 , 4 , 0 , 132 , (3, 0, None, None) , 0 , )),
(( u'DefaultDest' , u'pVal' , ), 19, (19, (), [ (16392, 10, None, None) , ], 1 , 2 , 4 , 0 , 136 , (3, 0, None, None) , 0 , )),
(( u'DefaultDest' , u'pVal' , ), 19, (19, (), [ (8, 1, None, None) , ], 1 , 4 , 4 , 0 , 140 , (3, 0, None, None) , 0 , )),
(( u'LastSize' , u'pVal' , ), 20, (20, (), [ (16387, 10, None, None) , ], 1 , 2 , 4 , 0 , 144 , (3, 0, None, None) , 0 , )),
(( u'LastSize' , u'pVal' , ), 20, (20, (), [ (3, 1, None, None) , ], 1 , 4 , 4 , 0 , 148 , (3, 0, None, None) , 0 , )),
(( u'Level' , u'pVal' , ), 21, (21, (), [ (16387, 10, None, None) , ], 1 , 2 , 4 , 0 , 152 , (3, 0, None, None) , 0 , )),
(( u'Level' , u'pVal' , ), 21, (21, (), [ (3, 1, None, None) , ], 1 , 4 , 4 , 0 , 156 , (3, 0, None, None) , 0 , )),
(( u'SubmitQuoteStruct' , u'bstrKey' , u'pQuote' , u'RetVal' , ), 22, (22, (), [
(8, 1, None, None) , (36, 1, None, None) , (16387, 10, None, None) , ], 1 , 1 , 4 , 0 , 160 , (3, 0, None, None) , 0 , )),
(( u'RegisterForAllNews' , u'bNews' , ), 23, (23, (), [ (11, 1, None, None) , ], 1 , 1 , 4 , 0 , 164 , (3, 0, None, None) , 0 , )),
(( u'RegisterForNewMdx' , u'bMdx' , u'bAllSyms' , ), 24, (24, (), [ (11, 1, None, None) ,
(11, 1, None, None) , ], 1 , 1 , 4 , 0 , 168 , (3, 0, None, None) , 0 , )),
(( u'RegisterQuoteEx' , u'pQuote' , ), 25, (25, (), [ (36, 1, None, None) , ], 1 , 1 , 4 , 0 , 172 , (3, 0, None, None) , 0 , )),
(( u'UpdateID' , u'pVal' , ), 26, (26, (), [ (16387, 10, None, None) , ], 1 , 2 , 4 , 0 , 176 , (3, 0, None, None) , 0 , )),
(( u'UpdateID' , u'pVal' , ), 26, (26, (), [ (3, 1, None, None) , ], 1 , 4 , 4 , 0 , 180 , (3, 0, None, None) , 0 , )),
(( u'RegisterMdxEx' , u'pMdx' , ), 27, (27, (), [ (36, 1, None, None) , ], 1 , 1 , 4 , 0 , 184 , (3, 0, None, None) , 0 , )),
(( u'RegisterL2' , u'pL2' , ), 28, (28, (), [ (36, 1, None, None) , ], 1 , 1 , 4 , 0 , 188 , (3, 0, None, None) , 0 , )),
]
..........................................................................
RecordMap = {
u'structSTIQuoteUpdate': '{5D4A5541-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIQuoteSnap': '{5D4A5543-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTITradeUpdate': '{5D4A5544-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIOrderUpdate': '{5D4A5545-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIOrderReject': '{5D4A5546-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIOrderConfirm': '{5D4A5547-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIPositionUpdate': '{5D4A5548-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIBookUpdate': '{5D4A5549-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTILink': '{5D4A5550-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIQuoteRqst': '{5D4A5551-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIGreeksUpdate': '{5D4A5552-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTINewsUpdate': '{5D4A5553-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIAcctUpdate': '{5D4A5554-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIOrderFilter': '{5D4A5555-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTITradeFilter': '{5D4A5556-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIDrop': '{5D4A5557-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIOrder': '{5D4A5558-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIQuoteRegEx': '{5D4A5559-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIMdxRegEx': '{5D4A5560-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTICancelAll': '{5D4A5561-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIL2Reg': '{5D4A5562-619F-48AB-8E64-0CAB14A92F6F}',
u'structSTIL2Update': '{5D4A5563-619F-48AB-8E64-0CAB14A92F6F}',
}
CLSIDToClassMap = {
..........................................................................
'{7482011F-7414-460B-92A8-693F1FDB8C27}' : _ISTIQuoteEvents,
'{8EB74552-C174-4D51-819B-9E65A8916273}' : STIQuote,
..........................................................................
}
CLSIDToPackageMap = {}
win32com.client.CLSIDToClass.RegisterCLSIDsFromDict( CLSIDToClassMap )
VTablesToPackageMap = {}
VTablesToClassMap = {
'{F8D282CC-FEE7-45EC-A080-4276E43E8214}' : 'ISTIQuote',
}
NamesToIIDMap = {
'ISTIQuote' : '{F8D282CC-FEE7-45EC-A080-4276E43E8214}',
'_ISTIQuoteEvents' : '{7482011F-7414-460B-92A8-693F1FDB8C27}',
}
win32com.client.constants.__dicts__.append(constants.__dict__)