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

7
  • Does this answer your question? How can I call to a C++ function from C# Commented Dec 3, 2020 at 17:43
  • Whats the error youre getting? You should be able to use it like any other .Net library. Commented Dec 3, 2020 at 17:48
  • Try CGetMscVer myObj = new CGetMscVer(); myObj.GetCompilerVersion (); Commented Dec 3, 2020 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?)" Commented Dec 3, 2020 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'" Commented Dec 3, 2020 at 17:56

3 Answers 3

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(); } 
Sign up to request clarification or add additional context in comments.

Comments

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 (); }; } 

Comments

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(); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.