none
Invalid Window Handle Error

    Question

  • I'm trying to get a Web Browser Control using an ATL Dialog Box. Tried using the method shown on

    http://www.codeproject.com/KB/wtl/wtl4mfc6.aspx

    Here's the code producing the error:
    wndPlaceholder.GetWindowRect ( rc );
    ScreenToClient ( rc );
    wndPlaceholder.DestroyWindow();

    Here's the entire code:
    WNDCLASSEX wc;
    		HINSTANCE hInstance = _Module.GetResourceInstance();
    
    		CWindow wndPlaceholder = GetDlgItem ( IDC_IE_PLACEHOLDER );
    		CRect rc;
    		CAxWindow wndIE;
    		wndPlaceholder.GetWindowRect ( rc );
    		ScreenToClient ( rc );
    		wndPlaceholder.DestroyWindow();
    		CComPtr<IUnknown> punkCtrl;
            CComQIPtr<IWebBrowser2> pWB2;
            CComVariant v;
            // Create the AX host window.
            wndIE.Create ( *this, rc, _T(""), WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN );
            // Create the browser control using its GUID.
            wndIE.CreateControlEx ( L"{8856F961-340A-11D0-A96B-00C04FD705A2}", NULL,
                                    NULL, &punkCtrl );
            /*
            NOTE: You could also use the control's ProgID: Shell.Explorer:
            wndIE.CreateControlEx ( L"Shell.Explorer", NULL,
                                    NULL, &punkCtrl );
            */
            // Get an IWebBrowser2 interface on the control and navigate to a page.
            pWB2 = punkCtrl;
            if ( pWB2 )
                pWB2->Navigate ( CComBSTR("about:mozilla"), &v, &v, &v, &v );
    
    
    		wc.cbSize		 = sizeof(WNDCLASSEX);
    		wc.style		 = CS_HREDRAW | CS_VREDRAW;
    		//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+1);
    		wc.lpszMenuName  = NULL;
    		wc.lpszClassName = g_szClassName;
    		wc.hIconSm	 = NULL;
    
    		RegisterClassEx(&wc);
    
    		HWND hDialog;
    		InitCommonControls();
    
    		hDialog = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_BROWSER), 0, (DLGPROC)DlgProc);
    
    		::ShowWindow(hDialog, SW_SHOW);
    		::UpdateWindow(hDialog);

    Any help is appreciated, thanks!
    Monday, August 17, 2009 9:48 PM

Answers

  • You are providing so little information that I suspect you are not using the debugger.  Run a debug build in the debugger and step through the code.  Before calling GetDlgItem make sure that the dialog has a non-NULL m_hWnd.  Find out what is returned by GetDlgItem.  If it returns NULL there is no such control on the dialog, or the control requires you to call InitCommonControls first.

    • Marked as answer by codehelper123 Wednesday, August 19, 2009 3:07 PM
    Tuesday, August 18, 2009 4:12 PM

All replies

  • Did GetDlgItem() succeed? I'm guessing it failed. If so, use GetLastError() to determine the cause.
    Monday, August 17, 2009 10:25 PM
  • Could you please update me the details,

    'error' means are you getting compilation error
      if YES please post the error
    if You are getting runtime errror ( exception)
       post the detail exception.


    Siva.
    Monday, August 17, 2009 10:50 PM
  • Sorry, its a runtime exception, and it occurs at this line:

    wndPlaceholder.GetWindowRect ( rc );

    and produces the "Invalid Window Handle" exception
    Tuesday, August 18, 2009 2:07 PM
  • You are providing so little information that I suspect you are not using the debugger.  Run a debug build in the debugger and step through the code.  Before calling GetDlgItem make sure that the dialog has a non-NULL m_hWnd.  Find out what is returned by GetDlgItem.  If it returns NULL there is no such control on the dialog, or the control requires you to call InitCommonControls first.

    • Marked as answer by codehelper123 Wednesday, August 19, 2009 3:07 PM
    Tuesday, August 18, 2009 4:12 PM
  • You were correct, it is returning NULL and producing

    Control id not found

    Any ideas on how to fix this?
    Tuesday, August 18, 2009 7:34 PM
  • You need to place a control on your dialog box whose ID is IDC_IE_PLACEHOLDER.  That means after you place the control on the dialog template you edit its properties by typing IDC_IE_PLACEHOLDER into the ID field.
    Tuesday, August 18, 2009 7:51 PM
  • I've done that, is there anything else that could cause the problem? Thanks for the help.
    Tuesday, August 18, 2009 8:02 PM
  • I already described three possibilities.  Either the control ID is not there, or the GetDlgItem call occurs some place in the code before the controls have been created, or the control is one of the special ones that need InitCommonControls to be called first.  I suppose a 4th possibility would be a typo in the ID name or capitalization.

    If you like you could post the section of your .rc file that defines this dialog.




    Tuesday, August 18, 2009 8:45 PM
  • Here's the code snippet from the .rc file:

    IDD_BROWSER DIALOGEX 0, 0, 320, 200
    STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Dialog"
    FONT 8, "MS Shell Dlg", 0, 0, 0x0
    BEGIN
        DEFPUSHBUTTON   "OK",IDOK,209,179,50,14
        PUSHBUTTON      "Cancel",IDCANCEL,263,179,50,14
        GROUPBOX        "Place Holder",IDC_IE_PLACEHOLDER,7,7,306,152
    END
    


    Tuesday, August 18, 2009 9:16 PM
  • any ideas what's producing the problem?
    Wednesday, August 19, 2009 1:41 PM
  • Nope.  You will have to compare your code to the working code from codeproject and figure out what you missed.

     

    Wednesday, August 19, 2009 1:54 PM