Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. std::string to System::String conversion

std::string to System::String conversion

Scheduled Pinned Locked Moved Managed C++/CLI
tutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    Hi, I have

    System::String ^ St = gcnew System::String("Hey ho!");
    //...
    std::string str("standard");

    St = str; //This line won't work, but it's what I'd like to achieve

    The only way I can make it work is by doing gcnew again St = gcnew System::String(str.c_str()); but I'm guessing this is not the right way. All the examples I have found tell how to do this when creating a new instance of System::String but none how to do it with an existing one.

    L A 2 Replies Last reply
    0
    • P piul

      Hi, I have

      System::String ^ St = gcnew System::String("Hey ho!");
      //...
      std::string str("standard");

      St = str; //This line won't work, but it's what I'd like to achieve

      The only way I can make it work is by doing gcnew again St = gcnew System::String(str.c_str()); but I'm guessing this is not the right way. All the examples I have found tell how to do this when creating a new instance of System::String but none how to do it with an existing one.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      System::String() has no constructor that accepts a std::string Try this:

      St = str->c_str();

      I must get a clever new signature for 2011.

      P 1 Reply Last reply
      0
      • L Lost User

        System::String() has no constructor that accepts a std::string Try this:

        St = str->c_str();

        I must get a clever new signature for 2011.

        P Offline
        P Offline
        piul
        wrote on last edited by
        #3

        Tried it, no good. error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'const char *' to 'System::String ^'

        L 1 Reply Last reply
        0
        • P piul

          Tried it, no good. error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'const char *' to 'System::String ^'

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sorry, I guess there is no simple way to do this other than :

          St = gcnew System::String(str.c_str());

          I must get a clever new signature for 2011.

          P 1 Reply Last reply
          0
          • L Lost User

            Sorry, I guess there is no simple way to do this other than :

            St = gcnew System::String(str.c_str());

            I must get a clever new signature for 2011.

            P Offline
            P Offline
            piul
            wrote on last edited by
            #5

            Fair enough. That's what I'd done, but... am I not re-allocating memory without freeing it first?? (I have to do it for a dozen of strings!)

            L 1 Reply Last reply
            0
            • P piul

              Fair enough. That's what I'd done, but... am I not re-allocating memory without freeing it first?? (I have to do it for a dozen of strings!)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              piul wrote:

              am I not re-allocating memory without freeing it first?

              It depends on the actual structure of your code. I think a more pertinent question would be: why are you using both std::string and System::String when you only really need one of them, probably the latter. For example code such as:

              std::string str("Some string");
              System::String ^ st = gcnew(str.c_str());

              serves no real purpose, in that the std::string is totally redundant.

              I must get a clever new signature for 2011.

              1 Reply Last reply
              0
              • P piul

                Hi, I have

                System::String ^ St = gcnew System::String("Hey ho!");
                //...
                std::string str("standard");

                St = str; //This line won't work, but it's what I'd like to achieve

                The only way I can make it work is by doing gcnew again St = gcnew System::String(str.c_str()); but I'm guessing this is not the right way. All the examples I have found tell how to do this when creating a new instance of System::String but none how to do it with an existing one.

                A Offline
                A Offline
                anti AS
                wrote on last edited by
                #7

                what about

                St = gcnew String(str);

                or

                using namespace System::Runtime::InteropServices;
                ...
                ...
                St = Marshal::PtrToStringAnsi((IntPtr)str);

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups