简体   繁体   中英

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

When I call such function in C# , function accepts arguments types as sbyte*. Just using ac#-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++?

A char * [] is not the same as a C# string. In the managed C++, you want a String^, like this

public ref class MyClass
{
public:
    void Sample(String^ var1, array<String^>^ var2);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM