py2exe - COM Server problem

Paul paul.kemp at standardbank.com
Mon Mar 8 08:49:59 EST 2004


Hi all

I've been trying to build a COM Server as an executable using Py2exe. 
I had a few problems, so I came back to using Thomas Heller's basic
example (copied below).  When I build this, I still get an error.  I
can register Python.TestServer (testcomserver.exe --register),
however, when I try to create an instance of it (e.g. using VBA), a
command prompt window appears with the title:

d:\...\testcomserver.exe /automate

And the command prompt window just sits there!  Eventually, VBA times
out.

I really don't understand, because I've succeeded with COM servers and
Py2Exe in the past, and have never seen this before.

Can anyone help?

Thanks
Paul

---

import sys
import pythoncom

if hasattr(sys, 'importers'):
    # we are running as py2exe-packed executable
    pythoncom.frozen = 1

sys.frozen = 1

class HelloWorld:
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    if hasattr(sys, 'importers'):
        # In the py2exe-packed version, specify the module.class
        # to use. In the python script version, python is able
        # to figure it out itself.
        _reg_class_spec_ = "__main__.HelloWorld"
    _reg_clsid_ = "{B83DD222-7750-413D-A9AD-01B37021B24B}"
    _reg_desc_ = "Python Test COM Server"
    _reg_progid_ = "Python.TestServer"
    _public_methods_ = ['Hello']
    _public_attrs_ = ['softspace', 'noCalls']
    _readonly_attrs_ = ['noCalls']
    def __init__(self):
        self.softspace = 1
        self.noCalls = 0
    # __init__()

    def Hello(self, who):
        self.noCalls = self.noCalls + 1
        # insert "softspace" number of spaces
        return "Hello" + " " * self.softspace + str(who)

if __name__ == '__main__':
    if hasattr(sys, 'importers'):
        # running as packed executable.
        if '--register' in sys.argv[1:] or '--unregister' in
sys.argv[1:]:
            # --register and --unregister work as usual
            import win32com.server.register
            win32com.server.register.UseCommandLine(HelloWorld)
        else:
            # start the server.
            from win32com.server import localserver
            localserver.main()
    else:
        import win32com.server.register
        win32com.server.register.UseCommandLine(HelloWorld)

---------

setup.py:

from distutils.core import setup
import py2exe

setup(name='testCOMserver',
      scripts=['testCOMserver.py'])

vba:

dim o as object
set o = CreateObject("Python.TestServer")



More information about the Python-list mailing list