CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 1999
    Posts
    12

    Getting HTML Source Code To CString

    Does anybody have an idea of how to retrieve HTML Source Code from Microsoft WebBrowser control in VC++ 6.0 to CString.

    Thanks
    Svenni Bono.


  2. #2
    Join Date
    Apr 1999
    Posts
    40

    Re: Getting HTML Source Code To CString

    with this- it downloads the html file, and puts it in the CString name

    this came from the VC++ documentation

    void CWebBrowseView::OnViewSource()
    {
    HANDLE heMail;
    DWORD dwWritten;

    CString url = GetLocationURL();
    HINTERNET hSession;
    hSession = InternetOpen("llornkcor browser", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if (hSession)
    {
    HINTERNET hService;
    hService = InternetOpenUrl(hSession, url, NULL,
    0, 0, 0);
    if (hService)
    {
    DWORD dwBytesAvailable, dwBytesRead;
    InternetQueryDataAvailable(hService, &dwBytesAvailable, 0, 0);
    char *lpBuffer = (char*)malloc(dwBytesAvailable+1);
    InternetReadFile(hService, lpBuffer, dwBytesAvailable, &dwBytesRead);
    lpBuffer[dwBytesRead]=NULL;
    CString name = lpBuffer;
    try
    {
    heMail = CreateFile ("C:\\llornkcor\\tmp.txt",
    GENERIC_WRITE,
    FILE_SHARE_READ,
    NULL,
    CREATE_NEW,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

    WriteFile (heMail,
    name,
    strlen(name),
    &dwWritten, NULL);
    CloseHandle(heMail);
    }
    catch(...)
    {
    }


    ShellExecute(m_hWnd,
    "open",
    "C:\\llornkcor\\tmp.txt",
    NULL,
    NULL,
    SW_SHOWNORMAL);

    // AfxMessageBox(lpBuffer);
    free(lpBuffer);
    }
    }




    Lorn "ljp" Potter
    Trolltech Qtopia Community Liaison
    irc.freenode.net #qtopia, #qt

  3. #3
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    418

    Re: Getting HTML Source Code To CString

    Hi Svenni,

    if you have hosted the WebBrowser control query the interface IHTMLDocument2, call the toString function, and convert the resulting BSTR to a CString. You can do it by casting it to _bstr_t object and use the extractor char*.


    Martin

  4. #4
    Guest

    Re: Doesn't work at my site ?!

    I tried this, but the BSTR returned contains "[object]" only.
    Any idea? Can you help one again?
    Peter


  5. #5
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    418

    Re: Doesn't work at my site ?!

    Hi Peter,

    Bono wrote private to me, with the same question. I was thinking too fast, sorry for that. Here's a solution for IE5:

    void CShowSourceView::OnShowSource()
    {
    IHTMLDocument2Ptr pDocument = GetHtmlDocument();
    IHTMLElementCollectionPtr pCollection;
    IDispatchPtr pDispatch;
    if ( ( S_OK == pDocument->get_all( &pCollection ) ) &&
    ( NULL != pDocument ) &&
    ( S_OK == pCollection->tags( _variant_t( "HTML" ), &pDispatch ) ) &&
    ( NULL != pDispatch ) )
    {
    IHTMLElementCollectionPtr pCollectionHTML = pDispatch;
    if ( pCollectionHTML != NULL )
    {
    IDispatchPtr pDispatchHTML;
    if ( S_OK == pCollectionHTML->item( _variant_t( 0L ), _variant_t(), &pDispatchHTML ) )
    {
    IHTMLElementPtr pHTML = pDispatchHTML;
    if ( NULL != pHTML )
    {
    BSTR bstrHTML = NULL;
    if ( S_OK == pHTML->get_outerHTML( &bstrHTML ) )
    {
    CString strHTML = bstrHTML;
    ::SysFreeString( bstrHTML );
    }
    }
    }
    }
    }
    }




    Martin

  6. #6
    Guest

    Re: And IE 4.x ?

    great you take care - but your code works not under IE4 (get_outerHTML returns E_FAIL, happens always if I try to get access to the HEAD tag).
    Do you have any idea to get the thing running under IE4 ? I don't want to require IE5 for the app.

    Perhaps you know another way - my original problem is: load a HTML, modify it through IHTMLxxxxx, and save the modified contents. However, if I use IPersistStreamInit, or IPersistFile, only the ORIGINAL source (without my modifications) is saved. (If the document is modified by some DHTML script, IPersist works fine)
    Similar behaviour with document->ExecWB

    Hope you can help

    Peter


  7. #7
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    418

    Re: And IE 4.x ?

    Hi Peter,

    as I wrote, it will work only under IE5. IE4 doesn't support get_outerHTML for the <HTML> tag.

    And sorry, I never worked with the IPersistxxx interfaces.

    Martin

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