C# using classes from C++ DLL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rich

    C# using classes from C++ DLL

    Yes, I'm a noob with .NET looking for help.

    My goal is to eventually write a managed DLL in C++ that is a wrapper
    for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.

    Initially, I am just trying to write a very simple managed DLL in C++
    and use it from C#. In my C++ DLL I define a namespace containing one
    public class, this class contains a couple public functions. In C# I
    reference the DLL and I can see the namespace and class, but I cannot
    see any members of the class. What am I doing wrong?

    Here are the details . . .

    My C++ DLL project contains one source file that looks like this:

    #define DLLEXPORT __declspec( dllexport )

    namespace test_ns {

    public class DLLEXPORT testclass {
    public:
    int x, y;

    int add_function(in t a, int b)
    {
    return(a + b);
    }

    int sub_function(in t a, int b)
    {
    return(a - b);
    }
    };
    }



    On the C# side this is what I have:

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using test_ns;
    using anothername;


    namespace anothername
    {
    public class anotherclass
    {
    public int x, y;

    public int addstuff(int a, int b)
    {
    return (a + b);
    }
    }
    }


    namespace MyFirstApplicat ion
    {
    class Program
    {
    static void Main(string[] args)
    {
    int i;

    testclass tc = new testclass();
    anotherclass ac = new anotherclass();

    /*
    * At this point, I can see testclass but
    * I cannot access any members (variables
    * or functions) - ???
    */
    // The compiler chokes on this with the error
    // 'test_ns.testcl ass' does not contain a definition for
    'add_function'
    i = tc.add_function (5, 6);

    // This works fine (class defined in C#)
    i = ac.addstuff(12, 13);
    Console.WriteLi ne("i = " + i);
    }
    }
    }

    I'm sure there is something simple I am missing. Can anybody enlighten
    me on this issue?

  • Marc Gravell

    #2
    Re: C# using classes from C++ DLL

    Well, I'm no managed C++ buff, but google turns up a few possibles,
    e.g.
    Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology


    The main difference I can see is the __gc modifier. Any use?

    Marc

    Comment

    • Willy Denoyette [MVP]

      #3
      Re: C# using classes from C++ DLL


      "Rich" <richard.s.wade @us.army.milwro te in message
      news:1163271122 .765524.67430@i 42g2000cwa.goog legroups.com...
      | Yes, I'm a noob with .NET looking for help.
      |
      | My goal is to eventually write a managed DLL in C++ that is a wrapper
      | for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.
      |
      | Initially, I am just trying to write a very simple managed DLL in C++
      | and use it from C#. In my C++ DLL I define a namespace containing one
      | public class, this class contains a couple public functions. In C# I
      | reference the DLL and I can see the namespace and class, but I cannot
      | see any members of the class. What am I doing wrong?
      |
      | Here are the details . . .
      |
      | My C++ DLL project contains one source file that looks like this:
      |
      | #define DLLEXPORT __declspec( dllexport )
      |
      | namespace test_ns {
      |
      | public class DLLEXPORT testclass {
      | public:
      | int x, y;
      |
      | int add_function(in t a, int b)
      | {
      | return(a + b);
      | }
      |
      | int sub_function(in t a, int b)
      | {
      | return(a - b);
      | }
      | };
      | }
      |
      |

      This is an unmanaged class definition not a managed class.
      Managed types use a different syntax, I would suggest you first take a look
      at the C++ language documentation in msdn
      http://msdn2.microsoft.com/en-us/library/xey702bw.aspx before you start
      using managed C++.
      Your class should look something like...

      public ref class TestClass {
      public: int x, y;
      int add_function(.. .)
      {...}

      };

      Note also that you may get better answers when posting C++ questions to the
      vc NG.

      Willy.


      Comment

      • Rich

        #4
        Re: C# using classes from C++ DLL


        Marc Gravell wrote:
        Well, I'm no managed C++ buff, but google turns up a few possibles,
        e.g.
        Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

        >
        The main difference I can see is the __gc modifier. Any use?
        >
        Marc
        Good article - I added the __gc modifier and it does work. This did
        require setting the switch for CLR "old syntax".

        I'll eventually figure out the "new syntax" but this works for now -
        thanks!

        Comment

        • Marc Gravell

          #5
          Re: C# using classes from C++ DLL

          MSDN will probably tell you the new syntax - the other respondant
          suggests "ref".

          Also, note that you may [possibly] be able to PInvoke the unmanaged dll
          directly from C# without even needing the MC++ wrapper, unless you are
          trying to do something specific.

          Marc

          Comment

          • Rich

            #6
            Re: C# using classes from C++ DLL


            Marc Gravell wrote:
            MSDN will probably tell you the new syntax - the other respondant
            suggests "ref".
            >
            Also, note that you may [possibly] be able to PInvoke the unmanaged dll
            directly from C# without even needing the MC++ wrapper, unless you are
            trying to do something specific.
            >
            Marc
            I had tried "ref" unsuccessfully - I'm sure I was doing something wrong
            and with a little research I'll eventually figure it out. I have been
            looking through various MSDN articles but I haven't entirely solved the
            puzzle just yet.

            I initially looked into the PInvoke approach, but this can get complex
            when you have lots of C structures that need to be used - one article
            suggested the C++ managed wrapper DLL as a better approach for cases
            like this.

            Thanks again.

            Comment

            • MrAsm

              #7
              Re: C# using classes from C++ DLL

              On 11 Nov 2006 10:52:02 -0800, "Rich" <richard.s.wade @us.army.mil>
              wrote:
              >My goal is to eventually write a managed DLL in C++ that is a wrapper
              >for an unmanaged C DLL, then use the managed DLL in C#, VB, etc.
              To use the unmanaged C DLL from C#, maybe you might consider wrapping
              the umanaged C DLL into an ActiveX (use MFC or ATL and standard
              unmanaged C++), and use C# COM interop.

              If I don't mistake, given an ActiveX to Visual C# 2005, it can
              automatically wrap it into C# class.

              Mr.Asm

              Comment

              Working...