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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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?
Last edited on
I don't know anything about what you are doing PapaGeek and neither do I know anything about Windows Forms apps. What I do know however is that if this Web Browser Control is a COM object (which it likely is) then anything like that only understands BSTRs, which is what I'm assumming you mean by a 'System String'. A BSTR is actually pretty close to a null terminated wchar_t string, except that the string length (character count) is stored in the four (likely 8 bytes in x64) bytes preceeding the 1st character in the string. BSTRs are created by the OLE String Engine. Ck out SysAllocString(), SysFreeString() etc.
So if I'm right about what you are asking, and you need to feed BSTRs into this Web Browser Control, you'll need to create BSTRs. Kinda like so ...

std::wstring strMyString(L"Hello, World!");
BSTR strMyOleString=NULL;

strMyOleString=SysAllocString(strMyString.c_str());
....
....
SysFreeString(strMyOleString);

I don't use the C++ String Class at all (or anything else in the C++ Standard Library), so I don't have to deal with this. I just use null terminated strings and my own string class, but when feeding strings into COM objects you need to create BSTRs. And they need to be released when done or you'll have a memory leak.
Here is the code from a Win32 SDK Style app which loads this COM Object ...

 
const IID IID_WebExplorer  = {0xEAB22AC1,0x30C1,0x11CF,{0xA7,0xEB,0x00,0x00,0xC0,0x5B,0xAE,0x0B}};


...onto a GUI Form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
long fnWndProc_OnCommand(lpWndEventArgs Wea)
{
 switch(LOWORD(Wea->wParam))
 {
   case BTN_NAVIGATE:
     {
        TCHAR szBuffer[256];                                  // Allocate Working Buffer For Url
        HWND hEdit=GetDlgItem(Wea->hWnd,ID_URL);              // Get HWND of Edit Control Where User Types Url
        GetWindowText(hEdit,szBuffer,256);                    // Get Text Out Of Edit Control
        IWebBrowser* pWebBrowser=NULL;                        // Web Browser Control Created In fnWndProc_OnCreate()
        pWebBrowser=(IWebBrowser*)GetWindowLong(Wea->hWnd,8); // And Interface Pointer Stored in WNDCLASSEX.cbWndExtra Bytes
        BSTR strUrl;                                          // Declare BSTR And Allocate New Buffer Which Contains URL
        strUrl=SysAllocString(szBuffer);                      // Then Call Interface Member Function 'Navigate'.
        if(pWebBrowser)
           pWebBrowser->Navigate(strUrl,NULL,NULL,NULL,NULL);
        SysFreeString(strUrl);                                // Free BSTR
     }
     break;
   case ID_CLOSE:
     {

     }
     break;
 }

 return 0;
}


The form just has the Web Browser Control on it which fills most of the window, and a text box and button. When you click the button the code shown above executes and retrieves the text out of the text box and the Web Browser control navigates to the chosen web site. My whole app compiles to just 10 K with no other dependencies except to be running on Windows.
Thanks for the answers so far. From what I’m able to gather, BSTR is a visual basic string type, not C++, so I can’t get that to compile. As far as the GUI form, I’m trying to create the full HTML stream within my program and then display it as a web page. The example you mentioned merely navigates you to a URL.

BSTR is a visual basic string type, not C++,


No.

BSTRs are easier to use in Visual Basic because it was the native string type for that language. It is also the native string type for any COM object, and that is completely programming language agnostic. In C++, and by that I mean native 'unmanaged' C++, if you need to interact with COM objects in terms of strings, you need to use BSTRs. And that goes for both inputs and outputs.

I can't really speak to the Windows Forms or .NET languages - only native C/C++ apps.
Your question can easily be answered by looking at google:
https://msdn.microsoft.com/en-us/library/ms235219.aspx

So System::String has a constructor that takes const char* and std::string::c_str() provides it. Very simple.
Last edited on
Topic archived. No new replies allowed.