"Adriano Coser" <coser@%removet his%altoqi.com. br> wrote in message
news:e5******** ******@TK2MSFTN GP12.phx.gbl...
Carl and Kapil.
I added to my DLLs a source file like this:
__gc class QIVCadCctor
{
public:
QIVCadCctor()
{
System::Threadi ng::Thread::Cur rentThread->ApartmentSta te =
System::Threadi ng::ApartmentSt ate::STA;
}
static QIVCadCctor* cadCctor = new QIVCadCctor();
};
Is this right? The constructor will execute during .cctor initialization?
I made this in all DLLs with clr support, but I'm still getting the drag &
drop registration failure.
In fact, I get an exception on my debug output window when I try to set
STA apartment state model:
Managed Debugging Assistant 'InvalidApartme ntStateChange' has detected
a problem in 'c:\Fontes\QiCa d.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\Micr osoft.NET\Frame work\v2.0.50215 \sdk\bin\mdaBoi lerplate.exe.md a.config
for documentation.
Am I missing something?
Thanks for your help.
Adriano.
The calling thread runs in an MTA, so you can't change it's apartment once
set to MTA (after all it's bad practice to set the apartment in a DLL).
You have two options to solve this issue:
1.
- Remove the STA stuff from the constructor.
- Create new thread
- Set its apartment state to STA
- Start the thread
- Create/call your object in the newly created thread.
2. (preferably)
Set the 'main' thread apartment to STA (the thread running the main entry).
You can do this by means of the linker argument
/CLRTHREADATTRIB UTE:{STA|MTA|NO NE} and simply create/call your object on the
main thread.
Willy.