0
votes

Good night,

I was trying to make a simple dll in C++/CLI to use in my c# library using something like the following code:

// This is the main DLL file.

#include "stdafx.h"

namespace Something
{
    public class Tools
    {
        public : int Test (...)
        {
            (...)
        }
    }
}

I can compile the dll and load it into the C# project without any problems, and can use the namespace Something and the class Tools from C#. The problem is that when I try to write Tools.Test(something) I get an error message saying that Tools doesn't have a definition for Test. Why can't the compiler get the function, even if it is declared public?

Also... Can I share a class across two project, half written in C# and half written in managed C++?

Thank you very much.

3
Show us the other half of the code, the part that tries to invoke Test and fails. - abelenky

3 Answers

1
votes

C# can only access managed C++ classes. You would need to use public ref class Tools to indicate that Tools is a managed class to make it accessible from C#. For more info see msdn.

This class can then be used in either managed C++ or C#. Note that managed C++ classes can also use native C++ classes internally.

1
votes

You can share a managed class across a project, but what you've written in an unmanaged (i.e. standard C++ class. Use the ref class keyword to define a managed class in C++.

// This is the main DLL file.

#include "stdafx.h"

namespace Something
{
    public ref class Tools
    {
        public : int Test (...)
        {
            (...)
        }
    }
}
0
votes

The function is not static. try this in the

var someTools = new Tools();
int result = someTools.Test(...);

or make the method static :

public : 
   static int Test (...)
   {
            (...)
   }