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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
newoperators andCreateInstance()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.