Discussion:
[python-win32] py2exe com_server newbie problem
Blair Hall
2014-09-26 05:32:16 UTC
Permalink
I have been working on this all day, with no joy. Some help would be
appreciated :-)

I have a simple COM server (that I found on the internet):

#-------------------------------------------------------------------
import pythoncom

import sys

class HelloWorld:

#pythoncom.frozen = 1
if hasattr(sys, 'importers'):
_reg_class_spec_ = "__main__.HelloWorld"
_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
_reg_clsid_ = '{E3D5F332-F080-47B3-A319-A3A0E287E466}'
_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

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


if __name__=='__main__':
import sys
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)
#-------------------------------------------------------------------


This works just fine as a python module. I can run it, register the COM
server and use the Hello method in VBA code inside Excel.

However, I would like to make a DLL, register it, and be able to have the
same access to the COM server. I have not been able to do that so far.

I have the following simple setup.py module

#------------------------------------------------------------------------
from distutils.core import setup
import py2exe

setup(com_server = ["test"])
#------------------------------------------------------------------------

Using this with py2exe generates both test.dll and test.exe.

I can call test.exe with '--register' on the command line and the COM
server is registered.

However, when I use regsvr32 to register test.dll, the COM server does not
work with Excel. (although regsvr32 says that the DLL has been successfully
registered).

What am I missing?
Blair Hall
2014-12-08 03:34:55 UTC
Permalink
Update
--------

I now have something working, but not everywhere :-(

I have two different machines, both Win7 64-bit (one professional the other
home), both with identical versions of Python 2.7.8 and py2exe 0.6.9.

I can create a COM server DLL on the machine running Win7-Home (machine A)
but when I try the same process on the one running Win7-professional
(machine B) it does not work in the same way.

After using py2exe on machine B I cannot get regsvr32 to register the
server on either machine. But when I use py2exe on machine A the server can
be registered (on both machines).

I have used Dependency Walker to look at what is happening and I see that
during registration an attempt to load zlib.pyd is made. This file is not
available, so I think that is causing an error message. However, zlib is
not actually needed, so the registration of the DLL then proceeds (after
clearing the error dialog) and is successful. The same thing happens when
de-registering the DLL.

I have no idea why zlib is required when building on one machine and not
the other. Can anyone suggest a reason?
Post by Blair Hall
I have been working on this all day, with no joy. Some help would be
appreciated :-)
#-------------------------------------------------------------------
import pythoncom
import sys
#pythoncom.frozen = 1
_reg_class_spec_ = "__main__.HelloWorld"
_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
_reg_clsid_ = '{E3D5F332-F080-47B3-A319-A3A0E287E466}'
_reg_desc_ = "Python Test COM Server"
_reg_progid_ = "Python.TestServer"
_public_methods_ = ['Hello']
_public_attrs_ = ['softspace', 'noCalls']
_readonly_attrs_ = ['noCalls']
self.softspace = 1
self.noCalls = 0
self.noCalls = self.noCalls + 1
# insert "softspace" number of spaces
print "Hello" + " " * self.softspace + str(who)
return "Hello" + " " * self.softspace + str(who)
import sys
# running as packed executable.
# --register and --unregister work as usual
import win32com.server.register
win32com.server.register.UseCommandLine(HelloWorld)
# start the server.
from win32com.server import localserver
localserver.main()
import win32com.server.register
win32com.server.register.UseCommandLine(HelloWorld)
#-------------------------------------------------------------------
This works just fine as a python module. I can run it, register the COM
server and use the Hello method in VBA code inside Excel.
However, I would like to make a DLL, register it, and be able to have the
same access to the COM server. I have not been able to do that so far.
I have the following simple setup.py module
#------------------------------------------------------------------------
from distutils.core import setup
import py2exe
setup(com_server = ["test"])
#------------------------------------------------------------------------
Using this with py2exe generates both test.dll and test.exe.
I can call test.exe with '--register' on the command line and the COM
server is registered.
However, when I use regsvr32 to register test.dll, the COM server does not
work with Excel. (although regsvr32 says that the DLL has been successfully
registered).
What am I missing?
Loading...