Click here to Skip to main content
15,888,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All
I have a code that is in C++ and I am trying to write the c# equivalent. I encountered the following function which I can not make sense of. Therefore I can not write the C# code for it. Can anyone briefly describe what does this function do and what would the c# equivalent be or give me any pointers for conversion. Thanks

XML
static HRESULT Init(System::String^ InstanceName)
{
    wchar_t szInstanceName[MAX_PATH];
    pin_ptr<const wchar_t> ch = PtrToStringChars(InstanceName);
    wcscpy_s(szInstanceName, MAX_PATH, ch);
    return Init(szInstanceName);
};
Posted

1 solution

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:
C#
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
 
Share this answer
 
v2
Comments
Maciej Los 11-May-12 14:27pm    
Good answer, my 5!
I'm listening to you, my master ;)
Sergey Alexandrovich Kryukov 11-May-12 16:32pm    
Thanks a lot; and listen to the Force... :-)
--SA
[no name] 12-May-12 1:23am    
Quality answer.
Sergey Alexandrovich Kryukov 12-May-12 21:59pm    
Thank you,
--SA
BillW33 16-May-12 9:03am    
Very good explanation, +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900