Pass a string from managed C # to managed C ++

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 parameter char const * const.

how can i call this function in c #? just using C # - stringdoes not seem to work, since a function in C # requiressbyte*

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
source share
3 answers

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

+4
source

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

Sample((sbyte)nameOfParameter);

.

+1

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

+1
source

Source: https://habr.com/ru/post/1723333/


All Articles