[python-win32] How to get access to existing Com Server custom class?

Jeff Shannon jks55 at hotmail.com
Fri Jun 3 18:45:57 EDT 2016


Im using python 2.7.9, windows 7...

The overall goal: Have another application access our custom com server (already running at this point) and send it a message to be displayed. Obviously, there needs to be a single server, multiple clients.

Im trying to use some custom code as a com server. The class was created as:

class StatusServerClass:
     _public_methods_ = ...
     _reg_progid_ = "CseStatusServerLib.CseStatusServer"
     _reg_verprogid_ = "CseStatusServerLib.CseStatusServer"
     _reg_progid_ = "CseStatusServerLib.CseStatusServer"
     _reg_clsid_ = "{<GUID here>}"
     _reg_desc_ = 'CSE Status Server'
     _typelib_guid_ = "{<typelib GUID here>}"
     _typelib_version_ = 1, 0    # Version 1.0
     _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
     _reg_threading_ = "Apartment"   # Not used?

     def __init__.....


and registered using:

win32com.server.register.UseCommandLine(StatusServerClass)


I can see it in regedit and as far as i know, it looks ok. The GUID is right, name is right.

Now when i go to use it, this works just fine:

self.StatusClient = Dispatch('CseStatusServerLib.CseStatusServer')


but when i want to attach to a running instance from another exe (or even another python window for debug) using:

win32com.client.GetActiveObject("CseStatusServerLib.CseStatusServer")


it just gives me:

dispatch = pythoncom.GetActiveObject(resultCLSID)
com_error: (-2147221021, 'Operation unavailable', None, None)


Tells me that its not registered?

Ive tried using the GUID, Ive tried using pythoncom.GetObject with both the ID and the GUID... no luck.

Ive tried comtypes package and get the same thing.

Any ideas on what im doing wrong? Why does Dispatch find it by name, but GetActiveObject gets mad?

Seems that Dispatch working by name would suggest that the registering worked?

What else can i verify in the regedit?

thanks!!!

More testing...

In case you haven't realized yet, I know very little about this. But I have read that for win32com.client.GetActiveObject() to work, the server needs to be in the "running object table"... and its not.

So, I found some more example code that i used to register the class this way:

import win32com.server.util
wrapped = win32com.server.util.wrap(StatusServerClass)
flags = pythoncom.REGCLS_MULTIPLEUSE|pythoncom.REGCLS_SUSPENDED

handle = pythoncom.RegisterActiveObject(wrapped,
                            StatusServerClass._reg_clsid_,flags)


and that does allow the server to show in the running object table, and i can get this:

testSW = win32com.client.GetActiveObject("CseStatusServerLib.CseStatusServer")


to return without error.

But when i try to use it to access a Write method thats part of the class, i get this:

    testSW.Write(Message = 'test')
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 525, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: CseStatusServerLib.CseStatusServer.Write


so its like the original class information is gone.

How can i register in the running object table and preserve my class methods?

Also, seeing as how i know very little about this, by all means, suggest a better way of doing this: starting a server in python as a singleton and then accessing it from another application.

thanks!!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20160603/0245cc70/attachment.html>


More information about the python-win32 mailing list