Click here to Skip to main content
15,888,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to provide functionality to be used by both new C# applications and a legacy C++ (VC6!!) application. So I'm planning on exposing the interfaces via in-process COM. One of the issues is that the new functionality requires carrying some state across uses of these COM interfaces although the legacy application doesn't have a mechanism for passing state information.

This is data analysis like:

  1. Collect reference data for calibration. (legacy)
  2. Compute calibration. (legacy sw using my new code)
  3. Collect sample data. (legacy)
  4. Analyze using calibration. (legacy sw using my new code)

Probably repeat 3 & 4 for additional sample data.
Possibly repeat from 1 for additional data sets.

The legacy application is a third-party application for which we can write limited C++ plug-ins. The invocation of these are all independent.

This is all within a single execution of the application, so I thought that having a singleton "manager" class that held the state while the application was executing should keep things from going away while there are no COM references held.
If this assumption is not true, please let me know!

As I recall, there's no COM equivalent to invoking a C# static method/property to get the singleton instance of the "manager" class.
(Or am I wrong on this? It's been quite awhile since my COM heyday.)

So the question is how do I wrap the "manager" class interface in C# so all CoCreateInstance() in the C++ get the interface to the same "actual" instance?
(I.e., steps 2 and 4, above, will each call CoCreateInstance() for the "manager" interface, and will call Release() on that interface.)

Of course, suggestions of different strategies will be considered.

What I have tried:

I'm very familiar with making a C# singleton class.
I also have fairly recent experience exposing C# interfaces via COM.
The complication here is Singleton.
I haven't "tried" coding this yet.
One thought I had was a hack of a meta-class that could be CoCreateInstance()'ed and had a single property which returned the actual singleton instance interface. (:thumbsdown:)
Another was to have the COM exposed class implement the same interface as the singleton but have all method and property implementations just call the functionality on the singleton instance.

Any other suggestions?
Posted
Updated 27-Jul-16 12:13pm
Comments
[no name] 9-Jul-16 6:40am    
Have a look here: Singleton + COM + C# (Page 1) - .NET - Programmer's Town[^] and search there for "[Singleton, ComVisible (true)".

I did not read it carefully, but it sounds it is something you are looking for.

I know that some global named pipes are available, which enable you to transfer data. At one side you must properly manage your server while at the other side your clients are in charge.

It is a combination of named pipes and Inter-Process Communication. Use the "global" prefix.

I also can remember on earlier times, where a Windows service was a tool of choice for some global service on a machine.
 
Share this answer
 
I finally ended up with pretty much my second "option" above:
I was able to restructure the code so the singleton class wasn't necessary.
Instead, the functionality requiring the persistence was able to be moved into the classes doing the actual work and exposed there as static methods/properties.
The COM exposed class now calls the appropriate static methods/properties to accomplish its functionality.
There's no need for two versions of the same class, one for COM and one (singleton) to do the work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900