M
Michael Kennedy [UB]
Hi,
I'm working on a project which makes use of COM in unmanaged C++. That
unmanaged C++ is then wrapped in a managed C++ class. Finally, this managed
C++ class is then consumed in a C# application. It looks like this:
C# App
|
| (one-to-many relationship)
|
|- Managed C++
|
| (one-to-one relationship)
|
|- Unmanaged C++
|
| (one-to-many relationship)
|
|- COM
My question what is the best way to handle the call(s) to
CoInitialize()/CoUninitialize() for the COM being used in the unmanaged C++?
As far as I know I have the following options:
1. Add a call to CoInitialize() in the constructor for the unmanaged C++
class and a call to CoUninitialize() in the destructor.
2. Add a static pair of methods to the unmanaged C++ class and call them at
the beginning / end of the C# app using either static methods in the managed
C++ or via PInvoke.
Now method 1 doesn't work for me because I may have more than one instance
of those classes and the destructor for the first one could clobber the COM
apartment for the second class before it's done with the COM objects it's
using.
As far as I can tell, the STAThread attribute doesn't actually initialize
the COM system in the unmanaged C++ and only applies to the managed C++ and
C# right?
How are you handling this? Is there an easier way to do this? Maybe some
magical attribute that I'm not aware of?
Thanks,
Michael
I'm working on a project which makes use of COM in unmanaged C++. That
unmanaged C++ is then wrapped in a managed C++ class. Finally, this managed
C++ class is then consumed in a C# application. It looks like this:
C# App
|
| (one-to-many relationship)
|
|- Managed C++
|
| (one-to-one relationship)
|
|- Unmanaged C++
|
| (one-to-many relationship)
|
|- COM
My question what is the best way to handle the call(s) to
CoInitialize()/CoUninitialize() for the COM being used in the unmanaged C++?
As far as I know I have the following options:
1. Add a call to CoInitialize() in the constructor for the unmanaged C++
class and a call to CoUninitialize() in the destructor.
2. Add a static pair of methods to the unmanaged C++ class and call them at
the beginning / end of the C# app using either static methods in the managed
C++ or via PInvoke.
Now method 1 doesn't work for me because I may have more than one instance
of those classes and the destructor for the first one could clobber the COM
apartment for the second class before it's done with the COM objects it's
using.
As far as I can tell, the STAThread attribute doesn't actually initialize
the COM system in the unmanaged C++ and only applies to the managed C++ and
C# right?
How are you handling this? Is there an easier way to do this? Maybe some
magical attribute that I'm not aware of?
Thanks,
Michael