COM object registration

dsavitsk dsavitsk at e-coli.net
Wed Sep 18 00:50:34 EDT 2002


"Mark Hammond" <mhammond at skippinet.com.au> wrote in message
news:T_Rh9.17227$Ee4.51953 at news-server.bigpond.net.au...
> dsavitsk wrote:
> > if the module below is run from the command line, it registers and works
> > fine.  if, however, it is imported and the register() method is executed
it
> > does not work (giving a 'localserver' error).
>
> What doesn't work?  Registration or subsequent use of the object?  Can
> you be more specific about the error you got, or else attach code I can
> run without modification to see the error.
>
> Thanks,
>
> Mark.
>

here are 2 modules. call the first anything, I called the second
REGISTER_MODULE2.py for the example. (I tend to do anything COM related in
caps). Place them both on the path on one computer, and place the second on
a second computer.

the problem is illustrated below.

# -------- 1st ------
"module to register Python COM components"
import pythoncom
class regmodule:
    _reg_clsid_ = '{7E63BF81-E120-4CD9-A9AE-13B5B6BB4ADB}'
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    _public_methods_ = ['REGISTERM']
    _reg_progid_ = 'this_is.MODULE_REGISTER'

    def REGISTERM(self, mod):
        exec('import ' + str(mod) + ' as mr')
        mr.register()
if __name__ == '__main__':
    import win32com.server.register
    win32com.server.register.UseCommandLine(regmodule)
# -------------------
# -------- 2nd ------
import pythoncom
class regmodule2:
    _reg_clsid_ = '{2B1120DD-83F8-4850-91F6-B4F9601C16B7}'
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    _public_methods_ = ['GET_VALUE']
    _reg_progid_ = 'ecp.MODULE_REGISTER2'
    def __init__(self):
        self.x = 0
    def GET_VALUE(self):
        self.x += 1
        return self.x # it needs to do something :-)
def register():
    import win32com.server.register
    win32com.server.register.UseCommandLine(regmodule2)
if __name__ == '__main__': register()
# -------------------

1. register the first on computer 1
2. register the second on computer 2
3. do the following on computer 1
# instance the server
>>> f = win32com.client.Dispatch('this_is.MODULE_REGISTER')
# use it to locally register other server
>>> f.REGISTERM('MODULE_REGISTER2')
# instance the second on server
# my server is named camus
>>> a = win32com.client.DispatchEx('ecp.MODULE_REGISTER2', 'CAMUS')
# works fine
>>> a.GET_VALUE()
1
>>> a.GET_VALUE()
2
>>> del a
# try instance it locally
>>> a = win32com.client.Dispatch('ecp.MODULE_REGISTER2')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python\win32com\client\__init__.py", line 92, in Dispatch
    dispatch, userName =
dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "C:\Python\win32com\client\dynamic.py", line 81, in
_GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\Python\win32com\client\dynamic.py", line 72, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.IID_IDispatch)
com_error: (-2147467259, 'Unspecified error', None, None)

if you go back and regularly register the second module locally (double
clicking or what not) the local instancing will work fine. In some sense,
this might be useful to only allow remote use, but i suppose this can be
done by distributing a shell of the module as well.

Thanks,

-doug






More information about the Python-list mailing list