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

CoInitialize in C# ?

Hi,

I wanted to instantiate a STA COM component twice, once in the main
thread and once in a secondary thread. All worked fine for the main
thread, but for the secondary thread, I got 'Query Interface failed'
errors when trying to access COM methods.
I once had the same problem in a C++ application. The solution there was
to call CoInitialize() for each thread. So I tried the same in C#; I
added the following code to my worker thread class:

[DllImport("ole32.dll")]
static extern int CoInitialize(IntPtr pvReserved);

And then called 'CoInitialize((System.IntPtr)null)'
in the worker thread function, before instantiating the 2nd COM object.
AND IT WORKED!

Questions:
- Is this allowed?
- Is this correct?
- Isn't there a pendant for CoInitialize() in C#
- If all is wrong, what would be the correct approach?

Thanks in advance
Urs

For direct contact: Remove ALL 4 underscore- characters from e-mail address
Apr 5 '07 #1
1 19244
Urs,

Yes, it is allowed. There is nothing stopping you from making the call.

Is it correct? Absolutely not.

When you create your new thread, you want to call the SetApartmentState
method on the Thread instance before you make any calls to create a COM
component in the thread (preferably, you should call it before you call
Start on the thread). Given that you need an STA apartment, you want to
pass the STA value from the ApartmentState enumeration.

The reason the component is created correctly on the main thread is
because the entry point of your program has an STAThread attribute on it
which tells the CLR to make that initial thread an STA apartment.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Urs Wigger" <_urs.wigger_@_leica-geosystems_.comwrote in message
news:OY**************@TK2MSFTNGP04.phx.gbl...
Hi,

I wanted to instantiate a STA COM component twice, once in the main thread
and once in a secondary thread. All worked fine for the main thread, but
for the secondary thread, I got 'Query Interface failed' errors when
trying to access COM methods.
I once had the same problem in a C++ application. The solution there was
to call CoInitialize() for each thread. So I tried the same in C#; I added
the following code to my worker thread class:

[DllImport("ole32.dll")]
static extern int CoInitialize(IntPtr pvReserved);

And then called 'CoInitialize((System.IntPtr)null)'
in the worker thread function, before instantiating the 2nd COM object.
AND IT WORKED!

Questions:
- Is this allowed?
- Is this correct?
- Isn't there a pendant for CoInitialize() in C#
- If all is wrong, what would be the correct approach?

Thanks in advance
Urs

For direct contact: Remove ALL 4 underscore- characters from e-mail
address

Apr 5 '07 #2

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

Similar topics

1
by: Max Ischenko | last post by:
Hi, I'm using adodbapi wrapper arount OLEDB driver and I got this error when calling adodbapi.connect(): (-2147221008, 'CoInitialize has not been called.', None, None) The weird part is that...
0
by: Gijs Korremans | last post by:
Hi, I'm extending a Python application that uses several com objects and threads. The origional programmer set the main thread to multithreaded by doing this: * sys.coinit_flags = 0 * import...
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#...
20
by: Bonj | last post by:
Hello. Does anybody know if there is a difference between the way C# calls a COM object, and the way C++ calls a COM object? And is there anyway to make the latter emulate the former? I have a...
19
by: Jazper Manto | last post by:
hi i read thousands of millions of articles about STA and MTA :-). everything very theoretical and i don't get it... could anybody explain me the and on a nice, small and simple example where...
13
by: Burt Johnson | last post by:
I have to use a COM DLL written by another person, and am having trouble. Part of the problem is that I am writing inside another DLL (non-COM), and do not know what has been CoInitialized or...
6
by: Gary Lee | last post by:
In VB.NET using CDO, I'd like to allow multiple threads to share a single MAPI.Session object. If I declare and instantiate sessions within each thread, I'm OK (although this negates the...
0
by: Ken Varn | last post by:
I have a thread that was running queries on a database using ADO.NET. The process owning the thread was exhibiting a memory leak on each call to run a query with SqlCommand::ExecuteReader() or...
14
by: Samant.Trupti | last post by:
Hi all, I have some code that I am unable to understand. Can you please help? int _tmain(int argc, _TCHAR* argv) { // Initialize COM environment. ccLib::CCoInitialize...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...

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.