Answered very simple function for using in C#

  • Monday, April 16, 2012 9:22 AM
     
     

    Hi all

    I am very new with C++ and I am trying to build a function in C++
    to use in C#

    When I do this it works fine, but the code is now in the header file:
    // cClassLibrary.h

    #pragma once

    using namespace System;

    namespace cClassLibrary
    {
    public ref class Class1
    {
    public:
    int DoSomething(int a, int b)
    {return a + b;}
    };
    }


    When I try to use the .cpp file, I cannot get it to work ...
    I am able to compile, but I cannot see my method in C#.
    What am I doing wrong ??

    Thanks for any help.
    Regards, Frank

     

    // cClassLibrary.h

    #pragma once

    using namespace System;

    namespace cClassLibrary{ public class Class1
    {
    public:
    int DoSomething(int a, int b);
    };
    }

     

    // This is the main DLL file.

    #include "stdafx.h"

    #include "cClassLibrary.h"


    int DoSomething(int a, int b)
    {
    return a + b;
    }

All Replies

  • Monday, April 16, 2012 9:46 AM
     
     

    Change to:

    int Class1::DoSomething(int a, int b)
    {
        return a + b;
    }

  • Monday, April 16, 2012 9:53 AM
     
     
    Now I get this exception:
    error C2653: 'Class1' : is not a class or
    namespace name


    Regards
    Frank
  • Monday, April 16, 2012 10:04 AM
     
     

    Then add namespace:

    namespace cClassLibrary
    {
        int Class1::DoSomething(int a, int b)
        {
            return a + b;
        }
    }

    or add using directive.

  • Monday, April 16, 2012 10:19 AM
     
     

    Thanks, I am getting closer :-)
    I tried "cClassLibrary.Class1::DoSomething" ...

    Now I am able to compile,
    but the DoSomething function is not visible in C# ...

    Regards
    Frank

  • Monday, April 16, 2012 11:09 AM
     
     Answered

    Make sure Class1 is defined with “public ref class” and DoSomething is after “public:”.

  • Monday, April 16, 2012 11:18 AM
     
     

    great, that's it !
    Thanks a lot ! :-)

    Regards
    Frank

  • Monday, April 16, 2012 12:26 PM
     
     
    Sorry, I just have a very little additional question:
    In the context of declaration variables, what does ^ mean ??
    I guess % means something like C# ref ?

    System::Data::SqlClient::SqlConnection ^%_SqlConnection,
    System::String ^_Query,
    System::String ^_DataTableName

  • Monday, April 16, 2012 1:30 PM
     
     Answered
    Sorry, I just have a very little additional question:
    In the context of declaration variables, what does ^ mean ??
    I guess % means something like C# ref ?
     
    System::Data::SqlClient::SqlConnection ^%_SqlConnection,
    System::String ^_Query,
    System::String ^_DataTableName
    The hat denotes a "managed handle", which is like a moveable pointer. In Managed C++ (the predecessor of C++/CLI) both managed and unmanaged poinetrs were denoted by *, which could (and did) get confusing.
     
    An excellent book on C++/CLI is Nishant Sivakumar's "C++/CLI in Action".
     

    David Wilkinson | Visual C++ MVP