Re: Unable to initialize COM in VC Dll when invoked via C# application
Ashutosh Bhawasinka <smbs-msdn@nospam.nospam> wrote:
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