Connecting to running win32com

ghubbar ghubbar at sandia.gov
Tue Oct 17 16:43:27 EDT 2000


Hi again,

I lost track of the thread, so I am starting a new one.
Thanks Alex and Mark for your attempts but I am afraid I mistated
the problem, confusing Visual C++ problems with COM problems.

There is the code that is not working.  The server uses wxPython,
but I have the same problem with WIN32 GUI code.

====
The server side is:


from wxPython.wx import *

counter = 0
class MyFrame(wxFrame):
        def __init__(self, parent, id, title):
                wxFrame.__init__(self, parent, id, title)
                self.timer = wxPyTimer(self.OnTimer)
                self.timer.Start(250)
                EVT_PAINT(self,self.OnPaint)

        def OnPaint(self,event):
                dc = wxPaintDC(self)
                dc.DrawText("%3d Counts" % counter, 0,0)

        def OnTimer(self):
                global counter
                counter = counter + 1
                self.Refresh(1)

class MyApp(wxApp):
        def OnInit(self):
                self.frame = MyFrame(NULL, -1, "COM Test")
                self.frame.Show(true)
                self.frame.SetFocus()
                self.SetTopWindow(self.frame)
                return true

class MyPythonComServer:
        _public_methods_ = ["ReturnMessage"]
        _reg_progid_ = "StatusData.Access"
        _reg_clsid_ = "{38C739C1-4EC0-11D4-BDFB-005004D78D9D}"
        print "Creating ComServer"

        def ReturnMessage(self,arg):
                global counter
#               counter = counter+1
                s = str(counter)
                return str(arg) + ":" + str(counter)

if __name__ == '__main__':
        print "Registering COM server..."
        import win32com.server.register
	apply(win32com.server.register.RegisterClasses([MyPythonComServer],
		{"debug":1}))

        app = MyApp(0)
        app.MainLoop()


===
This is the client

import pythoncom
import win32com.client

s = win32com.client.Dispatch("StatusData.Access",
	clsctx=pythoncom.CLSCTX_LOCAL_SERVER)
print s.ReturnMessage("Hello")

s=None


===

The GUI program shows counter increasing all the time, while the
WIN32 client always shows counter as 0.  As you stated, once
the win32com server is started by win32com.client.Dispatch,
other starting another one definitely serves up a the same
counter.  I can see that by uncommenting the counter increment
in ReturnMessage.

My whole goal here is for a remote program, eventually on a second
computer, to be able to access data collected by a long running
data acquisition program.  Unfortunately, the instance started by
running
	c:\progra~1\python\python serverCode.py
is not the instance run by win32com.

I can see a choice of creating a second, non-GUI program which the
GUI program also communicates with using COM, placing any data
there it might want to export.  The client on the second machine
then communicates with this second, slave, program.  This would
be acceptable, but seems a little kludgy.  Is there any way to
do it within a single program?

TIA
Gary



More information about the Python-list mailing list