IronPython and COM Interface

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Divya

    #1

    IronPython and COM Interface

    Hello,

    I'm new to IronPython and COM so please bear with me.

    I have a COM interface provided to me. I need to implement this
    interface and pass it to a method as a way of implementing
    'callbacks'.

    However it appears that the methods of my interface object are never
    called.

    As a alternative, I implemented the interface in C# and then extended
    the C# class in IronPython. However, on running the code, methods of
    my C# class rather than the IronPython class are being called.

    Can anyone advise? Is this possible in IronPython?

    The code is listed below.

    Regards,
    Divya

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

    import time

    import clr
    clr.AddReferenc e('Interop.MBTC OMLib.dll')
    clr.AddReferenc e('Interop.MBTO RDERSLib.dll')
    clr.AddReferenc e('Interop.MBTQ UOTELib.dll')

    import MBTCOMLib
    import MBTORDERSLib
    import MBTQUOTELib

    demo_user='user '
    demo_pass='pass '
    demo_host=9

    class MBEvents(MBTQUO TELib.IMbtQuote sNotify):
    def __init__(self):
    MBTQUOTELib.IMb tQuotesNotify._ _init__(self)

    def OnLevel2Data(da ta):
    print 'OnLevel2Data'

    def OnOptionsData(s elf, data):
    print 'OnOptionsData'

    def OnQuoteData(sel f, data):
    print 'OnQuoteData'

    def OnTSData(self, data):
    print 'OnTSData'

    class MB:
    def __init__(self):
    self.conn = MBTCOMLib.MbtCo mMgrClass()
    #self.conn.Enab leSplash(False)
    self.orders = self.conn.Order Client
    self.quotes = self.conn.Quote s
    self.events = MBEvents()

    def login(self, host=demo_host, user=demo_user, passwd=demo_pas s):
    self.conn.DoLog in(host, user, passwd, '')
    print self.quotes.Rem oteAddress, self.quotes.Rem otePort
    self.quotes.Adv iseSymbol(self. events, 'EUR/USD', 1|2|3|4)

  • Larry Bates

    #2
    Re: IronPython and COM Interface

    Divya wrote:
    Hello,
    >
    I'm new to IronPython and COM so please bear with me.
    >
    I have a COM interface provided to me. I need to implement this
    interface and pass it to a method as a way of implementing
    'callbacks'.
    >
    However it appears that the methods of my interface object are never
    called.
    >
    As a alternative, I implemented the interface in C# and then extended
    the C# class in IronPython. However, on running the code, methods of
    my C# class rather than the IronPython class are being called.
    >
    Can anyone advise? Is this possible in IronPython?
    >
    The code is listed below.
    >
    Regards,
    Divya
    >
    ---------------------------
    >
    import time
    >
    import clr
    clr.AddReferenc e('Interop.MBTC OMLib.dll')
    clr.AddReferenc e('Interop.MBTO RDERSLib.dll')
    clr.AddReferenc e('Interop.MBTQ UOTELib.dll')
    >
    import MBTCOMLib
    import MBTORDERSLib
    import MBTQUOTELib
    >
    demo_user='user '
    demo_pass='pass '
    demo_host=9
    >
    class MBEvents(MBTQUO TELib.IMbtQuote sNotify):
    def __init__(self):
    MBTQUOTELib.IMb tQuotesNotify._ _init__(self)
    >
    def OnLevel2Data(da ta):
    print 'OnLevel2Data'
    >
    def OnOptionsData(s elf, data):
    print 'OnOptionsData'
    >
    def OnQuoteData(sel f, data):
    print 'OnQuoteData'
    >
    def OnTSData(self, data):
    print 'OnTSData'
    >
    class MB:
    def __init__(self):
    self.conn = MBTCOMLib.MbtCo mMgrClass()
    #self.conn.Enab leSplash(False)
    self.orders = self.conn.Order Client
    self.quotes = self.conn.Quote s
    self.events = MBEvents()
    >
    def login(self, host=demo_host, user=demo_user, passwd=demo_pas s):
    self.conn.DoLog in(host, user, passwd, '')
    print self.quotes.Rem oteAddress, self.quotes.Rem otePort
    self.quotes.Adv iseSymbol(self. events, 'EUR/USD', 1|2|3|4)
    >
    It may not help but I was struggling with the COM-callback issue
    yesterday and found that this works in regular python:

    import win32com.server .util

    def callback:
    _public_methods _=['progress']
    def progress(self, total, number):
    print "completed %i of %i" % (number, total)

    #
    # Get instance of callback class
    #
    cb=callback()
    #
    # Wrap the callback class to turn it into an IDispatch (COM) object
    # so I can pass it to my COM object.
    #
    idCallback=win3 2com.server.uti l.wrap(cb)
    #
    # Call the COM interface and pass it the callback function
    #
    r=oC.WSset_call back(idCallback )

    In my case the COM object was also written in Python so to get
    to the python object I do following:

    def WSset_callback( idCallback)
    self.callback=w in32com.client. Dispatch(idCall back)

    Now I can call .progress method of callback class from the COM
    object.

    self.callback.p rogress(total, number)

    Hope this helps in some way.

    -Larry

    Comment

    Working...