问题
I wrote an OLE automation server (using Delphi). I usually start the OLE server manually and use it as a normal application. From time to time I start a client, which automatically connect to the existing OLE Server.
When I terminate the client, the server does not terminate (at least when it was started manually before the client) but it won't accept any other OLE connection. Starting another client will trigger a new server instead of reusing the first one.
How can I reuse the same server with the second client?
(Question edited to reformulate it correctly. In the original version I was asking how to prevent the server from terminating, which wasn't a good formulation)
回答1:
There is a setting "Instancing" in the COM Object Wizard in Delphi. Allowed values are "internal", "Multiple Instance", "Single Instance".
I wanted to reuse the same COM server with multiple clients. That is why I chose "single Instance" and though that I would have a single instance of my server application for all the clients. But I was wrong. "Single Instance" means that there will be only one instance of a COM connection in my server. I should have chosen "Multiple Instance" to allow multiple COM connection (but one after the other, not simultaneous) in the same server.
I think that the words used in the COM Object Wizard in Delphi are not really clear. Instead of "multiple instance", "single instance", it would be better to have "multiuse" and "single use" like in this article about OLE Server and VB.
回答2:
In the client, use
ConnectKind := ckRunningOrNew
and an existing server should be used instead of starting a new one.
回答3:
You should be able to increment the reference counter of the automation server when you start the server as a normal application. What you want to achieve is two-fold: Let the server not terminate when the client exits, and also let the server not terminate when you close your main form while the client is still running.
回答4:
Create the COM object as Singleton. And also to keep the object running even after the Client goes, put extra reference count. To do this call QI once inside the COM object.
回答5:
A note about the previous post 'There is a setting "Instancing" in the COM Object Wizard in Delphi.' : At least in C++ builder, this option can simply be changed afterwards in the project settings, item "ATL". This item only appears there for an EXE-ole-server after you have added the first automation object to it.
(I have also asked author of This fine page to mention this in item 18.).
回答6:
You can also try changing the identity of the user that launches the OLE server, if it is an Exe and not a dll, by running dcomcnfg and choosing Component Services/Computers/My Computer/DCOM Config and selecting your server.
You might have to play around with it, I can't remember the differences between them all but I think "Interactive User" should do it.
来源:https://stackoverflow.com/questions/495785/how-to-reuse-a-delphi-ole-server-with-a-second-client