1

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 const char var1 and const char var2[] as parameter.

When I call such function in C# , function accepts arguments types as sbyte*. Just using a c#-string doesnt seem to work as the function in C# requires a sbyte* and sbyte**

C++ class:

public ref class MyClass
{
public:
    void Sample(const char * var1,const char* Var2[]);
}

C# call...

class Program
    {
        static void Main(string[] args)
        {
               MyClass oClass = new MyClass();
               string var1 = "Variable1";
               string[] var2 = {"1","2"};
               oClass.Sample(var1,var2);
        }
}

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

So I need help in understanding how can I pass string arguments from managed C# to managed C++?

4

1 回答 1

2

char * [] 与 C# 字符串不同。在托管 C++ 中,您需要一个字符串^,像这样

public ref class MyClass
{
public:
    void Sample(String^ var1, array<String^>^ var2);
};
于 2013-05-16T07:08:57.180 回答