unmanaged -> managed call (again)
-
Few days ago I asked a similar question, but it seems I wasn't clear about the problem. I'll try to rephrase question in hope that it will be more clear. I have a managed code that I would like to call from unmanaged application. This is the sample that explains the problem that I stumbled upon. Here is a C++ dll that I would like to call: using namespace System::Windows::Forms; extern "C" DLL2005_API int fnDll2005(void) { // unmanaged Message box ::MessageBoxA(0, "unmanaged text", "unmanaged caption", 0); // Managed message box System::Windows::Forms::MessageBox::Show("managed text", "managed caption"); return 42; } -------- I am trying to call this DLL from a sample console application written in VC6: extern "C" __declspec(dllexport) int fnDll2005(void); int main(int argc, char* argv[]) { fnDll2005(); return 0; } so far so good, everything works fine, console application calls dll and both dialog boxes are shown, unmanaged and managed one. Problem arises when C++ function tries to call another managed method written in C#. Here is the C# code: using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace ClassLibrary1 { public class Class1 { public static int ShowDialog(string sText, string sCaption) { System.Windows.Forms.MessageBox.Show(sText, sCaption); return 0; } } } nothing special, a simple C# assembly, and here is the modified C++ function that should call C# method: using namespace System::Windows::Forms; using namespace ClassLibrary1; extern "C" DLL2005_API int fnDll2005(void) { ::MessageBoxA(0, "unmanaged text", "unmanaged caption", 0); System::Windows::Forms::MessageBox::Show("managed text", "managed caption"); ClassLibrary1::Class1::ShowDialog("class library text", "class library caption"); return 42; } and here's the problem. The moment that console application tries to call fnDll2005 function the program breaks with message "unhandled exception in console1.exe (KERNEL32.DLL)..." If I compile console program with VS2005 everything works fine. I don't understand why the program breaks when I call C# code when in the first example obviously CLR was started when I called it from C++ code. Since I'm a newbie in managed world I don't know if what I am trying to do is possible at all? Is there a way to call unmanaged function (from a program written in VC6) in a DLL that wo
-
Few days ago I asked a similar question, but it seems I wasn't clear about the problem. I'll try to rephrase question in hope that it will be more clear. I have a managed code that I would like to call from unmanaged application. This is the sample that explains the problem that I stumbled upon. Here is a C++ dll that I would like to call: using namespace System::Windows::Forms; extern "C" DLL2005_API int fnDll2005(void) { // unmanaged Message box ::MessageBoxA(0, "unmanaged text", "unmanaged caption", 0); // Managed message box System::Windows::Forms::MessageBox::Show("managed text", "managed caption"); return 42; } -------- I am trying to call this DLL from a sample console application written in VC6: extern "C" __declspec(dllexport) int fnDll2005(void); int main(int argc, char* argv[]) { fnDll2005(); return 0; } so far so good, everything works fine, console application calls dll and both dialog boxes are shown, unmanaged and managed one. Problem arises when C++ function tries to call another managed method written in C#. Here is the C# code: using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace ClassLibrary1 { public class Class1 { public static int ShowDialog(string sText, string sCaption) { System.Windows.Forms.MessageBox.Show(sText, sCaption); return 0; } } } nothing special, a simple C# assembly, and here is the modified C++ function that should call C# method: using namespace System::Windows::Forms; using namespace ClassLibrary1; extern "C" DLL2005_API int fnDll2005(void) { ::MessageBoxA(0, "unmanaged text", "unmanaged caption", 0); System::Windows::Forms::MessageBox::Show("managed text", "managed caption"); ClassLibrary1::Class1::ShowDialog("class library text", "class library caption"); return 42; } and here's the problem. The moment that console application tries to call fnDll2005 function the program breaks with message "unhandled exception in console1.exe (KERNEL32.DLL)..." If I compile console program with VS2005 everything works fine. I don't understand why the program breaks when I call C# code when in the first example obviously CLR was started when I called it from C++ code. Since I'm a newbie in managed world I don't know if what I am trying to do is possible at all? Is there a way to call unmanaged function (from a program written in VC6) in a DLL that wo
Im not sure but I suspect your problem is related to the cpp runtime. The cpp runtime in VS2005 is not compatible with the cpp runtime in VC6 or 2003. Are all your c++ projects dynamically linked to the runtime library? and are both using single or multi threaded version? Try using the dependency walker to determine which libraries are being used by each dll or exe