[PATCH] win32: wx.activex feature

Hi,

    I've been using wx.activex to wrap an ActiveX control (WMP10), and
needed to get hold of its various COM-interfaces in order to make it
useful. However, I couldn't find any way to do this with the current
API, so I got an idea: By adding a call to RegisterActiveObject() in
wxie/wxactivex.cpp (in wxPython/contrib/activex), calls to
win32com.client.Dispatch() reaching out for the same CLSID will get
the existing instance instead of creating a new one (or by calling
pythoncom.GetActiveObject() to force it to get an existing object --
raising an exception if there isn't one). The way I use it right now
in my code is:

        wmp = wmpctl.WMPControl(*args, **kwargs)
        
        obj = win32com.client.Dispatch("WMPlayer.OCX.7")

        self.player = win32com.client.CastTo(obj, "IWMPPlayer")
        self.controller = win32com.client.CastTo(obj, "IWMPControls")
        self.settings = win32com.client.CastTo(obj, "IWMPSettings")

    My first approach was to modify wx.activex to be able to take a
Python COM-object (and/or adding a method returning one), but I
figured that it was too invasive and would add a direct dependency on
pywin32.

What do you think?

Regards,
Ole André

Patch (also attached):

--- wxPython-src-2.6.1.0\wxPython\contrib\activex\wxie\wxactivex.cpp-orig Sat
Feb 26 12:18:16 2005
+++ wxPython-src-2.6.1.0\wxPython\contrib\activex\wxie\wxactivex.cpp Mon
Jun 13 14:14:30 2005
@@ -317,9 +317,14 @@
     wxCHECK_RET(adviseSink.Ok(), _T("adviseSink not Ok"));

- // // Create Object, get IUnknown interface
+ // Create Object, get IUnknown interface
     m_ActiveX.CreateInstance(clsid, IID_IUnknown);
     wxCHECK_RET(m_ActiveX.Ok(), _T("m_ActiveX.CreateInstance failed"));

wxPython-2.6.1.0-wxactivex-registeractiveobject.patch (830 Bytes)

···

+
+ // Register object as active
+ unsigned long pdwRegister;
+ hret = RegisterActiveObject(m_ActiveX, clsid, ACTIVEOBJECT_WEAK,
&pdwRegister);
+ WXOLE_WARN(hret, "Unable to register object as active");

     // Get Dispatch interface
     hret = m_Dispatch.QueryInterface(IID_IDispatch, m_ActiveX);

Ole André Vadla Ravnås wrote:

Hi,

    I've been using wx.activex to wrap an ActiveX control (WMP10), and
needed to get hold of its various COM-interfaces in order to make it
useful. However, I couldn't find any way to do this with the current
API, so I got an idea: By adding a call to RegisterActiveObject() in
wxie/wxactivex.cpp (in wxPython/contrib/activex), calls to
win32com.client.Dispatch() reaching out for the same CLSID will get
the existing instance instead of creating a new one (or by calling
pythoncom.GetActiveObject() to force it to get an existing object --
raising an exception if there isn't one). The way I use it right now
in my code is:

        wmp = wmpctl.WMPControl(*args, **kwargs)
                obj = win32com.client.Dispatch("WMPlayer.OCX.7")

        self.player = win32com.client.CastTo(obj, "IWMPPlayer")
        self.controller = win32com.client.CastTo(obj, "IWMPControls")
        self.settings = win32com.client.CastTo(obj, "IWMPSettings")

    My first approach was to modify wx.activex to be able to take a
Python COM-object (and/or adding a method returning one), but I
figured that it was too invasive and would add a direct dependency on
pywin32.

What do you think?

It's a good idea. Thanks for the patch.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

It's a good idea. Thanks for the patch.

That's good to hear. Here's a bugfix for the previous patch, it
should unregister the object on shutdown (didn't think it was
necessary due to ACTIVEOBJECT_WEAK but apparently I was wrong, as my
application revealed after some testing).

Ole André

wxPython-2.6.1.0-wxactivex-registeractiveobject-bugfix.patch (1.34 KB)

Ole André Vadla Ravnås wrote:

It's a good idea. Thanks for the patch.

That's good to hear. Here's a bugfix for the previous patch, it
should unregister the object on shutdown (didn't think it was
necessary due to ACTIVEOBJECT_WEAK but apparently I was wrong, as my
application revealed after some testing).

Thanks. I've checked in the change.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!