CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2008
    Posts
    47

    Importing C++ Class in C#

    Hi,

    I was wondering if there was an easy way to import a C++ (Dll Exported)
    Class in a C#/.Net program so I could call :

    MyClass oMyClass = new MyClass(); //MyClass is coming from the C++ DllExport

    I know how to import a static function with
    [DllImport("myDll.dll", CharSet = CharSet.Auto]
    public static extern myFunc();

    But I would really like to know how to import a whole class and all of its public methods.

    Thank you
    Visual Studio 2005 Professional

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Importing C++ Class in C#

    You can't import a C++ class into C#, you can only import C-styled dll functions.

    The closest you can get is to create a COM dll with an interface (similar to the C++ class) and import the COM dll into C#.

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Importing C++ Class in C#

    Quote Originally Posted by lmoreault
    I was wondering if there was an easy way to import a C++ (Dll Exported)
    Class in a C#/.Net program so I could call :

    MyClass oMyClass = new MyClass(); //MyClass is coming from the C++ DllExport

    I know how to import a static function with
    [DllImport("myDll.dll", CharSet = CharSet.Auto]
    public static extern myFunc();

    But I would really like to know how to import a whole class and all of its public methods.

    Thank you
    Why do you want to import the class in a C# application? If you really need to use the methods in the C++ class you will have to write a wrapper C++ based DLL that exposes the methods so that you can P/Invoke or you can wrapp it up in a COM DLL. The COM dll approach will make it easier to you within a C# application.

  4. #4
    Join Date
    Feb 2008
    Posts
    47

    Re: Importing C++ Class in C#

    I already have the C++ source,

    how do I wrap my whole class then so I could use it in a Managed environment ?

    A short example would be... in C++
    I have that class..

    Code:
    class myClass {
          public:
                int getInt();
                void setInt(int nInt);
    
          private:
                int nMyint;
                char cMychar;
    
    }
    Then once in C#, I could use my C++ code so that I could do:

    Code:
    myClass oMyInstance = new myClass();
    oMyInstance.setInt(23);
    oMyInstance.getInt();
    //bla bla bla
    I have found a way to do it with static functions built in C++ (by exporting it to a DLL) but can't manage to export/import a whole class.

    Thanks a lot
    Visual Studio 2005 Professional

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Importing C++ Class in C#

    Quote Originally Posted by lmoreault
    I have found a way to do it with static functions built in C++ (by exporting it to a DLL) but can't manage to export/import a whole class.
    This is because you cannot import a C++ class into C#. You have already been told of the options in previous replies.

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Importing C++ Class in C#

    look up C++/CLI and wrap the native C++ code with C++/CLI code and compile it to a .net assembly and use it in your C# class (where you can use the class like you normally would).

  7. #7
    Join Date
    Feb 2008
    Posts
    47

    Re: Importing C++ Class in C#

    I have wrapped my native C++ class in a C++/CLI class (thank you for your patience by the way).

    But now, can't seem to find a way to use my new WrapperClass in my C# application, any clue on how to do it ?

    Best regard again and sorry for my beginner question.
    Visual Studio 2005 Professional

  8. #8
    Join Date
    May 2007
    Posts
    811

    Re: Importing C++ Class in C#

    You have to add reference in your C# project to dll which contains your managed code.

  9. #9
    Join Date
    Feb 2008
    Posts
    47

    Re: Importing C++ Class in C#

    Thanks STLDude,

    I have done that, it now works perfectly
    Visual Studio 2005 Professional

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured