calling managed c++ from an unmanaged mfc application (VC 7.1)

A

Arne Adams

Hi,
I try to use a C# Dialog in a legacy MFC application.
The problem seems to boil down to the following:
from an unmanaged console application I can call any function of the managed
bridge - if I try to do the same from my mfc application the application
won't start
'SeaMain.exe': 'E:\WINDOWS\system32\mscoree.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\msvcr71d.dll' geladen, Symbole geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\mfc71d.dll' geladen, Symbole geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\gdi32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\user32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\shlwapi.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\advapi32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\rpcrt4.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\msvcrt.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\comctl32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\oleaut32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\ole32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\msvcp71d.dll' geladen, Symbole geladen.

Eine Ausnahme (erste Chance) bei 0x7c928fea in SeaMain.exe: 0xC0000005:
Zugriffsverletzung-Schreibposition 0x00000010.

Eine Ausnahme (erste Chance) bei 0x7c974ed1 in SeaMain.exe: 0xC0000005:
Access violation.

From what I have found in the net I conclude that calling managed code from
unmanaged code in an MFC application requires hosting the CLR somehow using
CorBindToRuntimeEx

Moreover it looks as if I have to use COM interop in that case.



If these conclusion are wrong, (which I silently hope) how can I call into
managed dll from an unmanaged MFC application?



Thanks in advance,

Arne
 
M

Marcus Heege

Arne Adams said:
Hi,
I try to use a C# Dialog in a legacy MFC application.
The problem seems to boil down to the following:
from an unmanaged console application I can call any function of the
managed bridge - if I try to do the same from my mfc application the
application won't start
'SeaMain.exe': 'E:\WINDOWS\system32\mscoree.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\msvcr71d.dll' geladen, Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\mfc71d.dll' geladen, Symbole geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\gdi32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\user32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\shlwapi.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\advapi32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\rpcrt4.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\msvcrt.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\comctl32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\oleaut32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\ole32.dll' geladen, Keine Symbole
geladen.

'SeaMain.exe': 'E:\WINDOWS\system32\msvcp71d.dll' geladen, Symbole
geladen.

Eine Ausnahme (erste Chance) bei 0x7c928fea in SeaMain.exe: 0xC0000005:
Zugriffsverletzung-Schreibposition 0x00000010.

Eine Ausnahme (erste Chance) bei 0x7c974ed1 in SeaMain.exe: 0xC0000005:
Access violation.

From what I have found in the net I conclude that calling managed code
from unmanaged code in an MFC application requires hosting the CLR somehow
using
CorBindToRuntimeEx

Moreover it looks as if I have to use COM interop in that case.



If these conclusion are wrong, (which I silently hope) how can I call into
managed dll from an unmanaged MFC application?



Thanks in advance,

Arne

1) There is no need to call CorBindToRuntimeEx manually.
2) Choose the /Zi compiler switch for your whole project
3) Add a new file to your project.
4) Modify the following property switches for the file
set /clr (mixed code compilation model)
set /EHa (async exceptions)
don't set /Gm, /RTC1 (non minimal rebuild, no runtime checks)
don't use /Yu (precompiled header)
5) In your file write something like this
#using "MyCSharpUIDLL.dll"

void ShowCSharpDialog()
{
MyCSharpDialog dlg = __gc new MyCSharpDialog();
dlg.ShowDialog();
}
6) Call that functin from your code

HTH

Marcus Heege
 

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