使用win32com的VB6和python中ActiveX控件的实现差异。

2024-09-27 20:20:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有一个第三方ActiveX控件,设计用于VB6。我想将控件迁移到python。我得到了win32com然后我跑了'makepy.py-我在类型库文件上。我了解如何使用win32分派可执行容器com.client.Dispatch公司()方法,然后调用子类。容器名为Sterling,在调用对象时将运行该容器,但我在VB代码中并没有专门提到容器,因此我认为这里也不需要它。我从这些COMM对象中得到的只是类型库,但是,它似乎都在VB6中工作。这是VB6代码,在添加对的引用后,它可以工作并访问COM对象英镑.tlb类型库:

' 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类和结构:

^{pr2}$

是makepy生成的包装器中的一个类,但由于某些原因,它在脚本中不可见。我打电话给gencache,所以我想我应该可以看到它,但是名字没有被认出。ISTIQuote是从DispatchBaseClass派生的类,是一个接口类。STIQuote是包装器中从CoClassBaseClass派生的类,它包含ISTIQuote作为接口和作为事件的ISTIQuoteEvents。容器应用程序是在机器上运行的第三方程序,因此此代码仅在容器运行时执行。在通信术语中,它是英镑。应该是英镑。所以STIQuote是英镑的一个子类。structQuoteUpdatestructSTIMdxRegEx都只有带有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__)

Tags: importselfclientnonedefmethodclasswin32com

热门问题