473,408 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

COM, Unmanaged/Managed C++, and CoInitialize()

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

Nov 22 '05 #1
4 3863
Michael,
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?


COM initialization is something you do on a per-thread basis. So if
the C++ code runs on the default C# thread, COM has already been
initialized by the CLR.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #2

"Michael Kennedy [UB]" <mk******@REMOVETHIS.unitedbinary.com> wrote in
message news:e%********************@TK2MSFTNGP12.phx.gbl.. .
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?
No. The apartment is initialized after the first call to unmanaged code
occurs. Before that you can set it through
Thread.CurrentThread.ApartmentState. After the first call you can't touch
it. And if you don't set it, it's either set to the default or it picks up
the attribute, if present.
How are you handling this? Is there an easier way to do this? Maybe some
magical attribute that I'm not aware of?


Like Mattias said, it's a per-thread issue. If all of your code is running
in the same thread (and/or the same process) then you need to keep in mind
that the CLR already called CoInitialize[Ex] for you. But again, this
happens only until the first call to unmanaged code occurs.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Nov 22 '05 #3
Hi Mattias,

Thanks for pointing this out to me. So the STAThread attribute does handle
this well. I guess the last time I tested this scenario I didn't carefullly
pay attention which threads were doing what.

Thanks again,
Michael

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uY**************@TK2MSFTNGP11.phx.gbl...
Michael,
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++ andC# right?


COM initialization is something you do on a per-thread basis. So if
the C++ code runs on the default C# thread, COM has already been
initialized by the CLR.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 22 '05 #4
Hi Klaus,

Thanks for the information. Last time I wrote something like this I guess I
didn't pay enough attention to my threading. I wrote a few tests for this
last night and it works like a charm.

Thanks again,
Michael
"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...

"Michael Kennedy [UB]" <mk******@REMOVETHIS.unitedbinary.com> wrote in
message news:e%********************@TK2MSFTNGP12.phx.gbl.. .
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?


No. The apartment is initialized after the first call to unmanaged code
occurs. Before that you can set it through
Thread.CurrentThread.ApartmentState. After the first call you can't touch
it. And if you don't set it, it's either set to the default or it picks up
the attribute, if present.
How are you handling this? Is there an easier way to do this? Maybe some
magical attribute that I'm not aware of?


Like Mattias said, it's a per-thread issue. If all of your code is running
in the same thread (and/or the same process) then you need to keep in mind
that the CLR already called CoInitialize[Ex] for you. But again, this
happens only until the first call to unmanaged code occurs.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Kennedy [UB] | last post by:
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#...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
2
by: Bonj | last post by:
Hello I have a problem with creating a COM object from C++. The COM object is called, say, My3rdPartyDLL.MyCOMObject. When I do it from unmanaged C++, with smart pointers, the DLL gets #imported...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
1
by: CharlesHenri | last post by:
Hello, i plan to use a managed dll containing WinForm classes in an unmanaged win32 exe. Starting from NOTHING, how do i have to proceed ? I have found the following tutorial on...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.