troubleshooting Question

Getting A String from unmanaged C++ to VB.NET

Avatar of Randwro
Randwro asked on
Visual C++.NET
7 Comments1 Solution797 ViewsLast Modified:
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.

VB.NET Code:
   <DllImport("Automation.dll")> Public Shared Function GetAString(ByRef PassString As StringBuilder) As Boolean '  NOTE: I have tried ByVal as well
    End Function

Sub GetDataFromIE()

        Dim sStr = New StringBuilder("PREINPUT", 1024)
        bResult = GetAString(sStr) ' Get a good True result
        TextBox1.Text = sStr.ToString
End Sub

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 ??
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved                               )
    return TRUE;

// ... unmanaged code ....

#using <mscorlib.dll>
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 *
return TRUE;

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros