C++/CLI wrapper for native C++ to use as reference in C#

Ok, tutorial. You have a C++ class NativeClass that you want to expose to C#. class NativeClass { public: void Method(); }; 1) Create a C++/CLI project. Link to your C++ library and headers. 2) Create a wrapper class that exposes the methods you want. Example: #include “NativeClass.h” public ref class NativeClassWrapper { NativeClass* m_nativeClass; … Read more

C++ CLI error C3767: candidate function(s) not accessible

The problem is that std::string will compile as a internal (non public) type. This is actually a change in VS 2005+: http://msdn.microsoft.com/en-us/library/ms177253(VS.80).aspx: Native types are private by default outside the assembly Native types now will not be visible outside the assembly by default. For more information on type visibility outside the assembly, see Type Visibility. … Read more

In C++/CLI, how do I declare and call a function with an ‘out’ parameter?

C++/CLI itself doesn’t support a real ‘out’ argument, but you can mark a reference as an out argument to make other languages see it as a real out argument. You can do this for reference types as: void ReturnString([Out] String^% value) { value = “Returned via out parameter”; } // Called as String^ result; ReturnString(result); … Read more

What is the best way to convert between char* and System::String in C++/CLI

System::String has a constructor that takes a char*: using namespace system; const char* charstr = “Hello, world!”; String^ clistr = gcnew String(charstr); Console::WriteLine(clistr); Getting a char* back is a bit harder, but not too bad: IntPtr p = Marshal::StringToHGlobalAnsi(clistr); char *pNewCharStr = static_cast<char*>(p.ToPointer()); cout << pNewCharStr << endl; Marshal::FreeHGlobal(p);

how to convert System::String to const char*?

You can do this using the msclr::interop::marshal_context class: #include <msclr/marshal.h> Then: String^ something = “something”; msclr::interop::marshal_context ctx; const char* converted = ctx.marshal_as<const char*>(something); system(converted); The buffer for converted will be freed when ctx goes out of scope. But in your case it would be much easier to just call an equivalent managed API: System::Diagnostics::Process::Start(“netsh”, “the … Read more

How to use LINQ in C++/CLI – in VS 2010/.Net 4.0

You can use the Linq methods that are defined in the System::Linq namespace, but you’ll have to jump through a couple extra hoops. First, C++/CLI doesn’t support extension methods. However, the extension methods are regular methods defined on various classes in System::Linq, so you can call them directly. List<int>^ list = gcnew List<int>(); int i … Read more

What does System.Double[*] mean

This is a typical interop problem, the array was created in a COM Automation server. Which exposes the array as a SafeArray, the CLR automatically marshals them to a .NET array object and maintains its structure as specified in the safe array descriptor. A System.Double[] array is a very specific kind of array in the … Read more

Change C++/CLI project to another framework than 4.0 with vs2010

This shows up when you press F1 in the Framework and References dialog: By default for new projects, the targeted framework is set to .NET Framework 4. The IDE does not support modifying the targeted framework, but you can change it manually. In the project file (.vcxproj), the default targeted framework is represented by the … Read more