Unable to initialize COM in VC Dll when invoked via C# application

  • Thread starter Ashutosh Bhawasinka
  • Start date
A

Ashutosh Bhawasinka

Hi,

I have a C++ DLL, it exports some some plain C style functions. This
functions internally initializes COM and then MAPI. This works fine if I
use the DLL from a C++ application (I tested with a C++ Win32 console
application).

But, when I call/invoke those functions from a C# .Net application using
PInvoke, COM initialization fails in the DLL.

Do I need to initialize something??

Regards,
Ashutosh
 
S

SvenC

Hi Ashutosh,
I have a C++ DLL, it exports some some plain C style functions. This
functions internally initializes COM and then MAPI. This works fine
if I use the DLL from a C++ application (I tested with a C++ Win32
console application).

But, when I call/invoke those functions from a C# .Net application
using PInvoke, COM initialization fails in the DLL.

What is the HRESULT you get?
 
I

Igor Tandetnik

Ashutosh Bhawasinka said:
I have a C++ DLL, it exports some some plain C style functions. This
functions internally initializes COM and then MAPI. This works fine
if I use the DLL from a C++ application (I tested with a C++ Win32
console application).

But, when I call/invoke those functions from a C# .Net application
using PInvoke, COM initialization fails in the DLL.

Chances are high the C# application has already initialized COM, and you
are getting S_FALSE or RPC_E_CHANGED_MODE depending on whether you are
trying to join the same or different kind of apartment the calling
thread has joined. You would get the same problem in a C++ application
if that application has initialized COM for whatever reason.

In general, a DLL should not try to initialize COM on a calling thread.
It can (and should) do so on threads that it iself explicitly creates.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
 
J

Jialiang Ge [MSFT]

Hello Ashutosh,

I agree with Igor. If the C++ DLL CoInitialize the thread as MTA:
CoInitializeEx(0, COINIT_MULTITHREADED);
and our current .NET thread has been initialized as STA by the [STAThread]
attribute, for example:

[DllImport(@"TestDLL.dll", EntryPoint = "Function1")]
public static extern int Function1();
[STAThread]
static void Main()
{
Marshal.ThrowExceptionForHR(Function1());
}
It will throw an exception: "cannot change thread mode after it is set.
(Exception from HRESULT: 0x80010106 (RPC_E_CHANGED_MODE))". The fact is
that, when a thread is first created by the OS as a result of calling
either CreateProcess or CreateThread, the newly created thread has no
associated apartment. Prior to using COM, the new thread must first enter
an apartment by calling CoInitialize(Ex) or OleInitialize. Once a thread
enters an apartment, it is illegal to change apartment types using
CoInitializeEx. Attempts to do so will result in the HRESULT
RPC_E_CHANGED_MODE. However, once a thread completely exits an apartment
using CoUninitialize, it may enter another apartment by calling
CoInitializeEx again. Ashutosh, please let us know the HRESULT value
returned from the C++ DLL CoInit call so that we can have a more accurate
analysis of the symptom.

Thanks
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
S

Sam Hobbs

Whenever a question says "does not work" or something similar such as
"fails" then it is nearly alwyays necessary to be specific about what does
not work or fails.
 
A

Ashutosh Bhawasinka

Hi,
Thanks for your comments. I have now placed the whole code in another
thread & its working.

Thanks a lot.

Regards,
Ashutosh
 
J

Jialiang Ge [MSFT]

Hello Ashutosh,

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of the suggestions? If you need further
assistance, feel free to let me know. I will be more than happy to be of
assistance.

Have a great day!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top