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

    Join Date
    Sep 2003
    Rep Power

    question on C++ COM object accessed by Python via win32com


    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.

    How do I know that...
    import win32com.client
    was successful or not?

    In my COM object I do have the following C++ function:
    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:
    Funktion(......., [out, retval] BOOL *pVal)
    If I execute the following command...
    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?

    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"
            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.

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

    Join Date
    Sep 2003
    LosAngeles, SanDiego, Houston
    Rep Power
    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.

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

    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.

    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