Search code examples
pythoncomexeregsvr32

"DllRegitserServer not found" error for python_com_server


Here is my code for com server (the file name is "python_com_server.py"):

 import pythoncom, win32com.server.register
 import win32traceutil
 class Python_COM_Server_4:
    _public_methods_ = ['MachineInfo']
    _reg_progid_ = "PythonCOMServer_4"
    _reg_clsid_ = '{3B4B18D9-BE2C-43A5-B08C-A89B4334DFA1}'

    def MachineInfo(self, item=None):
        msg = "Your operating system: " 
        print (msg)

if __name__ == '__main__' or __name__ == 'python_com_server':
    win32com.server.register.UseCommandLine (Python_COM_Server_4)

and here is my setup.py:

from distutils.core import setup
import py2exe
import sys

class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the version info resources (Properties -- Version)
        self.version = "0.0.1"
        self.company_name = "my company"
        self.name = "my com server name"

my_com_server_target = Target(
    description = "my com server",
    # use module name for win32com exe/dll server
    modules = ["python_com_server"],
    # specify which type of com server you want (exe and/or dll)
    create_exe = True,
    create_dll = False
    )

setup(
    name="my_com_server",
    # the following two parameters embed support files within exe/dll file
    options={"py2exe": {"bundle_files": 1, }},
    zipfile=None,
    version="0.0.1",
    description="my com server",
    # author, maintainer, contact go here:
    author="First Last",
    author_email="some_name@some_company.com",
    # packages=["dir"],
    com_server=[my_com_server_target]
    )

I run the following command on the cmd line:

python setup.py py2exe

and it generates " python_com_server.exe" in the dist folder.

I then try to register it using:

regsvr32 dist\python_com_server.exe

but I get this error:

"The module "dist\pythion_com_server.exe" was loaded but the entry-point DllRegisterServer was not found.

I am using python 2.7 32 bit on the windows server 2012.

What am I missing?


Solution

  • In order to register the COM server of .exe, instead of using RegSvr32.exe, execute .exe itself with the /regserver parameter like this.

    python_com_server.exe /regserver
    

    RegSvr32.exe is a tool for registering COM servers of .dll and .ocx.
    DllRegisterServer/DllUnregsiterServer etc is the entry point for that, the COM server .dll and .ocx must have them.

    On the other hand, the COM server of .exe must register with its own code.
    Since the COM server of .exe does not have DllRegisterServer/DllUnregsiterServer etc, trying to register using RegSvr32.exe fails.

    For example, in MFC of VC ++, it is executed by UpdateRejistry() or UpdateRegistryAll() in InitInstance() of .exe.

    In Python's win32com, it corresponds to "win32com.server.register.UseCommandLine()" written by you.

    ctypes has the same function, and there is an explanation in "The main program" on this Implementing a COM server with ctypes.