0

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.

Masterile
  • 143
  • 2
  • 7
  • 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 Answers1

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.

Community
  • 1
  • 1
akton
  • 14,148
  • 3
  • 43
  • 47
  • 1
    You 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