IronPython and COM Interface

Discussion in 'Python' started by Divya, Mar 14, 2007.

  1. Divya

    Divya Guest

    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.AddReference('Interop.MBTCOMLib.dll')
    clr.AddReference('Interop.MBTORDERSLib.dll')
    clr.AddReference('Interop.MBTQUOTELib.dll')

    import MBTCOMLib
    import MBTORDERSLib
    import MBTQUOTELib

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

    class MBEvents(MBTQUOTELib.IMbtQuotesNotify):
    def __init__(self):
    MBTQUOTELib.IMbtQuotesNotify.__init__(self)

    def OnLevel2Data(data):
    print 'OnLevel2Data'

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

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

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

    class MB:
    def __init__(self):
    self.conn = MBTCOMLib.MbtComMgrClass()
    #self.conn.EnableSplash(False)
    self.orders = self.conn.OrderClient
    self.quotes = self.conn.Quotes
    self.events = MBEvents()

    def login(self, host=demo_host, user=demo_user, passwd=demo_pass):
    self.conn.DoLogin(host, user, passwd, '')
    print self.quotes.RemoteAddress, self.quotes.RemotePort
    self.quotes.AdviseSymbol(self.events, 'EUR/USD', 1|2|3|4)
     
    Divya, Mar 14, 2007
    #1
    1. Advertising

  2. Divya

    Larry Bates Guest

    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.AddReference('Interop.MBTCOMLib.dll')
    > clr.AddReference('Interop.MBTORDERSLib.dll')
    > clr.AddReference('Interop.MBTQUOTELib.dll')
    >
    > import MBTCOMLib
    > import MBTORDERSLib
    > import MBTQUOTELib
    >
    > demo_user='user'
    > demo_pass='pass'
    > demo_host=9
    >
    > class MBEvents(MBTQUOTELib.IMbtQuotesNotify):
    > def __init__(self):
    > MBTQUOTELib.IMbtQuotesNotify.__init__(self)
    >
    > def OnLevel2Data(data):
    > print 'OnLevel2Data'
    >
    > def OnOptionsData(self, data):
    > print 'OnOptionsData'
    >
    > def OnQuoteData(self, data):
    > print 'OnQuoteData'
    >
    > def OnTSData(self, data):
    > print 'OnTSData'
    >
    > class MB:
    > def __init__(self):
    > self.conn = MBTCOMLib.MbtComMgrClass()
    > #self.conn.EnableSplash(False)
    > self.orders = self.conn.OrderClient
    > self.quotes = self.conn.Quotes
    > self.events = MBEvents()
    >
    > def login(self, host=demo_host, user=demo_user, passwd=demo_pass):
    > self.conn.DoLogin(host, user, passwd, '')
    > print self.quotes.RemoteAddress, self.quotes.RemotePort
    > self.quotes.AdviseSymbol(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=win32com.server.util.wrap(cb)
    #
    # Call the COM interface and pass it the callback function
    #
    r=oC.WSset_callback(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=win32com.client.Dispatch(idCallback)

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

    self.callback.progress(total, number)

    Hope this helps in some way.

    -Larry
     
    Larry Bates, Mar 14, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hoang Do

    IronPython and collections access

    Hoang Do, Aug 18, 2004, in forum: Python
    Replies:
    1
    Views:
    342
    Lawrence Oluyede
    Aug 18, 2004
  2. JZ
    Replies:
    0
    Views:
    459
  3. John Salerno

    Python for .NET and IronPython

    John Salerno, Nov 2, 2005, in forum: Python
    Replies:
    8
    Views:
    533
    Luis M. Gonzalez
    Nov 3, 2005
  4. edfialk

    Interface in IronPython

    edfialk, Mar 12, 2007, in forum: Python
    Replies:
    2
    Views:
    627
    Steve Holden
    Mar 13, 2007
  5. Mudcat
    Replies:
    5
    Views:
    391
    Kay Schluehr
    Mar 17, 2009
Loading...

Share This Page