tags:

views:

247

answers:

3

hello,

what is the preferred method to pass a string between C++ and C#?

i have a c++ class where one of the functions takes a char const * const as parameter.

how would i call this function in C#? just using a c#-string doesnt seem to work as the function in C# requires a sbyte*

C++ class:

public ref class MyClass
{
public:
 void Sample(char const * const Name);
}

Error 2 Argument '1': cannot convert from 'string' to 'sbyte*'

thanks!

+3  A: 

If you are using managed C++, you can use System.String class

denisenkom
+1  A: 

You need to try casting your parameter in C# as an sbyte.

Sample((sbyte)nameOfParameter);

That should then work fine.

Smallgods
+1  A: 

IMO the best way would be to write a wrapper in C++/CLI that uses Marshal::StringToHGlobalAnsi or something like that to convert the System::String^ to a char pointer and than call that wrapper from C#.

helium