Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:27:18+00:00 2026-05-28T17:27:18+00:00

I’ve recently started learning about .net and the Windows API and I’m currently writting

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:27:19+00:00Added an answer on May 28, 2026 at 5:27 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.