questions about a C++ COM object accessed in Python via win32com

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

    questions about a C++ COM object accessed in Python via win32com

    Hi

    I am coding a C++ COM object which I want to access in Python. For that
    I do have some detail questions and need help since I am not that familiar
    with the documentation itself of Python and win32com yet.


    1.
    How do I know that...

    import win32com.client

    ....was successful or not?


    2.
    In my COM object I do have the following C++ function:

    STDMETHODIMP CMyTestObjekt:: Connect()
    {
    if (m_boolReadyToC onnect == false) {
    return E_FAIL;
    }
    return S_OK;
    }

    How do I know in Python whether I got back a S_OK or an E_FAIL? Or is this
    whole concept wrong and I should better make an additional return value like:

    MyFunktion(.... ..., [out, retval] BOOL *pVal); (declaration in the IDL)


    3.
    If I execute the following command...

    MyObject = win32com.client .Dispatch("MyCO M.MySuperCOMObj ect")

    ....what is the return value from which I know whether it was successful?
    Or what can be the ""content"" of "MyObject" afterwards?


    4.
    If I have a function in Python in which I call the (global) COM Object,
    what "if" do I have to use to know whether the COM object already had
    been allocated? I tried the following but that didn't work:

    def foo():
    if MyObject == None:
    print "COM not there yet"
    else:
    print MyObject.Messag e


    I'd really appreciate any help. Or even a hint where to look in the "original"
    documentation. FYI: I do use Win2000 and ActiveState ActivePython 2.2 which
    already includes win32com.

    Bye,
    Matthias "T.T.H." Grobe
  • Alex Martelli

    #2
    Re: questions about a C++ COM object accessed in Python via win32com

    T.T.H. wrote:
    [color=blue]
    > Hi
    >
    > I am coding a C++ COM object which I want to access in Python. For that
    > I do have some detail questions and need help since I am not that familiar
    > with the documentation itself of Python and win32com yet.
    >
    >
    > 1.
    > How do I know that...
    >
    > import win32com.client
    >
    > ...was successful or not?[/color]

    If it fails, it raises an exception -- so, if you want to handle the
    failure, you try this statement in the try clause of a try/except
    statement. That is reasonably similar to C++'s exception (syntax
    apart), so the concept shouldn't be too alien.

    [color=blue]
    > 2.
    > In my COM object I do have the following C++ function:
    >
    > STDMETHODIMP CMyTestObjekt:: Connect()
    > {
    > if (m_boolReadyToC onnect == false) {
    > return E_FAIL;
    > }
    > return S_OK;
    > }
    >
    > How do I know in Python whether I got back a S_OK or an E_FAIL? Or is this[/color]

    E_FAIL translates into an exception.
    [color=blue]
    > whole concept wrong and I should better make an additional return value
    > like:
    >
    > MyFunktion(.... ..., [out, retval] BOOL *pVal); (declaration in the IDL)[/color]

    This would be much more COM-idiomatic, unless the failure IS an exceptional
    event, something that SHOULD not happen in a well-functioning system and
    probably the result of mis-configuration or hardware malfunctioning.
    [color=blue]
    >
    > 3.
    > If I execute the following command...
    >
    > MyObject = win32com.client .Dispatch("MyCO M.MySuperCOMObj ect")
    >
    > ...what is the return value from which I know whether it was successful?[/color]

    Guess...?-) Yep: if it's not successful it raises an exception!
    [color=blue]
    > Or what can be the ""content"" of "MyObject" afterwards?[/color]

    If an exception wasn't raised, MyObject is an instance of some class
    (you need not care about which one...) on which you can call methods
    (which will be "proxied" to the actual C++-implemented COM object).
    [color=blue]
    >
    > 4.
    > If I have a function in Python in which I call the (global) COM Object,
    > what "if" do I have to use to know whether the COM object already had
    > been allocated? I tried the following but that didn't work:
    >
    > def foo():
    > if MyObject == None:
    > print "COM not there yet"
    > else:
    > print MyObject.Messag e[/color]

    If name "MyObject" has never been bound, trying to access it will...
    I give you three guesses... *raise an exception*!

    However, I _would_ suggest that in this case you set MyObject=None
    at a global level and test as above (except, use "Myobject is None"
    rather than ==) -- that is more idiomatic in Python.


    Alex

    Comment

    • T.T.H.

      #3
      Re: questions about a C++ COM object accessed in Python via win32com

      > Alex Martelli <aleax@aleax.it >

      Praise you, Alex, that was the most precise answer I could have imagined!

      Bye,
      T.T.H.

      Comment

      • T.T.H.

        #4
        Re: questions about a C++ COM object accessed in Python via win32com

        > Alex Martelli <aleax@aleax.it >

        Praise you, Alex, that was the most precise answer I could have imagined!

        Bye,
        T.T.H.

        Comment

        Working...