C
Carl Daniel [VC++ MVP]
Adriano said:Hello.
I'm moving an application VC 2003 to VC 2005 Beta2. I need to set STA
ApartmentState model so the drag & drop registration can work.
I used to do
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
as the first statment of _tWinMain and also in the DllMain of my
mixed mode assemblies.
On VC 2005 I can't do this statment in DllMain, so I was oriented by
Carl Daniel and Kapil to create a static instance of a class that makes
the
statment on its constructor. I included a class like the following in
all mixed mode DLLs, hoping the static instances would be initialized on
the .cctors.
__gc class QIVCadCctor
{
public:
QIVCadCctor()
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
}
static QIVCadCctor* cadCctor = new QIVCadCctor();
};
Even doing this, when the program tries to execute
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
on _tWinMain, I get the following error message on my debug output
window:
Managed Debugging Assistant 'InvalidApartmentStateChange' has
detected a problem in 'c:\Fontes\QiCad.NET\QiCad.NET.exe'.
Additional Information: Thread is attempting to set the apartment
state to STA, but it has already been set to MTA.
See
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\sdk\bin\mdaBoilerplate.exe.mda.config
for documentation.
Does anyone know what is missing?
I can't tell you anything very specific, but clearly - something else is
running, either in DllMain itself, or in the .cctor that has already called
CoInitializeEx and set the current thread to be part of the MTA. You might
be able to debug it by setting a breakpoint at CoInitializeEx(), and
examining the call stack.
-cd