Question

I've recently started learning about .net and the Windows API and I'm currently writting a class which talks the serial port.

In order to write a line to the serial port, I use the WriteLine function which takes a parameter, System::String^. I would like to know if it is possible to initialise the System::String^ from a std::string.

I've noticed that I can do the following:

serialPort->WriteLine("stuff");

but I cannot do:

std::string data = "stuff";
serialPort->WriteLine(data.c_str());

also, I cannot do:

char data[] = "stuff";
serialPort->WriteLine(data);

nor

char* data = "stuff";
serialPort->WriteLine(data);

Could someone let me know what's going on here? Is VS somehow converting the literal to it's own thing when I pass it in?

Lastly, I'm not sure if I've selected the correct tags for this as I don't really know what this falls under.

Était-ce utile?

La solution

You can always call functions with string literals because the compiler automatically converts that to a value of the appropriate string type.

The problem here is that you're trying to mix the .NET Framework stuff (System::String) with the standard C++ string type (std::string), and they simply don't mix well. The .NET functions that accept a parameter of type System::String are not written or overloaded to accept a parameter of type std::string. If you're working with the .NET Framework, then you're using C++/CLI, and you're generally expected to use the .NET string type, not the C++ string type.

Analogously, standard C++ functions are expecting std::string and don't know anything about the .NET Framework string type. So if you're expecting to call functions from the C++ standard library, you should be using its string type.

C-style strings (char[]) are the old fallback, not something you really ever want to use when writing C++ code. std::string is always a better option, and it even provides the c_str() method that you already know about to use when calling an API function that expects a char array. But like the other two string types, this one isn't inter-convertible either.

One particularly large problem that you'll run into is that std::string and char[] types accept only "narrow" (non-Unicode) strings, whereas the .NET Framework System::String type works only with wide (Unicode) strings. That throws a further wrench in the mix when it comes to conversion. There's no way it can be as simple as a cast.

But that doesn't mean it's impossible to convert between the types. There's a helpful document on MSDN that explains how to convert between various C++ string types.

In summary, you can convert from C-style strings to System::String by using the appropriately overloaded constructor that accepts a char*:

const char* cString = "Hello, World!";
System::String ^netString = gcnew System::String(cString);
Console::WriteLine(netString);
delete netString;

And then, of course, you can convert from to std::string to System::String by combining the above method with the c_str() method:

std::string stdString("Hello, World!");
System::String ^netString = gcnew System::String(stdString.c_str());
Console::WriteLine(netString);
delete netString;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top