I would want to know how to marshall a C# string to a native C++ char*. I was trying it but nothing seems to work. Thanks in advance.
Asked
Active
Viewed 1,959 times
0
-
possible duplicate of [char* pointer from string in C#](http://stackoverflow.com/questions/1658269/char-pointer-from-string-in-c-sharp) – Hans Passant Aug 07 '12 at 11:40
1 Answers
0
Remember that a C++ char is actually a byte so you need to pass it as a byte[] using something like
string str; // Contains string to pass to C++ DLL
byte[] bytes = Encoding.UTF8.GetBytes(str);
MyFun(bytes); // Call the C++ function with the string
See also Pass C# string to C++ and pass C++ result (string, char*.. whatever) to C# for a different perspective.
-
1You should not normally need to do that - you should normally just be able to declare the function as taking a System.String parameter. – Matthew Watson Sep 20 '12 at 09:56