#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0

    question on C++ COM object accessed by 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...
    Code:
    import win32com.client
    was successful or not?

    2.
    In my COM object I do have the following C++ function:
    Code:
    STDMETHODIMP CMyTestObjekt::Connect()
    {
    	if (m_boolReadyToConnect == 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:
    Code:
    Funktion(......., [out, retval] BOOL *pVal)
    3.
    If I execute the following command...
    Code:
    MyObject = win32com.client.Dispatch("MyCOM.MySuperCOMObject")
    ...what is the return value from which I know whether it was succesful? 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:
    Code:
    def foo():
        if MyObject == None:
            print "COM not there yet"
        else:
            print MyObject.Message
    I'd really appreciate any help. Or even a hint where to look in the "original" documentation. FYI: I do use ActiveState ActivePython 2.2 which already includes win32com.

    Bye,
    Matthias "T.T.H." Grobe
  2. #2
  3. No Profile Picture
    Hi, I'm Calvin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    LosAngeles, SanDiego, Houston
    Posts
    50
    Rep Power
    11
    what's up TTH... i don't know what COM objects are, and I'm really shaky on C++ (i hate it, actually)... i'll suggest what I think, but there are probably better ways that i'm unaware of. at least, I think I can help you with the first question for sure.

    (1)
    to know if your import stmt went through ok, just use dir().

    ie:
    --------------------------------------
    import cgi

    if 'cgi' in dir():
    print "yep, it's there"
    --------------------------------------

    OR, you could just try to use a function or call an object of the module you imported inside of a 'try' block, with an 'except' block right after it in case the import stmt didn't go through. it wasn't imported correctly, your 'except' code will execute, because you'll get a NameError. of course, the thing with this second way is that you'll have to be absolutely certain that the object/function that you call from that module in your try block exists in the module you want to import, and that you're using it correctly.

    (2)
    do you know before hand if E_FAIL has a constant value? the way you have it used in the code, it seems like E_FAIL should be readily distinguishable from S_OK, but I could be totally wrong. I don't know what the contexts of either are, nor what kind of variable, etc etc... if my assumptions are correct (time has taught me the chances are slim). alternatively, you could return an array of length 2 with the first index indication true or false (false would be E_FAIL, I guess), and the second index being the value of E_FAIL or S_OK, correspondingly. that way, you can just check the first index of the returned array in python.

    (3) & (4)
    sorry, can't help ya there i don't know enough about the flow or context of your code to be able to suggest anything that'd be useful.

    hope i've been at least somewhat helpful. have a good day.

IMN logo majestic logo threadwatch logo seochat tools logo