[wxPython] any hope for a wxWindows COM server?

Has anybody got sample code of a working wxWindows COM server? I'm actually using PythonCard but if I could see how it was done in wxWindows that might help. I also have some basic questions about how to connect with a client. The client would want to attach to a running copy of the COM server the same way you can use the copy of Word that's running if it's already started.

I've got the COM server and client samples from win32all working so I know how to do the Non-GUI stuff.

Here's an attempt to wxPython a simple example. This is the text sample from the wxWiki tutorial with added basic COM server stuff:

···

#########################################
#comserver.py

from wxPython.wx import *

class MainWindow(wxFrame):
    _reg_clsid_ = "{0D583BF3-1106-4CEF-A540-7D3D9AC336A8}"
    _reg_desc_ = "Python Test COM Server"
    _reg_progid_ = "wxPython.COMserver"
    _public_methods_ = ['Hello']

    def __init__(self,parent,id,title):
        wxFrame.__init__(self,parent,-4, title, size = ( 200,100),
             style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
        self.control = wxTextCtrl(self, 1, style=wxTE_MULTILINE)
        self.control.WriteText("hello ")
        self.Show(true)

    def Hello(self, who):
        self.control.WriteText("Hello " + who)

if __name__=='__main__':
    import win32com.server.register
    win32com.server.register.UseCommandLine(MainWindow)

    app = wxPySimpleApp()
    frame = MainWindow(None, -1, "Small editor")
    frame.Show(1)
    app.MainLoop()

###################################
#and comclient.py

import win32com.client
w = win32com.client.Dispatch('wxPython.COMserver')
w.Hello('bob')

############################
here's the error I get when I try to run the com client

  File "C:\web\scripts\python\comclient.py", line 2, in ?
    w = win32com.client.Dispatch('wxPython.COMserver')
  File "C:\Python22\lib\site-packages\win32com\client\__init__.py", line 92, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "C:\Python22\lib\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\Python22\lib\site-packages\win32com\client\dynamic.py", line 72, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147467259, 'Unspecified error', None, None)

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx

Dave Primmer wrote:

Has anybody got sample code of a working wxWindows COM server?

I have a working wxWindows COM server, but can't really share the code just
now. Besides, I've thought of ways that it should be improved, but don't have
the time for that at the moment... :wink:

My solution was to have the COM object start a secondary thread, and have that
secondary thread import wxPython and create the wxApp. I use a pair of Queues
to send commands and responses back and forth between the COM thread and
wxPython thread. The queue can be checked in an EVT_IDLE handler -- just use
wxWakeUpOnIdle() to nudge the wxPython thread.

The important issue here is that wxPython/wxWindows needs to have its main loop
running in what it sees as the "main" thread -- it considers whatever thread
first imports wxPython to be the main thread. This means that you may need to
do some creative arrangement of imports, so that wxPython isn't imported until
*after* you've started the thread you intend to run it in.

I'm afraid I can't offer any advice as far as connecting to an already-running
process, however.

Hope that this helps!

Jeff Shannon
Technician/Programmer
Credit International

I had started work on a small cross-platform
library for inter-app communication, wrapping
existing platform-specific facilities like
COM, AppleEvents, DCOP, ORBit.

The Win32 COM version includes a sample where
a wxPython app acts as a COM server. There's
also a client that connects to the running
COM server

See qasid.sourceforge.net and browse around in
the CVS repository.

Manoj

Dave Primmer wrote:

···

Has anybody got sample code of a working wxWindows COM server? I'm actually using PythonCard but if I could see how it was done in wxWindows that might help. I also have some basic questions about how to connect with a client. The client would want to attach to a running copy of the COM server the same way you can use the copy of Word that's running if it's already started.

I've got the COM server and client samples from win32all working so I know how to do the Non-GUI stuff.

Here's an attempt to wxPython a simple example. This is the text sample from the wxWiki tutorial with added basic COM server stuff:

#########################################
#comserver.py

from wxPython.wx import *

class MainWindow(wxFrame):
   _reg_clsid_ = "{0D583BF3-1106-4CEF-A540-7D3D9AC336A8}"
   _reg_desc_ = "Python Test COM Server"
   _reg_progid_ = "wxPython.COMserver"
   _public_methods_ = ['Hello']

   def __init__(self,parent,id,title):
       wxFrame.__init__(self,parent,-4, title, size = ( 200,100),
            style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
       self.control = wxTextCtrl(self, 1, style=wxTE_MULTILINE)
       self.control.WriteText("hello ")
       self.Show(true)

   def Hello(self, who):
       self.control.WriteText("Hello " + who)

if __name__=='__main__':
   import win32com.server.register
   win32com.server.register.UseCommandLine(MainWindow)

   app = wxPySimpleApp()
   frame = MainWindow(None, -1, "Small editor")
   frame.Show(1)
   app.MainLoop()

###################################
#and comclient.py

import win32com.client
w = win32com.client.Dispatch('wxPython.COMserver')
w.Hello('bob')

############################
here's the error I get when I try to run the com client

File "C:\web\scripts\python\comclient.py", line 2, in ?
   w = win32com.client.Dispatch('wxPython.COMserver')
File "C:\Python22\lib\site-packages\win32com\client\__init__.py", line 92, in Dispatch
   dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python22\lib\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatchAndUserName
   return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python22\lib\site-packages\win32com\client\dynamic.py", line 72, in _GetGoodDispatch
   IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147467259, 'Unspecified error', None, None)

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users