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. C#
  4. call C++ dll from C# without name mangling

call C++ dll from C# without name mangling

Scheduled Pinned Locked Moved C#
questioncsharpc++
4 Posts 3 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.
  • P Offline
    P Offline
    Paulraj G
    wrote on last edited by
    #1

    Hi All, I am calling a c++ dll from C# application. with out name mangling how can i call c++ function and class from c# application? Thanks in advance.

    G.Paulraj

    W 1 Reply Last reply
    0
    • P Paulraj G

      Hi All, I am calling a c++ dll from C# application. with out name mangling how can i call c++ function and class from c# application? Thanks in advance.

      G.Paulraj

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      You are going to have to use Interop(System.Runtime.Interopservices).Something to the tune of :-

      [Dllimport("your dll")]
      public static extern void functionName(parameters);

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      P 1 Reply Last reply
      0
      • W Wayne Gaylard

        You are going to have to use Interop(System.Runtime.Interopservices).Something to the tune of :-

        [Dllimport("your dll")]
        public static extern void functionName(parameters);

        When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

        P Offline
        P Offline
        Paulraj G
        wrote on last edited by
        #3

        Hi... thanks for ur quick reply. in .h file i have declared like

        __declspec(dllexport) char* BaseClassFunction();

        in .CPP file the definition is like

        extern "C" __declspec(dllexport) char* __stdcall ISim::BaseClassFunction()
        {
        static char* pszCPUType = "derived class function called";
        return pszCPUType;
        }

        from c# i am calling like following...

        const String _dllLocation1 = @"D:\Project\Dll.dll";
        [DllImport(_dllLocation1)]
        static extern String BaseClassFunction();

        String Baseclass = BaseClassFunction();
        MessageBox.Show(Baseclass.ToString());

        the above code is not working... any help will be appricated..

        G.Paulraj

        L 1 Reply Last reply
        0
        • P Paulraj G

          Hi... thanks for ur quick reply. in .h file i have declared like

          __declspec(dllexport) char* BaseClassFunction();

          in .CPP file the definition is like

          extern "C" __declspec(dllexport) char* __stdcall ISim::BaseClassFunction()
          {
          static char* pszCPUType = "derived class function called";
          return pszCPUType;
          }

          from c# i am calling like following...

          const String _dllLocation1 = @"D:\Project\Dll.dll";
          [DllImport(_dllLocation1)]
          static extern String BaseClassFunction();

          String Baseclass = BaseClassFunction();
          MessageBox.Show(Baseclass.ToString());

          the above code is not working... any help will be appricated..

          G.Paulraj

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

          Paulraj G wrote:

          the above code is not working.

          That is not a very helpful piece of information; exactly what is not working; does the program crash, do you see the wrong results, etc.? You have defined a DLL function as

          extern "C" __declspec(dllexport) char* __stdcall ISim::BaseClassFunction()

          and I suspect the ISim:: prefix may be affecting your exported name; you can check with Dependency Walker[^]. Also I am not sure whether a char* returned from C++ will be accepted as a String in C#, you should check the PInvoke rules for marshalling, and also this article[^], and this one[^], by Luc Pattyn[^].

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          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