Python COM server running as a singleton

Marc ENGEL marc.engel at recif.com
Thu Jan 17 12:20:15 EST 2002


Hello,

I want to be able to get a COM server written in Python running as a
singleton. I also want to Dispatch it from VB and from Python and to
get the same instance.

Actually, it is the Python.Dictionary that I want to modify to get a
unique Dictionary in my system.

I have looked in this newsgroup to find a way to do it but I didn't
succeed in getting something working properly. I could only share the
same dictionary between Python Command line and Pythonwin but not with
VisualBasic. I should say that if VisualBasic create the singleton,
than Python can use it (Python first try to connect to the existing
object) but in the other direction, it didn't work.

Here is a solution I found using GetActiveObject. Maybe it can help
someone else, maybe this solution already exists. In any case,
comments are welcome.

I replaced the code from _CreateInstance_ method of DictionaryPolicy
class by:

  def _CreateInstance_(self, clsid, reqIID):
    # Do this to get it a singleton
    try :
        # Try to get the existing instance of the object
        object = win32com.client.GetActiveObject(DictionaryPolicy._reg_progid_)
        self._wrap_(object)
    except pythoncom.com_error:
        # If the object is not in the list of Active object,
        # we need to create it
        self._wrap_({ })
        object = pythoncom.WrapObject(self, reqIID)
        pythoncom.RegisterActiveObject(object, self._reg_clsid_,
pythoncom.ACTIVEOBJECT_WEAK)
    return object

--
Marc



More information about the Python-list mailing list