Creating a registration free python COM component

306 views Asked by At

I've been trying to understand COM libraries but I'm still confused. If I want to make a python object com visible, the only instructions I can find are to make a python script that sets up a COM server which is invoked and used to generate class instances by name. IIUC, this is made possible by permanently adding some information to the registry that links a CLSID to the path of my server.py file; here's a minimum example:

class HelloWorld:
    # pythoncom.CreateGuid()
    _reg_clsid_ = "{7CC9F362-486D-11D1-BB48-0000E838A65F}"
    _reg_desc_ = "Python Test COM Server"
    _reg_progid_ = "PythonTestServer.HelloWorld"

    _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
        return "Hello" + " " * self.softspace + str(who)


if __name__ == "__main__": #run once to register the COM class
    import win32com.server.register

    win32com.server.register.UseCommandLine(HelloWorld) #this is what I want to avoid

Called like:

Dim a as Object = CreateObject("PythonTestServer.HelloWorld") 'late binding
a.softSpace = 10
?a.Hello("World") 'prints "Hello          World"

However I'm sure in the past I've downloaded some file.dll that I can just add as a reference to my COM client project (VBA) and I never call --register/regsrvr/etc. I assume the dll contains all the information needed to modify and revert changes to the registry at runtime. I can use Early or Late binding to create class instances. I even get intellisense if the referenced dll contains a type library.

The latter method feels much simpler and more portable. Is there a way to emulate this in python?

0

There are 0 answers