Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using a native C++ class directly from C# is technically possible, but it's not trivial, and it's rarely even a good idea. For starters, you have to know the names to use to import from the DLL, which will be the names after C++ name-mangling. You also can't directly access things like <code>vector</code> from C#.</p> <p>There are basically two good options:</p> <p>The first is to write a DLL with a C interface that uses only types that can be marshalled into CLR types. You may use pointers along with the <code>IntPtr</code> type, but you can't really dereference those pointers. You can pretty much just store them in your C# code and then pass them back to the native DLL when needed. And you can also use simple <code>struct</code> types as long as you don't need deep copy to work on them. This option involves using P/Invoke.</p> <p>The second option is to write a mixed-mode C++/CLI assembly that implements all the logic that needs to access your native code. This assembly can directly access classes and data from your C# code and also directly access your native code, although you should be forewarned that there are annoying breaks where you can't mix the two. For example, a <code>ref class</code> in C++/CLI can't have a <code>shared_ptr</code> member. However, it can have a raw C++ pointer as a member. A (mixed-mode) native class can also have access to a CLR handle type and make calls into the C# code through this. This option involves using C++ Interop.</p> <p>It's worth noting that you could also go the other way with C++ Interop. You could have your C# code access a mixed-mode C++/CLI assembly that provides a .NET interface to some native code. However, you will still have to do some translation in this case so it's not hugely better than the first option.</p> <p>A full tutorial on C++ Interop would be rather lengthy. I suggest you read up <a href="http://msdn.microsoft.com/en-us/library/2x8kf7zx%28v=vs.80%29.aspx" rel="nofollow">here</a> and do some further investigation of C++ Interop on Google.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload