CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2011
    Posts
    45

    Converting std::string to System::String

    I am trying to write a Windows Form front end for thousands of lines of unmanaged C++ code. The existing code runs as a batch console application and its output is a series of Intranet HTML files.

    My plan is to call “Initialize()” as the form is created to create the unmanaged resident database in memory which will feed a series of combo boxes which will request the HTML file “string” to be displayed in a WebBrowser control on the form.

    I can set up the combo selections to call BuildPage() in a C++ file which creates the page in a std::string variable, and I can reference that string as extern in the form.h file, but I can’t seem to find the proper syntax to set the document text of the web browser control to the string.

    Here are a few of my commented out attempts to place the string in the web control. The last one ignores the string and does work when button 4 is pressed.


    Code:
    private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
    
    	 BuildPage();
    
    	 //StringBuilder ^ sb = gcnew StringBuilder( str.c_str() );
    	 //this->webBrowser1->DocumentText = sb -> ToString();
    
    	 //StringBuilder ^ sb = gcnew StringBuilder( "Transferred from char array" ) ;
    	 //sb->AppendFormat(" more {0} data",str);
    	 //sb->AppendFormat(" more {0} data",str.c_str());
    	 //this->webBrowser1->DocumentText = sb -> ToString();
    
    	 //this->webBrowser1->DocumentText = str.c_str();
    
    	 //this->webBrowser1->DocumentText = str;
    
    	 StringBuilder ^ sb = gcnew StringBuilder( "Transferred from char array" ) ;
    	 this->webBrowser1->DocumentText = sb -> ToString();
    
     }


    Can this even be done? If so, what is the syntax to push a std:string into a WebBrowser control?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Converting std::string to System::String

    You've been on the right track with std::string::c_str(), it's just that its result type (SByte * from the .NET perspective) isn't implicitly convertible to System::String. You need to explicitly construct one:

    Code:
      std::string strNative = "Test!";
      String ^strManaged = gcnew String(strNative.c_str());
    That doesn't work for StringBuilder, however, since that has no constructor that takes an argument of type SByte *. Therefore, you need an explicit intermediate conversion step:

    Code:
      StringBuilder ^sb = gcnew StringBuilder(gcnew String(strNative.c_str()));
    All that works reliably, as long as your source native string only contains characters from the standard ASCII range (which should be the case for valid HTML code). Otherwise you may need to use the String constructor that takes an Encoding object as a parameter. Alternatively, you may use Encoding::GetString(), but that takes an array<Byte> as its input instead of an SByte *, so, in the scenario you decribe, you'd need an extra conversion step (and this, I think, is exactly what the Encoding-based String c'tor does internaly).
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured