Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. unmanaged -> managed call (again)

unmanaged -> managed call (again)

Scheduled Pinned Locked Moved Managed C++/CLI
questioncsharpc++dotnethelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dragan Matic
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • D Dragan Matic

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups