How can I call my C# code from a C++ dll? Access violation
Background:
Big C++ MFC app that's not going to get rewritten any time soon. A few years ago I needed to add features and decided that it would be easier built with C# and .NET, so I needed a way to call into .NET functions with the C++ app. At the time we were using VC6.0 for the MFC app, and we built the .NET functionality using VS.NET 2003 and .NET 1.1. In VS.NET 2003 we built a regular win32 dll (no MFC) with exported functions that the MFC app could consume. The dll functions referenced our .NET assemblies and it all worked great. Now, our MFC app is still an MFC app, but it is compiled with VS9, and it still works fine with the old 2003 compiled dll. However, we are now faced with creating a new DLL (new features) and are building it with VS9 and .NET 3.5.
The problem:
I've created the new DLL with the /clr option. I've got an options form in our .NET assembly that I'm trying to invoke from the exported dll function.
I get an unhandled exception message like this:
Unhandled exception at 0x04359108 in IP2000.exe: 0xC0000005: Access violation reading location 0x00000000
The code that it doesn't like looks something like this:
FooTools::frmConfiguration ^ frmConfig = gcnew FooTools::frmConfiguration();
frmConfig -> ShowDialog();
The syntax has changed. We used to use *
and new
instead of ^
and gcnew
, but the old syntax won't compile (not sure if that's an issue anyhow).
I've tested that the exported function is getting called by placing a straight win32 ::MessageBox
and it gets called. I also tried calling some different managed code from the exported function, ie. System::Windows::Forms::MessageBox->Show
... , and it also worked fine. It just doesn't want to run this form.
It seems to me that something must have changed between VS 2003 and VS9 that is causing me some grief, but I can't seem to see what it is. This form works fine if called from a test Windows.Forms a开发者_JAVA百科pp.
精彩评论