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. Using C++ class in C#

Using C++ class in C#

Scheduled Pinned Locked Moved C#
csharpc++helptutorialquestion
6 Posts 5 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.
  • N Offline
    N Offline
    Nematjon Rahmanov
    wrote on last edited by
    #1

    Hi. In my project have 3 module. 1 and 2 nd module writen in c++ and 3th module - Application module written in C#. Every module different parts of projects. My first module have like that class :

    class __declspec(dllexport) MyClass1
    {
    public :
    int n;
    MySimple1()
    {
    n=0;
    }
    ~MySimple1()
    {
    }

    int Plus(int a,int b)
    {
    
    	return n=(a+b);
    }
    

    };

    In my second module have like that class. And this class use 1 th class.

    class __declspec(dllexport) MyClass2
    {
    public :

    MySimple2(MyClass1 a)
    {
    	
    }
    ~MySimple2()
    {
    }
    
    ...
    

    };

    Can i use MyClass1 fields (i know how to use methods) in my C# app? In my C# app , how i can send MyClass1 like parameter to 2nd class ? How i can use like that in my C# app ?

    public MyClass1 my1=new MyClass1();
    int n=my1.Plus(10,12);

    MySimple2(my1); // calling through PInvoke.

    Thanks.

    We are haven't bug,just temporarily undecided problems.

    N C 2 Replies Last reply
    0
    • N Nematjon Rahmanov

      Hi. In my project have 3 module. 1 and 2 nd module writen in c++ and 3th module - Application module written in C#. Every module different parts of projects. My first module have like that class :

      class __declspec(dllexport) MyClass1
      {
      public :
      int n;
      MySimple1()
      {
      n=0;
      }
      ~MySimple1()
      {
      }

      int Plus(int a,int b)
      {
      
      	return n=(a+b);
      }
      

      };

      In my second module have like that class. And this class use 1 th class.

      class __declspec(dllexport) MyClass2
      {
      public :

      MySimple2(MyClass1 a)
      {
      	
      }
      ~MySimple2()
      {
      }
      
      ...
      

      };

      Can i use MyClass1 fields (i know how to use methods) in my C# app? In my C# app , how i can send MyClass1 like parameter to 2nd class ? How i can use like that in my C# app ?

      public MyClass1 my1=new MyClass1();
      int n=my1.Plus(10,12);

      MySimple2(my1); // calling through PInvoke.

      Thanks.

      We are haven't bug,just temporarily undecided problems.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      You would need to use pinvoke and create interop to access the unmanaged classes. This might help http://www.pinvoke.net/[^]


      I know the language. I've read a book. - _Madmatt

      N Z 2 Replies Last reply
      0
      • N Not Active

        You would need to use pinvoke and create interop to access the unmanaged classes. This might help http://www.pinvoke.net/[^]


        I know the language. I've read a book. - _Madmatt

        N Offline
        N Offline
        Nematjon Rahmanov
        wrote on last edited by
        #3

        Thanks for reply. I know how to use class methods with pinvoke. But i need use that class (NO METHOD) ? Any ideas? thanks.

        We are haven't bug,just temporarily undecided problems.

        S 1 Reply Last reply
        0
        • N Nematjon Rahmanov

          Thanks for reply. I know how to use class methods with pinvoke. But i need use that class (NO METHOD) ? Any ideas? thanks.

          We are haven't bug,just temporarily undecided problems.

          S Offline
          S Offline
          Saksida Bojan
          wrote on last edited by
          #4

          I found answer here[^] I hope that will help you

          1 Reply Last reply
          0
          • N Not Active

            You would need to use pinvoke and create interop to access the unmanaged classes. This might help http://www.pinvoke.net/[^]


            I know the language. I've read a book. - _Madmatt

            Z Offline
            Z Offline
            Ziad Elmalki
            wrote on last edited by
            #5

            Runtime Call Wrapepers are for COM. This is just a normal C++ class exposed in the export table. In C++ calling methods on a class use a calling convention called ThisCall. With ThisCall the "this" pointer gets passed into the method via the ECX register and everything else is the same as STD call. .Net supports this. You can specify this calling convention in the DllImport attribute. How the constructor and destructor get called is something else. When you specify ThisCall in the DllImport attribute the first parameter of the method must be a pointer to "This". The names are going to be mangled in the export table. Use a program like dependency walker to see what they are. Here is a good post I found on this site. How to Marshal a C++ Class[^]

            modified on Friday, October 1, 2010 2:38 PM

            1 Reply Last reply
            0
            • N Nematjon Rahmanov

              Hi. In my project have 3 module. 1 and 2 nd module writen in c++ and 3th module - Application module written in C#. Every module different parts of projects. My first module have like that class :

              class __declspec(dllexport) MyClass1
              {
              public :
              int n;
              MySimple1()
              {
              n=0;
              }
              ~MySimple1()
              {
              }

              int Plus(int a,int b)
              {
              
              	return n=(a+b);
              }
              

              };

              In my second module have like that class. And this class use 1 th class.

              class __declspec(dllexport) MyClass2
              {
              public :

              MySimple2(MyClass1 a)
              {
              	
              }
              ~MySimple2()
              {
              }
              
              ...
              

              };

              Can i use MyClass1 fields (i know how to use methods) in my C# app? In my C# app , how i can send MyClass1 like parameter to 2nd class ? How i can use like that in my C# app ?

              public MyClass1 my1=new MyClass1();
              int n=my1.Plus(10,12);

              MySimple2(my1); // calling through PInvoke.

              Thanks.

              We are haven't bug,just temporarily undecided problems.

              C Offline
              C Offline
              cepi69
              wrote on last edited by
              #6

              Create a wrapper class in a CLI project (C++.NET) and you should be able to access the C++ class from the .NET class. The advantage of this is that you keep your C++ project as is and you can debug both heaps easily. In this mode the unmanaged and managed heaps run within the same process without marshaling. I hope this helps.

              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