Re: get instance of ie 7 from hwnd
thank for your advises
finnaly i found the problem
i used the code in http://support.microsoft.com/kb/q249232
but what i did wrong!!! was not to use the EnumChildProc() function
and for hWndChild i simply used ::FindWindow(L"IEFrame",NULL); agin
this is WRONG!!!
first find the explorer parent window by this
line ::FindWindow(L"IEFrame",NULL);
and I had use the line ::EnumChildWindows( hWnd, EnumChildProc,
(LPARAM)&hWndChild ); because it gives the HWND to "Internet
Explorer_Server" BUT one last thing I Had to change the line:
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0,
(void**)&spDoc );
to:
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0,
(void**)&spDoc ); The '2' is important
here is the code i use its a simple MFC program named "iecon" and the
code from CieconView class and chane the background coler to red when
left button clicked on the program:
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
TCHAR buf[100];
::GetClassName( hwnd, (LPTSTR)&buf, 100 );
if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 )
{
*(HWND*)lParam = hwnd;
return FALSE;
}
else
return TRUE;
};
void CieconView::OnLButtonDown(UINT nFlags, CPoint point)
{
CoInitialize( NULL );
HWND hWnd=::FindWindow(L"IEFrame",NULL);
// Explicitly load MSAA so we know if it's installed
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
if ( hInst != NULL )
{
if ( hWnd != NULL )
{
HWND hWndChild=NULL;
// Get 1st document window
::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );
if ( hWndChild )
{
CComPtr<IHTMLDocument2> spDoc;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG,
1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult =
(LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst,
("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0,
(void**)&spDoc );
if ( SUCCEEDED(hr) )
{
// Change background color to red
spDoc->put_bgColor( CComVariant(L"red") );
}
}
} // else document not ready
} // else Internet Explorer is not running
::FreeLibrary( hInst );
} // else Active Accessibility is not installed
CoUninitialize();
}
Hopes this helps anyone who encourted the problem