Sufficient conditions for COM local server to be singleton

Bill Bell bill-bell at bill-bell.hamilton.on.ca
Wed Jun 20 20:38:09 CEST 2001


My essential question is: I had understood from reading previous 
discussions on this list that, by default, to make a COM local 
server fire up in only a single copy it is only necessary to declare 
"_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER". However, 
this doesn't seem to do the trick.

The following is a slightly altered version of the "Final Sample" 
server code from Chapter 12 of the Hammond & Robinson Win32 
book (O'Reilly).

from win32com.server.util import wrap, unwrap
 
import win32com.client, pythoncom
 
debugging = 0
if debugging:
    from win32com.server.dispatcher import DefaultDebugDispatcher
    useDispatcher = DefaultDebugDispatcher
else:
    useDispatcher = None
 
class Parent:
    _public_methods_ = ['CreateChild', 'KissChild']
    _reg_clsid_ = "{41A0B351-A708-48c2-9E12-56C4291E0F42}"
    _reg_progid_ = "PythonDemos.Parent"
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER

    def __init__(self):
        import time
        self.StartTime = time.asctime()
 
    def CreateChild(self):
        child = Child()
        child.Name = self.StartTime
        print "Our Python child is ", child
        wrapped = wrap( child, useDispatcher=useDispatcher )
        print "Returning wrapped ", wrapped
        return wrapped
 
    def KissChild(self, child):
        print "KissChild called with child ", child
        dispatch = win32com.client.Dispatch(child)
        print "KissChild called with child named ", dispatch.Name
 
        child = unwrap(child)
        print "The Python child is ", child
 
class Child:
    _public_methods_ = []
    _public_attrs_ = ['Name']
    def __init__(self):
        self.Name = "Unnamed"
 
if __name__=='__main__':
    import win32com.server.register
    win32com.server.register.UseCommandLine(Parent, 
debug=debugging)

I have just verified (using 'diff') that the only differences between this 
code and the original version of it are the following:

1. 'pythoncom' is imported (in order to have access to the constant 
mentioned at the beginning of this message).
2. 'debugging' is set to 0 (rather than 1, to switch off debugging)
3. different CLSID
4. _reg_clsctx_ is set as mentioned above
5. an __init__ is added to the Parent class to save instantiation 
time in an instance variable
6. CreateChild() sets name of child to Parent instantiation time.

When I inhibit complete execution of the following code (by setting 
a break on its final line in PythonWin) and run the same code 
meanwhile in a DOS box Parent is created twice, as evidenced by 
the different times for the names of the children.

import win32com.client, msvcrt, pythoncom

ParentObj = win32com.client.Dispatch("PythonDemos.Parent")

child = ParentObj.CreateChild()
print "DRIVECHAPTER12.PY: Child's name is ", child.Name
 
ParentObj.KissChild(child)
print "DRIVECHAPTER12.PY: I kissed my child"

pass

My apologies for returning so often to this topic!




More information about the Python-list mailing list