user626528
user626528

Reputation: 14417

Singleton COM object required

Is it possible to create the single instance of COM object, and be sure all subsequent calls from any client will be made to this single instance only?

Upvotes: 6

Views: 1837

Answers (1)

Euro Micelli
Euro Micelli

Reputation: 34058

Note that you will have to to make your COM object run Out-of-process (exposed by an EXE).

Do you really need the very same COM object used everywhere? Or do just want to control the same underlying resources from a single control point?

COM doesn't support the Singleton pattern directly, but it doesn't stricktly forbid it either. It's just that there is no registry setting that says "always serve the same object". In fact, the standard COM instantiation mechanism requires that a truly new object be returned each time you call it (this mechanism is what new operators and CreateInstance() use internally) . That means that to make a proper COM singleton, you cannot let your clients create it themselves. This can all be done, but it's tricky and rarely necessary.

Your best bet - funny enough - is to NOT have a COM Singleton at all. Let the client create as many different objects as it wants. Instead of a single COM object, allow multiple COM objects but make those objects "shims" which communicate with a single - internal - object implementation. Don't expose the internal singleton implementation directly as a COM object at all. You will avoid a lot of headaches.

Upvotes: 9

Related Questions