Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串从托管C传递到托管C++; 在C++与C*之间传递字符串的首选方法是什么? 我有一个C++类,其中一个函数以 char const *const 为参数。< /p>_C#_.net_C++_String - Fatal编程技术网

C# 将字符串从托管C传递到托管C++; 在C++与C*之间传递字符串的首选方法是什么? 我有一个C++类,其中一个函数以 char const *const 为参数。< /p>

C# 将字符串从托管C传递到托管C++; 在C++与C*之间传递字符串的首选方法是什么? 我有一个C++类,其中一个函数以 char const *const 为参数。< /p>,c#,.net,c++,string,C#,.net,C++,String,在C#中如何调用此函数?仅仅使用c#-字符串似乎不起作用,因为c#中的函数需要一个sbyte* C++类: public ref class MyClass { public: void Sample(char const * const Name); } 错误2参数“1”:无法从“字符串”转换为“sbyte*” 谢谢 > P>如果使用C++,可以使用Stry.String类< p>您需要尝试将参数作为C字节转换成C字节。< /P> Sample((sbyte)nameOfParame

在C#中如何调用此函数?仅仅使用c#-
字符串
似乎不起作用,因为c#中的函数需要一个
sbyte*

C++类:

public ref class MyClass
{
public:
    void Sample(char const * const Name);
}
错误2参数“1”:无法从“字符串”转换为“sbyte*”


谢谢

> P>如果使用C++,可以使用Stry.String类

< p>您需要尝试将参数作为C字节转换成C字节。< /P>
Sample((sbyte)nameOfParameter);

那就可以了。

我认为最好的方法是用C++/CLI编写一个包装器,使用
Marshal::StringToHGlobalAnsi
或类似的东西将System::String^转换为char指针,然后从C#调用该包装器。

您能展示一下您的函数原型和给出错误的代码吗,我已相应地编辑了我的问题