Hi Thomas,

thanks for your reply. I used the following code to keep the server 
alive:

class OpcServer(CoClass):
        ref_count = 0

(...)

        def __init__(self):

(...)
                if Settings.KEEP_ALIVE and self.ref_count == 0:
                        # This will keep the server alive even if all clients 
are 
disconnected
                        self.IUnknown_AddRef(None)
                        self.ref_count += 1

The disadvantage is, that one server instance which is the first that is 
created by a client will stay alive as a zombie rather than the object 
is closed and the COM application stays in a listening mode if if it 
would have been started using "-embedding". I guess this is what using 
CoLockObjectExternal would achieve. However, as a quick fix it did work.

Thanks a lot.

> -----Ursprüngliche Nachricht-----
> Von: Thomas Heller [mailto:thel...@ctypes.org]
> Gesendet: Donnerstag, 12. April 2012 22:49
> An: comtypes-users@lists.sourceforge.net
> Betreff: Re: [comtypes-users] How to keep-alive a COM server without 
clients
> connected?
> 
> Am 12.04.2012 09:55, schrieb Jan Wedel:
> > I think IClassFactory.LockServer is exactly what I'm looking for.
> > However, I do not know how, when and where I should be able to 
access
> > this method. I checked the sources and found that the factory 
instance
> > gets lost somewhere in COMObject. [...]
> 
> You could somehow patch comtypes to make the factory instance 
available in the
> COM object that it has created, but there are probably much easier 
ways.
> 
> You could increment the COM refcount of your object from the object 
itself, by
> calling AddRef() on it.  In the COMObject base class, and by 
comtypes-magic,
> the method that must be called is self.IUnknown_AddRef(None).  Calling
> self.IUnknown_Release(None) will then decrement the refcount again if 
your are
> done and eventually end the process if there are no other references 
to it.
> 
> The 'official' way, according to MSDN, seems to be the 
CoLockObjectExternal
> function [1]; in comtypes it is not exposed but you can define a 
method in
> your COMObject based class like this:
> 
>      def CoLockObjectExternal(self, fLock, fLastUnlockReleases):
>          punk = self._com_pointers_.values()[0]
>          windll.oledll.ole32.CoLockObjectExternal(punk,
>                                                   fLock,
>                                                   fLastUnlockReleases)
> 
> I'm not really sure about the differences in the two approaches.
> 
> Thomas
> 
> 
> [1]
> http://msdn.microsoft.com/en-
> us/library/windows/desktop/ms680592%28v=vs.85%29.aspx
> 
> 
------------------------------------------------------------------------
------
> For Developers, A Lot Can Happen In A Second.
> Boundary is the first to Know...and Tell You.
> Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
> http://p.sf.net/sfu/Boundary-d2dvs2
> _______________________________________________
> comtypes-users mailing list
> comtypes-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/comtypes-users



------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to