2014년 10월 29일 수요일

HWND to IHTMLDocument AND IWebBrowser2

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;
};

BOOL  SetRealNameCheckSilent(HWND hWnd)
{
BOOL bResult = FALSE;
if( hWnd == NULL )
return bResult;

CoInitialize( NULL );

// Explicitly load MSAA so we know if it's installed
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
if( hInst == NULL )
return bResult;

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, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
 //spDoc is IHTMLDocument2
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
if ( SUCCEEDED(hr) )
{
// Change background color to red
IOleCommandTarget *pCmdTarget = NULL;
spDoc->QueryInterface( IID_IOleCommandTarget, ( LPVOID* )&pCmdTarget );
if( pCmdTarget != NULL )
{
IServiceProvider *pSp;
HRESULT hr = pCmdTarget->QueryInterface( IID_IServiceProvider, ( LPVOID* )&pSp );
if( SUCCEEDED( hr ) )
{
IWebBrowser2 *pWebBrowser;
//pWebBrowser is IWebBrowser2;
hr = pSp->QueryService( SID_SWebBrowserApp, IID_IWebBrowser2, ( LPVOID* )&pWebBrowser );
if( SUCCEEDED( hr ) )
{
bResult = TRUE;
pWebBrowser->put_Silent( VARIANT_TRUE );
pWebBrowser->put_AddressBar( VARIANT_FALSE );
pWebBrowser->put_Resizable( VARIANT_FALSE );
pWebBrowser->put_ToolBar( VARIANT_FALSE );
}
}
}
}
}
} // else document not ready
} // else Internet Explorer is not running
::FreeLibrary( hInst );
} // else Active Accessibility is not installed
CoUninitialize();

return bResult;
}

댓글 없음: