Solved

Hosting web browser control in a separate thread.

Posted on 2002-07-30
15
826 Views
Last Modified: 2010-05-18
The following code creates a web browser
control in a thread separate from the main
UI thread.  When the window containing the
web browser control is closed, the following
error occurs:

"Unhandled exception in htmltester.exe: 0xC0000005: Access Violation."

This crash only seems to occur under Windows XP and
does not seem to occur when the control is hosted
in the main UI thread.

What am I doing wrong and how can I fix the
problem?

----------------------------------------------------
#include "stdafx.h"
#include "resource.h"
#define _ATL_APARTMENT_THREADED
#include <atlbase.h>
CComModule _Module;
#include <atlwin.h>

#define GENERIC_WINDOW_NAME                    "Generic Window"
#define HTML_WINDOW_NAME                    "HTML Window"
#define GENERIC_WINDOW_CLASS               "GenericWindowClass"
 
LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{    
     switch( uMsg )
     {
     case WM_CLOSE:
          {
               ::DestroyWindow( hwnd );
          }
          break;
     case WM_DESTROY:
          {
               PostQuitMessage(0);
          }
          break;
     default:
          {
               return ( DefWindowProc( hwnd, uMsg, wParam, lParam ) );
          }
     }
     return 0;
}

ATOM RegisterWindowClass( HINSTANCE hInstance )
{
     WNDCLASS wc;
     wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = ::LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = GENERIC_WINDOW_CLASS;      

     return RegisterClass(&wc);
}

DWORD WINAPI HTMLThreadProc( LPVOID lpParameter )
{
     // Get teh instance
     HINSTANCE hInstance  = (HINSTANCE) lpParameter;
     
     // Create the HTML Window
     HWND hWnd= CreateWindow( GENERIC_WINDOW_CLASS, HTML_WINDOW_NAME, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
     
     // Add ATL support
     //_Module.Init( 0, hInstance );
     AtlAxWinInit();
     
     // Add the HTML control
     RECT rtWindow;
     ::GetClientRect( hWnd, &rtWindow );
     CAxWindow ATLWnd;
     ATLWnd.Create( hWnd, rtWindow, _T("http://www.msn.com"), WS_VISIBLE | WS_CHILD );
     
     // Show the window
     ShowWindow( hWnd, SW_SHOW );
     UpdateWindow( hWnd );
     
     // Enter the mian message loop
     MSG msg;
     while( GetMessage( &msg, NULL, 0, 0 ) )
     {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
     }

     //_Module.Term( );
     return msg.wParam;
};

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
     // Register the window class
     RegisterWindowClass( hInstance );

     // Create the HTML Window
     DWORD dwThreadID;
     HANDLE hThread = ::CreateThread( NULL, 0, HTMLThreadProc, hInstance, 0, &dwThreadID );
         
     // Create the main thread window
     HWND hWnd= CreateWindow( GENERIC_WINDOW_CLASS, GENERIC_WINDOW_NAME, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
     
     // Show the window
     ShowWindow( hWnd, nCmdShow );
     UpdateWindow( hWnd );
     
     // Enter the mian message loop
     MSG msg;
     while(GetMessage( &msg, NULL, 0, 0 ) )
     {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
     }
     
     // Wait for the HTML thread to terminate
     ::WaitForSingleObject( hThread, INFINITE );
     ::CloseHandle( hThread );

     return 1;
}
----------------------------------------------------
0
Comment
Question by:azmajere
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 4

Expert Comment

by:Chizl
ID: 7188834
"Unhandled exception in htmltester.exe: 0xC0000005: Access Violation."

This means you created an instance of something without closing or doing a .release() on it.  I would look at your CreateWindow, ATLWnd.Create, CreateThread, and anything else your opening that you possibly are not releasing.
0
 

Author Comment

by:azmajere
ID: 7188847
All of the code is given above.  I have looked over the code a few times and can not seem to pinpoint where such an error might be occurring.
0
 
LVL 4

Expert Comment

by:Chizl
ID: 7188905
What about the:
//_Module.Init( 0, hInstance );
that you have..

Try:
_Module.Init( NULL, hInstance );
with the:
_Module.Term();


You may also want to try to put in a:
LRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL& ){
PostQuitMessage( 0 );
return 0;
}
0
 

Author Comment

by:azmajere
ID: 7188948
The _Module.Init and _Module.Term do seem to have any effect.  I guess technically that they should not be
commented out, I was just playing around trying to
get it work.

The "WM_DESTROY" message does appear to be handled
proper and is posting a "PostQuitMessage(0)".

0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7189551
No problem on my PC(W2k and XP) if I un-comment "_Module.Init" and "_Module.Term".
I think "_Module.Init" and "_Module.Term" shouldn't be commented.
Try to rebuild it and test it on another computer.
0
 

Author Comment

by:azmajere
ID: 7190909
Uncommented the "_Module.Init" and ""_Module.Term"
statements.

Ok, did the test on 5 systems (2 Windows 2000 and 3
Windows XP ).  Both the Windows 2000 systems worked fine.
Two of the Windows XP systems crashed upon closing the
the HTML window.  One Windows XP systems seemed to have
no problems at all.

Obviously there is something machine(s) related. Did
you have dev studio installed on your test machines?

But why would it only occur when the control is hosted
in a secondary thread?

As, far as you can tell the there are no bugs in the code, correct?  
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7192519
It seems there no bug in the code. The exception maybe caused by ATL's code. Which version of VC/ATL are you using?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 9

Expert Comment

by:BeyondWu
ID: 7192523
Have you debugged it on XP? Where it stopped when exception caused? Is it crashed at _Module.Term( )?
0
 

Author Comment

by:azmajere
ID: 7193524
I am using Visual C++ version 6.0.  I have the latest
service packs and Platform SDK installed.

When it crashed under Windows XP the call stack is as
follows:

4A008AC8( )
NTDLL! 77fabb68( )

....yeah didn't help me much either :-(
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7195023
Try to install the debug symbol then debug it again.
Which is the latest instruction before crash?Try to put a breakpoint on return msg.wParam; Can you reach the breakpoint before crash?

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7200030
Does it make any difference if you create the Browser thread-and-window after the app main window is up and running?  I think I've read about certain peculiarities related to window creation order and threading.  Recall that XP has do do some underhanded shennanigans vis-a-vis the screwball window frame colors and cartoon buttons and such.

-- Dan
0
 

Author Comment

by:azmajere
ID: 7209971
1. The html window exits the thread properly.  It seems
to crashing after the thread exits...perhaps in an ie
thread in the background.

2. It makes no difference if the html thread is created
after the main window is visible.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7212121
I don't have an XP system for testing, at it seems to have no problems on Win2000.

I commented out
       AtlAxWinInit();
and it ran OK.  The help said 'you don't always need to call this explicitly' and it seem at least possible that the problem is related to this.

Alternatively, you could try adding a call to
    AtlAxWinTerm( );
somewhere at the end.

=--==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
One other thing to try:  Shuffle the headers around a bit.  Perhaps placing

     #define _ATL_APARTMENT_THREADED
above
     #include "stdafx.h"
(and cleaning and rebuilding) might make a difference.

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
ID: 7439597
Dear azmajere

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to

     "refund the points and PAQ at zero points"

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 7479731
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now