FAQ
In my C++ extension that wraps the Microsoft Active Accessibility API
I can get a pointer to a IWebBrowser2 COM interface from an
IAccessible representing the browser. I'd like to hand this pointer
back to my Python code and use it just like I would use the object I'd
get from

ie = DispatchWithEvents("InternetExplorer.Application", IEEvents)

Can I do that? I don't want to create a new instance of Internet
Explorer. I want to connect to a particular instance already running.

I have looked for an example of "adopting" a COM pointer but haven't
found one.

Thanks
Gary Bishop
Associate Professor
Computer Science
University of North Carolina at Chapel Hill

Search Discussions

  • Mark Hammond at Mar 18, 2003 at 10:12 pm

    Gary Bishop wrote:
    In my C++ extension that wraps the Microsoft Active Accessibility API
    I can get a pointer to a IWebBrowser2 COM interface from an
    IAccessible representing the browser. I'd like to hand this pointer
    back to my Python code and use it just like I would use the object I'd
    get from

    ie = DispatchWithEvents("InternetExplorer.Application", IEEvents)

    Can I do that? I don't want to create a new instance of Internet
    Explorer. I want to connect to a particular instance already running.

    I have looked for an example of "adopting" a COM pointer but haven't
    found one.
    There are 2 steps you must go through:
    1) Convert the C++ interface pointer to a PyObject
    2) Convert this PyObject to a "nice" win32com instance

    1) is a matter of calling PyCom_PyObjectFromIUnknown(), defined in
    PythonCOM.h

    2) is really a matter of calling the functions yourself from the C++
    code using the Python API - eg, PyImport_ImportModule() to get
    win32com.client, PyObject_GetAttr() to get the function from the module,
    and PyEval_CallObject() to make the call. (The exact names may not be
    correct there)

    I am not sure how you hope to get DispatchWithEvents working, given that
    the second param is a class object - however, I am sure you have some
    plan :) Further, I suggest that (2) actually be put in Python code.
    Your C++ code should do (1), then delegate to your own Python code.
    This code then does (2), and anything else necessary.

    Mark.
  • Gary Bishop at Mar 19, 2003 at 12:07 am
    Thanks. I'll certainly do step 2 in my python code.

    I assume I have to link with some library to define
    PyCom_PyObjectFromIUnknown. Do I need to build from the sources to get
    it?

    gb
  • Gary Bishop at Mar 23, 2003 at 6:23 pm

    Gary Bishop wrote:
    Thanks. I'll certainly do step 2 in my python code.
    I assume I have to link with some library to define
    PyCom_PyObjectFromIUnknown. Do I need to build from the sources to get
    it?
    gb
    For future generations of Googler's I'll answer my own question.

    First, to get access to Mark's function, I used "explicit linking" to
    the dll rather than building the library from source. The code in my
    SWIG interface file to link to the dll is:

    ----------------------
    %wrapper %{
    /* get the function we need from the win32com package using explicit linking */
    typedef PyObject* (*PYCOM_PYOBJECTFROMIUNKNOWN)(IUnknown *punk,
    REFIID riid,
    BOOL bAddRef = FALSE);
    PYCOM_PYOBJECTFROMIUNKNOWN PyCom_PyObjectFromIUnknown = NULL;
    void GetPythoncomDLL() {
    /* this should use the version of figure out the name */
    HINSTANCE hDLL = LoadLibrary("pythoncom22");
    if (hDLL != NULL) {
    PyCom_PyObjectFromIUnknown = (PYCOM_PYOBJECTFROMIUNKNOWN)
    GetProcAddress(hDLL, "PyCom_PyObjectFromIUnknown");
    }
    if (hDLL == NULL || PyCom_PyObjectFromIUnknown == NULL) {
    fprintf(stderr, "Explicit link to pythoncom22.dll failed\n");
    }
    }
    %}
    %init %{
    GetPythoncomDLL();
    %}
    ------------------

    Then to convert from my COM pointer to an object accessible with
    win32com I mimicked Mark's code like:

    ------------------
    // wrap it for use by Mark Hammond's win32com package
    PyObject* result = PyCom_PyObjectFromIUnknown(pUnk, IID_IDispatch);
    ------------------

    Finally in the Python wrapper for the C extension, I complete the job
    with:

    ------------------
    com = CComObject(self.ia, clsid)
    # convert it using Mark Hammdond's code
    com = WrapDispatch(com)
    if EventClass:
    com = DispatchWithEvents(com, EventClass)
    return com
    ------------------

    I got access to those functions from Mark's module with:

    ------------------
    import win32com.client
    from win32com.client import DispatchWithEvents
    WrapDispatch = win32com.client.__WrapDispatch
    ------------------

    Thanks to Mark for the fine code!
    gb

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 18, '03 at 6:55p
activeMar 23, '03 at 6:23p
posts4
users2
websitepython.org

2 users in discussion

Gary Bishop: 3 posts Mark Hammond: 1 post

People

Translate

site design / logo © 2022 Grokbase