开发者

Singleton COM object required

开发者 https://www.devze.com 2023-03-10 22:51 出处:网络
Is it po开发者_JAVA百科ssible 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?Note that you will have to to make

Is it po开发者_JAVA百科ssible 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号