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.
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.
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.
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.
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.