tags:

views:

1072

answers:

3

I have an MFC application that uses several MFC extension DLL's. I want this app (and several other similar apps) to be able to access some parts of the .net framework. I wrote a C# library to do the .net work I want and was hoping to be able to write an MFC dll to hide all the C++/CLI code from my apps. The apps would stay as pure MFC apps and the only C++/CLI code would be in my new MFX extension DLL. However when I did this the apps crashed when accessing the new MFC C++/CLI dll. If I put the C++/CLI code in the apps it works ok and I can debug my way all the way in to the C#.

Does anyone understand why the dll idea doesn't work?

Thanks

+1  A: 

You can't reference managed assemblies from pure native code. You have to either flip the /clr switch on the consumer (either project-wide or in certain files,) or do some interop.

One interop option that will allow your consumer to say pure native is calling into the managed assembly via COM Callable Wrapper.

Aidan Ryan
A: 

The MFC dll project references the C# library and has one file compiled with /clr that handles the interface into my C# library. I have actually seen this work sometimes at run time but have never been able to debug into the MFC dll or into the C# code. However it doesn't seem to be at all stable and crashes in the majority of cases.

+1  A: 

I believe I have run into a similar problem. My setup was similar - A pure MFC app with a pure MFC DLL which in turn interacted with the C++/CLI DLL. Everything would run fine, but it would crash on exit. The problem was exacerbated while testing the pure MFC DLL using CppUnit.

On debugging, I found out that due to a bug, my C++ code was throwing first-chance exceptions for access violations (objects referenced via a dangling pointer) on exit. Now, the C++ runtime ignores these violations on exit, whereas the CLR does not. The CLR runtime throws an unhandled exception making it appear that the program / unit-test crashed.

Your problem maybe different, but it does sound quite similar to the one I had.

sachinjm