开发者

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

开发者 https://www.devze.com 2022-12-11 17:44 出处:网络
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.

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
{
pu开发者_开发问答blic:
    void Sample(char const * const Name);
}

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

thanks!


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


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

Sample((sbyte)nameOfParameter);

That should then work fine.


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#.

0

精彩评论

暂无评论...
验证码 换一张
取 消