Mixed Mode Application Entry Point problem

T

Tom Archer

I'm writing mixed-mode (MC++ and MFC) applications. Under
certain circumstances - such as when using the BCL
Clipboard class - the application excepts. I've tracked
this down to understanding that I need to define my
application's main thread as STA. Therefore I've done the
following:

1) Set up a custom manage entry point: /ENTRY:MyMain
2) Put [System::STAThread] attribute on the new entry point
3) Call WinMainCRTStartup() to run the normal CRT entry
point.

(Here's the code from my stdafx.h file)
extern "C" void WinMainCRTStartup();

#using <System.dll>

[System::STAThread]
void MyMain()
{
WinMainCRTStartup();
}

However, when I do this I receive numerous linker warnings
about functions such as __amsg_exit already being defined.
Finally, I get an "unresolved external error"
_mainCRTStartup.
 
T

Tomas Restrepo \(MVP\)

Hi Tom,
I'm writing mixed-mode (MC++ and MFC) applications. Under
certain circumstances - such as when using the BCL
Clipboard class - the application excepts. I've tracked
this down to understanding that I need to define my
application's main thread as STA. Therefore I've done the
following:

1) Set up a custom manage entry point: /ENTRY:MyMain
2) Put [System::STAThread] attribute on the new entry point
3) Call WinMainCRTStartup() to run the normal CRT entry
point.

Humm... that's a bad idea. Instead, just initialize it by code instead of
with the attribute:

Thread::CurrentThread->ApartmentState = ApartmentState::STA;

first thing on your main() or WinMain()
 
T

Tom Archer

First, thanks Thomas for replying.

Second, it was a member of the VC++ compiler team that told me to do it
this way :)

Third, I'm a little lost on your idea. Remember that my app is an MFC
app and that I don't have control over the WinMain function - hence the
entry point.

Am I misunderstanding your help somewhere?
 
T

Tomas Restrepo \(MVP\)

Hi Tom,
First, thanks Thomas for replying.

Second, it was a member of the VC++ compiler team that told me to do it
this way :)

Third, I'm a little lost on your idea. Remember that my app is an MFC
app and that I don't have control over the WinMain function - hence the
entry point.

Am I misunderstanding your help somewhere?

Well, seems fairly convoluted still. You should be able to still set it on
your CWinApp::InitInstance, I think, before you do anything else.... But if
that doesn't work, ping me and I'll check it out in more detail ;)
 
T

Tom Archer

Worked like a charm!

I can definitely see why you thought the other proposed change was
absolute overkill.

I should have tried something like this, but just made the assumption
that the threadmodel change needed to be made at a much lower level than
my app obj's InitInstance fcn.

Thanks Thomas.

Cheers,
Tom Archer - Author
Inside C#
Extending MFC Applications with the .NET Framework
 
T

Tom Archer

Ok. After some more investigation I now understand more fully what is
going on now.

The CLR will CoInitializeEx to MTA automatically for the main thread if
that thread is not STA, which was the cause of my original problem.

Therefore, simply calling AfxOleInit (which will CoInitializeEx to STA)
also solves the problem.

Thanks again for your help.



Cheers,
Tom Archer - Author
Inside C#
Extending MFC Applications with the .NET Framework
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top