0

I've written a simple C++/CLI DLL that has 2 public static methods:

namespace GetMscVerLib
{
    public ref class CGetMscVer
    {
        static System::Int32 GetCompilerVersion ();
        static System::Int32 GetCompilerFullVersion ();
    };
}

I'm trying to call these methods from a C# console app in the same solution but the compiler doesn't recognize the methods and shows an error that says that the methods don't "exist in the current context":

namespace Get_MSC_VER
{
    class Program
    {
        static void Main (string[] args)
        {
            Int32 i32CompilerVersion     = CGetMscVer.GetCompilerVersion ();
            Int32 i32CompilerFullVersion = CGetMscVer.GetCompilerFullVersion ();
        }
    }
}

What is the correct syntax? (online searches have produced pages of irrelevant links with some of the search keywords, assuming DllImport or COM). This seems like it should be quite a simple matter but finding it is not.

Thanks

Gregor J
  • 31
  • 7
  • Does this answer your question? [How can I call to a C++ function from C#](https://stackoverflow.com/questions/9407616/how-can-i-call-to-a-c-function-from-c-sharp) – quaabaam Dec 03 '20 at 17:43
  • Whats the error youre getting? You should be able to use it like any other .Net library. – Ben Dec 03 '20 at 17:48
  • Try `CGetMscVer myObj = new CGetMscVer(); myObj.GetCompilerVersion ();` – Ben Dec 03 '20 at 17:50
  • 1: The error is "error CS1061: 'GetMscVerLib.CGetMscVer' does not contain a definition for 'GetCompilerVersion' and no extension method 'GetCompilerVersion' accepting a first argument of type 'GetMscVerLib.CGetMscVer' could be found (are you missing a using directive or an assembly reference?)" – Gregor J Dec 03 '20 at 17:54
  • 2. I've written the methods as static. When I change them to non-static and use an instance of the class I get the error "error CS0117: 'GetMscVerLib.CGetMscVer' does not contain a definition for 'GetCompilerVersionStatic'" – Gregor J Dec 03 '20 at 17:56
  • Sorry my c++ isnt great, They may need to be marked as public methods. It is picking up your type okay just not the methods. – Ben Dec 03 '20 at 17:57
  • Also, looking up the errors online hasn't been productive because the only responses assume calling from C++. – Gregor J Dec 03 '20 at 17:57

3 Answers3

1

First you need build your C++ program, you will get dll. Then you should create method with the same return value and add extern key word add DllImport attribute to your method. In your example method will look like this:

public class Program
{
    static void Main(string[] args)
    {
        var version = GetCompilerVersion();
        var fullVersion = GetCompilerFullVersion();
    }
    
    [DllImport("yourDllName.dll")]
    public static extern int GetCompilerVersion();
    
    [DllImport("yourDllName.dll")]
    public static extern int GetCompilerFullVersion();
}
0

This should work. Because the methods are private by default c# wont allow you to access them.

namespace GetMscVerLib
{
    public ref class CGetMscVer
    {
      public:
        static System::Int32 GetCompilerVersion ();
        static System::Int32 GetCompilerFullVersion ();
    };
}
Ben
  • 757
  • 4
  • 14
0

Another way, keeping everything static.

// C++/CLI DLL

namespace GetMscVerLib
{
    public ref class CGetMscVer abstract sealed
    {
    public:
        static System::Int32 GetCompilerVersion();
    };
}
// C# assembly that references the C/C++ DLL

namespace Get_MSC_VER
{
    class Program
    {
        static void Main (string[] args)
        {
            Int32 i32CompilerVersion = GetMscVerLib.CGetMscVer.GetCompilerVersion();
        }
    }
}
dxiv
  • 16,984
  • 2
  • 27
  • 49