473,804 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hello.

I'm moving an application from VC 2003 to VC 2005 Beta2. In mixed mode DLLs
I need to set
System::Threadi ng::Thread::Cur rentThread->ApartmentSta te =
System::Threadi ng::ApartmentSt ate::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.
Nov 17 '05 #1
5 1642
Adriano Coser wrote:
Hello.

I'm moving an application from VC 2003 to VC 2005 Beta2. In mixed
mode DLLs I need to set
System::Threadi ng::Thread::Cur rentThread->ApartmentSta te =
System::Threadi ng::ApartmentSt ate::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?


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
Nov 17 '05 #2
> > I'm moving an application from VC 2003 to VC 2005 Beta2. In mixed
mode DLLs I need to set
System::Threadi ng::Thread::Cur rentThread->ApartmentSta te =
System::Threadi ng::ApartmentSt ate::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?


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

Generally using a module constructor is not recommended. Please refer to the
article
http://msdn2.microsoft.com/library/m...us,vs.80).aspx
which contains in detail what Carl explains on how to do Initialization of
Mixed assemblies.
Thanks,
Kapil
Nov 17 '05 #3
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.

Nov 17 '05 #4

"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.

Nov 17 '05 #5
Thanks a lot Willy.

I've just added /CLRTHREADATTRIB UTE: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: co***@altoqi.co m.br
website: www.altoqi.com.br
"Willy Denoyette [MVP]" <wi************ *@telenet.be> escreveu na mensagem
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..

"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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1331
by: Mike P | last post by:
I've been using Visual Studio 2005 Beta 1 for the last few weeks, and I now want to try using SQL Server 2005 Beta with it. I've been to the Microsoft URL http://lab.msdn.microsoft.com/express/sql/ which allows you to download a product called SQL Server Express (April CTP). It also specifies that you must uninstall all Express software and the .NET framework 2.0 from your machine before downloading. Does this mean uninstalling the VS...
5
1325
by: WRH | last post by:
Hello I have VS 2003 and would like to try the 2005 beta. Can someone tell me...any conflicts if installed with 2003? Is there any expiry date or other restrictions on the 2005 beta?
4
4524
by: Adriano Coser | last post by:
I'm getting the following error: error C3767: PictureSource - candidate function(s) not accessible when accessing a public member function of a managed class, either from managed or unmanaged code. The class is an owner draw control implemented into a Control Library. The access is on a DLLs that support CLR with old syntax. The code compiled OK on Beta-1.
10
2595
by: Adriano Coser | last post by:
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.
1
2962
by: Bob | last post by:
Trying to use VBisual web developer express 2005 beta 2. Dragged an accessdatasource to the blank web page. Started to configure. Entered location of an access database. On next page, clicked on Query builder. Got error with "Microsoft Visual Studio" in the title - I am using VWD express 2005 beta 2 although I also have Visual Studio 2003 installed. the text of the error is: Retrieving the com class factory for component w/ clsid...
2
1279
by: stand__sure | last post by:
Has microsoft changed something here? For some reason calls to System.Diagnostics.Trace get ignored UNLESS I explicitly define TRACE in my application -- this was not how things behaved in VS 2003. Have they changed this or do I have problems with my VS 2005 beta version?
2
1275
by: Christian Perthen | last post by:
Hi, I just replaced .NET framework 2.0 over .NET framework 2.0 beta and suddenly my VS 2005 beta doesn't work anymore. Anyone else experienced the same issue. Thanks Christian
2
1577
by: vbmark | last post by:
I get the following message box: Visual Basic 2005 Express Edition Beta 2- ENU Setup =================================================== Setup has detected that previous versions of the following product(s) are currently installed. Uninstall these applications before continuing with Setup. For more information, see the readme.htm file. This file can be
0
1529
by: Dr. Zharkov | last post by:
Hello. To see the graphics of technology DirectX 9.0 SDK Update - June 2005 in project Visual Basic, WindowsApplication1 from Visual Studio 2005 Beta 1, in file My Project, MyApplication.vb in a method: <Global.System.Diagnostics.DebuggerStepThrough()> _ Protected Overrides Sub OnCreateMainForm()
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.