PC Review


Reply
Thread Tools Rate Thread

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

 
 
Michael Kennedy [UB]
Guest
Posts: n/a
 
      10th Feb 2004
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



 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      10th Feb 2004
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.
 
Reply With Quote
 
Klaus H. Probst
Guest
Posts: n/a
 
      11th Feb 2004

"Michael Kennedy [UB]" <(E-Mail Removed)> wrote in
message news:e%23qg%23%(E-Mail Removed)...
> 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/



 
Reply With Quote
 
Michael Kennedy [UB]
Guest
Posts: n/a
 
      11th Feb 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Michael Kennedy [UB]
Guest
Posts: n/a
 
      11th Feb 2004
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" <(E-Mail Removed)> wrote in message
news:%23UBp%(E-Mail Removed)...
>
> "Michael Kennedy [UB]" <(E-Mail Removed)> wrote in
> message news:e%23qg%23%(E-Mail Removed)...
> > 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/
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Manage in unmanaged easier or unmanaged in managed garlic Microsoft Dot NET Compact Framework 5 28th Apr 2008 04:07 PM
Managed/Unmanaged code and #pragma managed/unmanaged ajtaylor@hushmail.com Microsoft VC .NET 6 18th Feb 2008 09:33 AM
mixed managed/unmanaged in same exe... how-to hide unmanaged? mike Microsoft VC .NET 4 17th Dec 2007 05:15 PM
Access violation in unmanaged code: Linking managed with unmanaged static libs Pixel.to.life Microsoft VC .NET 3 23rd Jul 2007 04:16 AM
call managed/unmanaged classes from unmanaged code bonk Microsoft VC .NET 1 1st Oct 2005 04:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:30 PM.