First of all, conversion from what to what? The key here is: there are three different languages involved in this question, not two. They are C++, C++/CLI and C#.
You can "convert" (actually, translate directly) from C++/CLI to C# (not exactly 100%) and back, but C++ and C# are languages for different platforms — there is no "convert".
Now, the sample you show is more tricky: it's a mix of C++/CLI and native C++ code. It looks like it is a thunk from the manager domain to unmanaged one. It calls some unmanaged function
Init(wchar_t)
which you did not show to us. It looks like the code has unmanaged component (engine, something I cannot see), and it accept a wide character pointer for initialization. The method you show adds the managed function with
.NET System::String^
parameter which converts it to an unmanaged pointer and calls unmanaged
Init
.
This method makes no direct sense in C# which does not operate with unmanaged pointers directly. Instead, you could use P/Invoke to call the unmanaged
Init
which should be exported from some DLL. I'm not sure you really want it. One of the benefits of C++/CLI is the ability to avoid P/Invoke. Anyway, in this case it's pretty simple:
using System.Runtime.InteropServices;
using HRESULT = System.Int32;
const string DllName =
[DllImport(DllName, EntryPoint="Name")]
static extern HRESULT Init([MarshalAs(UnmanagedType.LPWStr] string instanceName);
Pay attention for
EntryPoint
: you might need to modify it, due to possible name mangling in C++:
http://en.wikipedia.org/wiki/Name_mangling[
^].
Use some binary dump utility (such as "dumpbin.exe",
http://msdn.microsoft.com/en-us/library/c1h23y6c%28v=vs.100%29.aspx[
^]) to see under what exact name it was really exported.
If you need to learn P/Invoke, start from here:
http://en.wikipedia.org/wiki/P/Invoke[
^],
http://msdn.microsoft.com/en-us/library/Aa712982[
^].
This CodeProject can also be useful:
Essential P/Invoke[
^].
—SA