locked
How to convert C++ class as C# object? RRS feed

  • Question

  •  

    I am trying to use a class from C++ code as an object.

    How can I convert the C++ class to an object so that they can be accessed when I convert them to dll in C#.

    For Example:

     

    class extClass()
    
    {
    
    public:
    
    private:
    
    int myFun1();
    int myFun2();
    char m_strSNO[20];
    char m_strRegCode[20];
    
    }
    
    

    How can I make the class as objects to be called in C# If I use a DLL linkage?

     

    using System;<br/>
    using System.Linq;<br/>
    using System.Collections.Generic;<br/>
    using System.Windows.Forms;<br/>
    <br/>
    namespace TestSDKCS<br/>
    {<br/>
      static class Program<br/>
      {<br/>
        /// <summary><br/>
        /// The main entry point for the application.<br/>
        /// </summary><br/>
        [MTAThread]<br/>
        static void Main()<br/>
        {<br/>
          Application.Run(new Form1());<br/>
    <br/>
          <strong> extDevice d = new extdevice();<br/>
          <br/>
          Textbox1.Text = d.mstrSNO.ToString();<br/>
    </strong>
    <br/>
    <br/>
        }<br/>
      }<br/>
    }
    

     

    I am currently using DLLExport classes to import the functions

    but during runtime the myFun1 and myFun2 gives unHandledException error.

    please help to get myFun1 and myFun2  executed.


    Sri Hari Haran Seenivasan

    Sharing real experience is the best way to make every one Cautious or Happy
    Media Learning will be the future
    Cool Softwares
    As Software Engineers/Professionals we must take care of health (Obesity)
    Monday, October 25, 2010 4:38 AM

Answers

  • Hi Hari,

    You will need to use bridging functions to attain this functionality.

     

    eg. C++

    class MyTestClass
    {
     public:
      MyTestClass();
      ~MyTestClass();
    
      int MyMethod(int value);
    }
    

    You will then need to create bridging functions to access the class and functions.

    eg. C++

    /*
     Instantiate my object
    */
    extern "C" MyTestClass* CreateMyTestClass()
    {
      return new MyTestClass();
    }
    
    /*
    Dispose of my object
    */
    extern "C" void DisposeMyTestClass(MyTestClass* object)
    {
      if(object != NULL)
      {
        delete object;
        object = NULL;
      }
    }
    
    /*
    Call my class function
    */
    extern "C" int CallMyTestClassMethod(MyTestClass* object, int value)
    {
      if(object != NULL)
      {
        return object->MyMethod(value);
      }
    }
    
    Once that is done you can use p/invoke in c# to construct, call and dispose of your unmanaged class

    eg. C#

    [DllImport("MyTest.dll")]
    static public extern IntPtr CreateMyTestClass();
    
    [DllImport("MyTest.dll")]
    static public extern void DisposeMyTestClass(IntPtr object);
    
    [DllImport("MyTest.dll")]
    static public extern int CallMyMethod(IntPtr object, int nValue);
    

    You can then call these methods as required from c# code

    Hope this helps. (Code for example only)

    Monday, October 25, 2010 9:28 AM

All replies

  • Hi Hari,

    You will need to use bridging functions to attain this functionality.

     

    eg. C++

    class MyTestClass
    {
     public:
      MyTestClass();
      ~MyTestClass();
    
      int MyMethod(int value);
    }
    

    You will then need to create bridging functions to access the class and functions.

    eg. C++

    /*
     Instantiate my object
    */
    extern "C" MyTestClass* CreateMyTestClass()
    {
      return new MyTestClass();
    }
    
    /*
    Dispose of my object
    */
    extern "C" void DisposeMyTestClass(MyTestClass* object)
    {
      if(object != NULL)
      {
        delete object;
        object = NULL;
      }
    }
    
    /*
    Call my class function
    */
    extern "C" int CallMyTestClassMethod(MyTestClass* object, int value)
    {
      if(object != NULL)
      {
        return object->MyMethod(value);
      }
    }
    
    Once that is done you can use p/invoke in c# to construct, call and dispose of your unmanaged class

    eg. C#

    [DllImport("MyTest.dll")]
    static public extern IntPtr CreateMyTestClass();
    
    [DllImport("MyTest.dll")]
    static public extern void DisposeMyTestClass(IntPtr object);
    
    [DllImport("MyTest.dll")]
    static public extern int CallMyMethod(IntPtr object, int nValue);
    

    You can then call these methods as required from c# code

    Hope this helps. (Code for example only)

    Monday, October 25, 2010 9:28 AM
  • Is the same with .lib libraries?
    • Edited by Jon 123 Monday, January 3, 2011 2:06 PM extension mistake
    Monday, January 3, 2011 9:10 AM
  • Hello, I have been reading on internet and is not posible to use .lib directly in C#. First I have to make a .dll using methods like dhTodays answer.
    Tuesday, January 4, 2011 7:33 AM