473,397 Members | 1,969 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,397 software developers and data experts.

C++/C# interop causes OleInitialize (STA) to fail?

Is there something about C++ / C# interop that initializes the threading
model to MTA so that OleInitialize will fail?

I have a mostly C++ app that calls a single C# class DLL. Only one source
file in the C++ app is compiled as managed (with /clr) – the single file in
which only one of the many functions creates a class object from the C# DLL
and invokes its methods (to decode an XML file the easy way).

During normal execution my code eventually creates an instance of an
MSFlexGrid ocx object (a grid control with column and row headers), which in
turn calls AfxOleInit and therefore OleInitialize. Works fine in normal
execution. I believe OleInitialize is by default STA.

By “normal” execution I mean the situation when I do not execute any code in
that single C++ file that calls into the C# DLL. If, however, I call
functions in that C++ file (even if they do not call the only function that
invokes C#), then when I create the MSFlexGrid object the internal call to
OleInitialize fails with the Trace message “Warning: OleInitialize returned
scode = RPC_E_CHANGED_MODE ($80010106)”.

From the help files it sounds like OleInitialize expects to be run in STA
mode, and the error message means “A previous call to CoInitializeEx
specified the concurrency model for this apartment as multithread apartment
(MTA)”. I tried putting breakpoints on every bit of MFC / ATL source code
that calls OleInitialize or CoInitialize to see if I could find the offender,
but nothing trapped.

If I create the MSFlexGrid before invoking C#, all is fine, and if I call
OleInitialize at the very beginning of my app all is fine. But I’m worried
there is a threading disaster waiting to happen.

What’s the deal?

Nov 17 '05 #1
7 6125
Take a look at the Paul Dilascia's CSTAThread class here

http://msdn.microsoft.com/msdnmag/issues/05/03/CATWork/
--
Cheers
Check Abdoul [VC++ MVP]
-----------------------------------

"Bill Cumming" <Bi*********@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
Is there something about C++ / C# interop that initializes the threading
model to MTA so that OleInitialize will fail?

I have a mostly C++ app that calls a single C# class DLL. Only one source
file in the C++ app is compiled as managed (with /clr) - the single file in which only one of the many functions creates a class object from the C# DLL and invokes its methods (to decode an XML file the easy way).

During normal execution my code eventually creates an instance of an
MSFlexGrid ocx object (a grid control with column and row headers), which in turn calls AfxOleInit and therefore OleInitialize. Works fine in normal
execution. I believe OleInitialize is by default STA.

By "normal" execution I mean the situation when I do not execute any code in that single C++ file that calls into the C# DLL. If, however, I call
functions in that C++ file (even if they do not call the only function that invokes C#), then when I create the MSFlexGrid object the internal call to
OleInitialize fails with the Trace message "Warning: OleInitialize returned scode = RPC_E_CHANGED_MODE ($80010106)".

From the help files it sounds like OleInitialize expects to be run in STA
mode, and the error message means "A previous call to CoInitializeEx
specified the concurrency model for this apartment as multithread apartment (MTA)". I tried putting breakpoints on every bit of MFC / ATL source code
that calls OleInitialize or CoInitialize to see if I could find the offender, but nothing trapped.

If I create the MSFlexGrid before invoking C#, all is fine, and if I call
OleInitialize at the very beginning of my app all is fine. But I'm worried
there is a threading disaster waiting to happen.

What's the deal?

Nov 17 '05 #2
That's roughly what I tried, and it does indeed solve the problem to call
CoInitialize (or OleInitialize) before the Framework calls it with
COINIT_MULTITHREADED.

Two questions:
1) My original question: is this a threading disaster waiting to happen by
setting the mode to STA? Are there other conflicts I should be aware of?

2) By setting the mode to STA will that disable the garbage collector?

Thanks!

"CheckAbdoul" wrote:
Take a look at the Paul Dilascia's CSTAThread class here

http://msdn.microsoft.com/msdnmag/issues/05/03/CATWork/
--
Cheers
Check Abdoul [VC++ MVP]
-----------------------------------


Nov 17 '05 #3
Hi,
That's roughly what I tried, and it does indeed solve the problem to call
CoInitialize (or OleInitialize) before the Framework calls it with
COINIT_MULTITHREADED.

Two questions:
1) My original question: is this a threading disaster waiting to happen by
setting the mode to STA? Are there other conflicts I should be aware of?
Definitely not. The runtime simply defaults to MTA, if it
detects that CoInitialize was not called before, and
if the main entry point of an executable assembly doesn't
apply the STAThreadAttribute.
2) By setting the mode to STA will that disable the garbage collector?
No.

Rob

Thanks!

"CheckAbdoul" wrote:

Take a look at the Paul Dilascia's CSTAThread class here

http://msdn.microsoft.com/msdnmag/issues/05/03/CATWork/
--
Cheers
Check Abdoul [VC++ MVP]
-----------------------------------


Nov 17 '05 #4
Bill,
Is there something about C++ / C# interop that initializes the threading
model to MTA so that OleInitialize will fail?


Well, by default the CLR will initialize managed threads into the MTA. In C#
or VB, you can usually prevent this for the application's main thread by
tagging the entry point method with the [STAThread] attribute.
Unfortunately, this doesn't work reliably in Managed C++ because the main()
function is not actually the application's entry point (the actual one is in
the CRT).

Sounds like this might help:
http://support.microsoft.com/kb/824480
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Nov 17 '05 #5


Thanks for the info! I was getting nervous.
Nov 17 '05 #6

While the article applies directly to the issue I have, the proposed
solution is more appropriate for a command line app since you can more easily
get at the startup routine. But the article accurately describes my situation.

I merely added a call to OleInitialize(NULL) very early on in my app before
any calls to the Framework (which initializes COM as MTA), and that seems to
have solved my problem.

Thanks for your help!
Nov 17 '05 #7
Bill,

While the article applies directly to the issue I have, the proposed
solution is more appropriate for a command line app since you can more
easily
get at the startup routine. But the article accurately describes my
situation.

I merely added a call to OleInitialize(NULL) very early on in my app
before
any calls to the Framework (which initializes COM as MTA), and that seems
to
have solved my problem.


Glad to know. If your app does not have a managed entry point, then that
probably works. The problem with this, if there's a managed entry point, is
that you're not guaranteed it will work because you can't predict what the
runtime did before you...
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Nov 17 '05 #8

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

Similar topics

15
by: Mark Sisson | last post by:
Hey all. I've got a legacy COM dll that I'd like to use in my multithreaded C# app. Problem though is when I create several instances of the interop object and start them on their own threads...
1
by: Iain | last post by:
I raised this a few days ago and Dmitriy helped, but there's been a lot of posts under the bridge since then so I thought I'd start afresh. Basically, I have a COM Dll written in C++ (6). It's...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
0
by: Bill Cumming | last post by:
Is there something about C++ / C# interop that initializes the threading model to MTA so that OleInitialize will fail? I have a mostly C++ app that calls a single C# class DLL. Only one source...
12
by: Anil Krishnamurthy | last post by:
We have an ASP.NET application that uses COM objects through Interop. The web application requires access to network and database resources and hence, needs to impersonate a domain account. The...
0
by: Chris V | last post by:
Hello, I'm having problems sending MAPI Mail from an ASP.NET applcation (in VB) (which worked fine in traditional asp) When run on our test server, it returns an COMException (0x80010106): ]]...
7
by: Wiebe Tijsma | last post by:
Hi, I'm running a web application application using the Microsoft.Interop.Security.AzRoles version 1.2.0.0 in the GAC. After an upgrade to Vista, I also have a version 2.0.0.0 in the GAC. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.