I am trying to obtain the HTML source from the current instance of Internet Explorer to a VB.NET app.
I have working unmanaged C++ code (with Win32 calls) in VS.NET 2003 that does obtain the current instance of IE as a CComQIPtr<IWebBrowser2> and can extract its source HTML. So, I'm trying to get that string to my VB.NET app. I'm OK on VB.NET but weak on C++.
I'm aware of that there are reasons not to just pass a string from C++ to VB.NET. Unless someone has a better approach (I doubt I want to write a wrapper class as the C++ is intricate, and I tried passsing the WebBrowser object, but it won't marshal, no layout, perhaps the HTML doc would?), I'm passing in a StringBuilder argument to the C++ function as a BSTR to allow it to set the "preallocated" string.
I am successfully calling the C++ function, as I get a valid boolean return value, but the content of the string is unchanged. I have the 'Use Managed Extensions' as Yes, and copy the C++ DLL to system32. I suspect I need to label the argument as in & out, and/or designate the argument as a pointer of some type. I can supply full source code if needed. I'm aware that with this approach for a large webpage I will need to allocate a large StringBuilder space, but that seems like the least of my worries.
Like I said, I'm open to other easier approaches (Temp file? - really?), but not embedding the browser in VB.NET - I've done that, but want all the native IE features: favorites, history, etc.
- Rand W.
<DllImport("Automation.dll")> Public Shared Function GetAString(ByRef PassString As StringBuilder) As Boolean ' NOTE: I have tried ByVal as well
Dim sStr = New StringBuilder("PREINPUT", 1024)
bResult = GetAString(sStr) ' Get a good True result
TextBox1.Text = sStr.ToString
C++ Code: ( i have a .DEF to list GetAString as an entry point)
class CAutomationDlg : public CDialog
public: BOOL __stdcall CAutomationDlg::GetAString(BSTR PassString);
// RW ADDED: for Main DLL entry point, Needed ??
BOOL APIENTRY DllMain( HANDLE hModule,
LPVOID lpReserved )
// ... unmanaged code ....
BOOL __stdcall CAutomationDlg::GetAString(BSTR PassString)
PassString = SysAllocString (L"This is the test string."); // If S, I get an error C2664: cannot convert from System::String__gc to const OLECHAR *