Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

VC 2005 Beta 2 - How to: Explicitly Define a Module Constructor (.cctor)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adriano Coser - 27 Apr 2005 22:23 GMT
Hello.

I'm moving an application from VC 2003 to VC 2005 Beta2. In mixed mode DLLs
I need to set
 System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
so the drag and drop registration can work.

I used to do this in DllMain and it worked fine. But now I can't call
managed code from this point. I understand that I must provide a module
constructor (.cctor) to do managed code initializations.

I couldn't find information on how to do this. The help of  my Express
Edition gives me a script error when I try to access the topic "How to:
Explicitly Define a Module Constructor (.cctor)".

Does anyone have this orientations? Is this the right way to init managed
code on a mixed mode DLL?

Thanks in advance.
Regards,
Adriano.
Carl Daniel [VC++ MVP] - 28 Apr 2005 00:40 GMT
> Hello.
>
[quoted text clipped - 14 lines]
> Does anyone have this orientations? Is this the right way to init
> managed code on a mixed mode DLL?

Just put the code in the constructor of a managed object with file scope and
static storage duration and it'll automatically run in the .cctor.

-cd
Kapil Khosla [MSFT] - 28 Apr 2005 02:48 GMT
> > I'm moving an application from VC 2003 to VC 2005 Beta2. In mixed
> > mode DLLs I need to set
[quoted text clipped - 17 lines]
>
> -cd

Generally using a module constructor is not recommended. Please refer to the
article
http://msdn2.microsoft.com/library/ms173266(en-us,vs.80).aspx
which contains in detail what Carl explains on how to do Initialization of
Mixed assemblies.
Thanks,
Kapil
Adriano Coser - 29 Apr 2005 13:55 GMT
Carl and Kapil.

I added to my DLLs a source file like this:

__gc class QIVCadCctor
{
 public:
   QIVCadCctor()
   {
     System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::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 '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.

Am I missing something?

Thanks for your help.

Adriano.
Willy Denoyette [MVP] - 30 Apr 2005 16:11 GMT
> Carl and Kapil.
>
[quoted text clipped - 33 lines]
>
> 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
/CLRTHREADATTRIBUTE:{STA|MTA|NONE} and simply create/call your object on the
main thread.

Willy.
Adriano Coser - 03 May 2005 15:53 GMT
Thanks a lot Willy.

I've just added /CLRTHREADATTRIBUTE:STA to the application linker command
line and it worked fine. Now I don't need to do nothing special on my DLLs.

Regards,
Adriano.

AltoQi - Tecnologia Aplicada à Engenharia Adriano Coser Departamento de
Desenvolvimento Tel.: (48) 239-7000 ramal: 7069 e-mail: coser@altoqi.com.br
website: www.altoqi.com.br

>> Carl and Kapil.
>>
[quoted text clipped - 52 lines]
>
> Willy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.