Embed Windows Internet Explorer

Andre andre at s-e-a-p.de
Mon Dec 16 10:44:05 PST 2013


Hi,

I try to embed Windows Internet Explorer into my application.
Most of the coding should be availabe. During method navigate2
I get an empty white screen. No errors is thrown but also the
web page is not loaded. Do you have some ideas?

=> Is use the windows headers from DSource and the dgui forms library.
I attached the source code.

Kind regards
André
-------------- next part --------------
module main;

import dgui.all;
import webbrowser;
import win32.ole2;

import std.stdio;

class MainForm: Form
{
	private Webbrowser w;

	public this()
	{
		this.text = "DGui Form";
		this.handleCreated.attach(&this.onHandleCreated);
		this.size = Size(500, 400);
		this.startPosition = FormStartPosition.CENTER_SCREEN;
	}

	private void onHandleCreated(Control sender, EventArgs e){
		HRESULT hr = win32.objbase.CoInitialize(null);  
		w = new Webbrowser(this.handle);
		int h = createBrowserObject(w);
		hr = navigate( w, "http://www.google.de" );
		//win32.ole2.OleUninitialize();
		//win32.objbase.CoUninitialize();
		this.resize.attach(&this.onResize);
	}

	private void onResize(Control sender, EventArgs e){
		w.setBrowserSize(this.width, this.height);
	}
}

int main()
{
	return Application.run(new MainForm());
}
-------------- next part --------------
module webbrowser;

import core.atomic;

import std.stdio;

private import win32.objfwd: LPMONIKER, IStream, IEnumSTATSTG;

private import win32.oleidl: 
	LPCRECT,
	LPOLECONTAINER,
	LPOLEINPLACEFRAMEINFO,
	LPCBORDERWIDTHS,
	LPOLEINPLACEACTIVEOBJECT,
	LPOLEMENUGROUPWIDTHS,
	HOLEMENU,
	IOleInPlaceFrame,
	IOleInPlaceUIWindow,
	IOleInPlaceActiveObject,
	IOleInPlaceObject,
	IOleClientSite,
	IOleWindow,
	IOleInPlaceSite,
	IDropTarget,
	IDataObject,
	IOleContainer,
	IOleObject;

private import win32.unknwn: IUnknown;
private import win32.basetyps: GUID, IID, CLSID, REFIID, REFCLSID;
private import win32.docobj: IOleCommandTarget;
private import win32.oaidl: IDispatch;
private import win32.wtypes: LPOLESTR, OLECHAR, LPCOLESTR;
private import win32.uuid;
private import win32.winerror;
private import win32.windef: SIZE, HRESULT, DWORD, BOOL, ULONG, POINT, LONG, LPRECT, HWND, HMENU, WORD, FALSE,TRUE, RECT;
private import win32.winuser: LPMSG, GetClientRect;
private import win32.objidl:IStorage, SNB, STATSTG;
private import win32.winbase:FILETIME;
private import win32.exdisp: IWebBrowser2;
private import win32.ole2: OleCreate, OleSetContainedObject, OLEIVERB_SHOW;
private import win32.winnt: LPCWSTR, PVOID;
private import win32.oaidl:VARIANT;
private import win32.oleauto:VariantInit, VariantClear, SysAllocString;
private import win32.wtypes: VARENUM, VARTYPE;

interface IDocHostUIHandler : IUnknown {
	HRESULT ShowContextMenu(DWORD, POINT* , IUnknown, IDispatch);
	HRESULT GetHostInfo( DOCHOSTUIINFO * pInfo );
	HRESULT ShowUI(DWORD, IOleInPlaceActiveObject, IOleCommandTarget, IOleInPlaceFrame, IOleInPlaceUIWindow);
	HRESULT HideUI();
	HRESULT UpdateUI();
	HRESULT EnableModeless(BOOL);
	HRESULT OnDocWindowActivate(BOOL);
	HRESULT OnFrameWindowActivate(BOOL);
	HRESULT ResizeBorder(LPCRECT, IOleInPlaceUIWindow , BOOL);
	HRESULT TranslateAccelerator(LPMSG, GUID* , DWORD);
	HRESULT GetOptionKeyPath(LPOLESTR*, DWORD);
	HRESULT GetDropTarget(IDropTarget, IDropTarget*);
	HRESULT GetExternal(IDispatch*);
	HRESULT TranslateUrl(DWORD, OLECHAR*, OLECHAR**);
	HRESULT FilterDataObject(IDataObject, IDataObject*);
}

enum tagDOCHOSTUIFLAG {
	DIALOG = 0x00000001,
	DISABLE_HELP_MENU = 0x00000002,
	NO3DBORDER = 0x00000004,
	SCROLL_NO = 0x00000008,
	DISABLE_SCRIPT_INACTIVE = 0x00000010,
	OPENNEWWIN = 0x00000020,
	DISABLE_OFFSCREEN = 0x00000040,
	FLAT_SCROLLBAR = 0x00000080,
	DIV_BLOCKDEFAULT = 0x00000100,
	ACTIVATE_CLIENTHIT_ONLY = 0x00000200,
	OVERRIDEBEHAVIORFACTORY = 0x00000400,
	CODEPAGELINKEDFONTS = 0x00000800,
	URL_ENCODING_DISABLE_UTF8 = 0x00001000,
	URL_ENCODING_ENABLE_UTF8 = 0x00002000,
	ENABLE_FORMS_AUTOCOMPLETE = 0x00004000,
	ENABLE_INPLACE_NAVIGATION = 0x00010000,
	IME_ENABLE_RECONVERSION = 0x00020000,
	THEME = 0x00040000,
	NOTHEME = 0x00080000,
	NOPICS = 0x00100000,
	NO3DOUTERBORDER = 0x00200000,
	DISABLE_EDIT_NS_FIXUP = 0x00400000,
	LOCAL_MACHINE_ACCESS_CHECK = 0x00800000,
	DISABLE_UNTRUSTEDPROTOCOL = 0x01000000,
	HOST_NAVIGATES = 0x02000000,
	ENABLE_REDIRECT_NOTIFICATION = 0x04000000,
	USE_WINDOWLESS_SELECTCONTROL = 0x08000000,
	USE_WINDOWED_SELECTCONTROL = 0x10000000,
	ENABLE_ACTIVEX_INACTIVATE_MODE = 0x20000000,
	DPI_AWARE = 0x40000000
} 
alias tagDOCHOSTUIFLAG* DOCHOSTUIFLAG;

enum tagDOCHOSTUIDBLCLK {
	DEFAULT = 0,
	SHOWPROPERTIES = 1,
	SHOWCODE = 2
}
alias tagDOCHOSTUIDBLCLK* DOCHOSTUIDBLCLK;

enum tagOLERENDER { 
	NONE    = 0,
	DRAW    = 1,
	FORMAT  = 2,
	ASIS    = 3
} 
alias tagOLERENDER* OLERENDER; //*LPOLERENDER;


struct DOCHOSTUIINFO
{
	ULONG cbSize;
	DWORD dwFlags;     
	DWORD dwDoubleClick;
}

class ComObject : IUnknown
{
	
	HRESULT QueryInterface(IID* riid, void** pvObject){
		if (*riid == IID_IUnknown)
		{
			*pvObject = cast(void*)cast(IUnknown)this;
			AddRef();
			return S_OK;
		}
		else
		{   *pvObject = null;
			return E_NOINTERFACE;
		}
	}
	
	ULONG AddRef()
	{
		return atomicOp!"+="(*cast(shared)&count, 1);
	}
	
	ULONG Release()
	{
		LONG lRef = atomicOp!"-="(*cast(shared)&count, 1);
		if (lRef == 0)
		{
			return 0;
		}
		return cast(ULONG)lRef;
	}
	LONG count = 0;             // object reference count
}



class Webbrowser: ComObject, IOleClientSite, IOleInPlaceSite, IOleInPlaceFrame, IDocHostUIHandler
{
	HWND mainWindow;
	IOleObject browserObject;
	
	this(HWND mainWindow){
		this.mainWindow = mainWindow;
	}
	
extern (Windows)
	override HRESULT QueryInterface(IID* riid, void** pvObject) {
		if ( *riid == IID_IUnknown )
		{
			*pvObject = cast(void*) cast(IUnknown) this;
			AddRef();
			return S_OK;
		} 
		else if ( *riid == IID_IOleClientSite ) {
			*pvObject = cast(void*) cast(IOleClientSite) this;
			AddRef();
			return S_OK;
		}
		else if ( *riid == IID_IOleInPlaceSite ) {
			*pvObject = cast(void*) cast(IOleInPlaceSite) this; 
			AddRef();
			return S_OK;
		}
		else if ( *riid == IID_IDocHostUIHandler ) {
			*pvObject = cast(void*) cast(IDocHostUIHandler) this;
			AddRef();
			return S_OK;
		}
		else if ( *riid == IID_IOleInPlaceFrame ) {
			*pvObject = cast(void*) cast(IOleInPlaceFrame) this;
			AddRef();
			return S_OK;
		}
		else if ( *riid == IID_IOleInPlaceUIWindow ) {
			*pvObject = cast(void*) cast(IOleInPlaceUIWindow) this;
			AddRef();
			return S_OK;
		}
		else if ( *riid == IID_IOleWindow ) {
			*pvObject = cast(void*) cast(IOleWindow) this;
			AddRef();
			return S_OK;
		} 
		else {
			*pvObject = null;
			return E_NOINTERFACE;
		}
	}
	
public:
	
	// IOleClientSite
	HRESULT SaveObject(){
		return E_NOTIMPL;
	}
	
	HRESULT GetMoniker(DWORD,DWORD,LPMONIKER*){
		return E_NOTIMPL;
	}
	
	HRESULT GetContainer(IOleContainer * ppContainer){
		*ppContainer = null;
		return E_NOINTERFACE;
	}
	
	HRESULT ShowObject(){
		return NOERROR;
	}
	
	HRESULT OnShowWindow(BOOL){
		return E_NOTIMPL;
	}
	
	HRESULT RequestNewObjectLayout(){
		return E_NOTIMPL;
	}
	
	// IOleInPlaceUIWindow
	HRESULT GetBorder(LPRECT){
		return E_NOTIMPL;
	}
	
	HRESULT RequestBorderSpace(LPCBORDERWIDTHS){
		return E_NOTIMPL;
	}
	
	HRESULT SetBorderSpace(LPCBORDERWIDTHS){
		return E_NOTIMPL;
	}
	
	HRESULT SetActiveObject(LPOLEINPLACEACTIVEOBJECT,LPCOLESTR){
		return S_OK;
	}
	
	// IOleWindow
	HRESULT GetWindow(HWND * phwnd){
		*phwnd = mainWindow;
		return S_OK;
	}
	
	HRESULT ContextSensitiveHelp(BOOL){
		return E_NOTIMPL;
	}
	
	// IOleInPlaceFrame
	HRESULT InsertMenus(HMENU,LPOLEMENUGROUPWIDTHS){
		return E_NOTIMPL;
	}
	
	HRESULT SetMenu(HMENU,HOLEMENU,HWND){
		return S_OK;
	}
	
	HRESULT RemoveMenus(HMENU){
		return E_NOTIMPL;
	}
	
	HRESULT SetStatusText(LPCOLESTR){
		return S_OK;
	}
	
	HRESULT EnableModeless(BOOL){
		return E_NOTIMPL;
	}
	
	HRESULT TranslateAccelerator(LPMSG,WORD){
		return E_NOTIMPL;
	}
	
	
	// IOleInPlaceSite
	HRESULT CanInPlaceActivate(){
		return S_OK;
	}
	
	HRESULT OnInPlaceActivate(){
		return S_OK;
	}
	
	HRESULT OnUIActivate(){
		return S_OK;
	}
	
	HRESULT GetWindowContext(IOleInPlaceFrame frame,IOleInPlaceUIWindow window,LPRECT posRect,LPRECT clipRect,LPOLEINPLACEFRAMEINFO poleInplaceFrameInfo) {
		frame =  cast(IOleInPlaceFrame)this;
		window = null;
		poleInplaceFrameInfo.fMDIApp = FALSE;
		poleInplaceFrameInfo.hwndFrame = mainWindow;
		poleInplaceFrameInfo.haccel = null;
		poleInplaceFrameInfo.cAccelEntries = 0;
		return S_OK;
	}
	
	HRESULT Scroll(SIZE){
		return E_NOTIMPL;
	}
	
	HRESULT OnUIDeactivate(BOOL){
		return S_OK;
	}
	
	HRESULT OnInPlaceDeactivate(){
		return S_OK;
	}
	
	HRESULT DiscardUndoState(){
		return E_NOTIMPL;
	}
	
	HRESULT DeactivateAndUndo(){
		return E_NOTIMPL;
	}
	
	HRESULT OnPosRectChange(LPCRECT lprcPosRect){
		IOleInPlaceObject	inplace;
		if ( S_OK == browserObject.QueryInterface(cast(GUID*) &IID_IOleInPlaceObject, cast(void**)&inplace) )
		{
			inplace.SetObjectRects( lprcPosRect, lprcPosRect );
		}
		return S_OK;
	}
	
	//IDocHostUIHandler
	HRESULT ShowContextMenu( DWORD dwID, POINT* ppt, IUnknown pcmdtReserved, IDispatch pdispReserved)
	{
		return S_OK;
	}
	
	HRESULT GetHostInfo( DOCHOSTUIINFO * pInfo )
	{
		pInfo.cbSize = DOCHOSTUIINFO.sizeof;
		pInfo.dwFlags = DOCHOSTUIFLAG.NO3DBORDER;
		pInfo.dwDoubleClick = DOCHOSTUIDBLCLK.DEFAULT;
		return S_OK;
	}
	
	HRESULT ShowUI( DWORD dwID, IOleInPlaceActiveObject pActiveObject, IOleCommandTarget pCommandTarget, IOleInPlaceFrame pFrame, IOleInPlaceUIWindow pDoc )
	{
		return S_OK;
	}
	
	HRESULT HideUI()
	{
		return S_OK;
	}
	
	HRESULT UpdateUI()
	{
		return S_OK;
	}
	
	HRESULT OnDocWindowActivate( BOOL fActivate )
	{
		return S_OK;
	}
	
	HRESULT OnFrameWindowActivate( BOOL fActivate )
	{
		return S_OK;
	}
	
	HRESULT ResizeBorder( LPCRECT prcBorder, IOleInPlaceUIWindow pUIWindow, BOOL fRameWindow )
	{
		return S_OK;
	}
	
	HRESULT TranslateAccelerator( LPMSG lpMsg, GUID * pguidCmdGroup, DWORD nCmdID )
	{
		return S_FALSE;
	}
	
	HRESULT GetOptionKeyPath( LPOLESTR * pchKey, DWORD dw )
	{
		return S_OK;
	}
	
	HRESULT GetDropTarget( IDropTarget pDropTarget, IDropTarget * ppDropTarget )
	{
		return S_FALSE;
	}
	
	HRESULT GetExternal( IDispatch * ppDispatch )
	{
		*ppDispatch = null;
		return S_FALSE;
	}
	
	HRESULT TranslateUrl( DWORD dwTranslate, OLECHAR * pchURLIn, OLECHAR ** ppchURLOut )
	{
		*ppchURLOut = cast(wchar*)"\0";
		return S_FALSE;
	}
	
	HRESULT FilterDataObject( IDataObject pDO, IDataObject * ppDORet )
	{
		*ppDORet = null;
		return S_FALSE;
	}
}


class FakeStorage : ComObject, IStorage {
	extern (Windows):
	override HRESULT QueryInterface(IID* riid, void** ppv)
	{
		return E_NOTIMPL;
	}
	
	HRESULT CreateStream(LPCWSTR, DWORD, DWORD, DWORD, IStream)
	{
		return E_NOTIMPL;
	}
	HRESULT OpenStream(LPCWSTR, PVOID, DWORD, DWORD, IStream)
	{
		return E_NOTIMPL;
	}
	HRESULT CreateStorage(LPCWSTR, DWORD, DWORD, DWORD, IStorage)
	{
		return E_NOTIMPL;
	}
	HRESULT OpenStorage(LPCWSTR, IStorage, DWORD, SNB, DWORD, IStorage)
	{
		return E_NOTIMPL;
	}
	HRESULT CopyTo(DWORD, IID* , SNB, IStorage)
	{
		return E_NOTIMPL;
	}
	HRESULT MoveElementTo( LPCWSTR, IStorage, LPCWSTR, DWORD)
	{
		return E_NOTIMPL;
	}
	HRESULT Commit(DWORD)
	{
		return E_NOTIMPL;
	}
	HRESULT Revert()
	{
		return E_NOTIMPL;
	}
	HRESULT EnumElements(DWORD, PVOID, DWORD, IEnumSTATSTG)
	{
		return E_NOTIMPL;
	}
	HRESULT DestroyElement(LPCWSTR)
	{
		return E_NOTIMPL;
	}
	HRESULT RenameElement(LPCWSTR, LPCWSTR)
	{
		return E_NOTIMPL;
	}
	HRESULT SetElementTimes(LPCWSTR, FILETIME* , FILETIME* , FILETIME* )
	{
		return E_NOTIMPL;
	}
	HRESULT SetClass(REFCLSID) {
		return S_OK;
	}
	
	HRESULT SetStateBits(DWORD, DWORD){
		return E_NOTIMPL;
	}
	
	HRESULT SetStatebools(DWORD, DWORD)
	{
		return E_NOTIMPL;
	}
	HRESULT Stat(STATSTG*, DWORD)
	{
		return E_NOTIMPL;
	}
	
}

int createBrowserObject( Webbrowser webbrowser )
{
	IWebBrowser2 webBrowser2;
	IStorage MyIStorage = cast(IStorage)(new FakeStorage());
	RECT rect;
	HRESULT rv;

	if (S_OK == (rv = OleCreate(cast(REFCLSID)&CLSID_WebBrowser,cast(REFIID)&IID_IOleObject,
	                            cast(uint)OLERENDER.DRAW,null,cast(IOleClientSite)webbrowser,MyIStorage, cast(void**)&(webbrowser.browserObject)))){
		webbrowser.browserObject.SetHostNames( "Test", null );
		GetClientRect( webbrowser.mainWindow, &rect );

		if ( (rv = OleSetContainedObject( webbrowser.browserObject, TRUE )) != S_OK )
		{
			return rv;
		}
		rv = -33333;
		if ( (rv = webbrowser.browserObject.DoVerb(OLEIVERB_SHOW,null,cast(IOleClientSite)webbrowser,	-1,	webbrowser.mainWindow,&rect)) != S_OK ) 
		{
			return rv;
		}
		if (( rv = webbrowser.browserObject.QueryInterface( cast(GUID*) &IID_IWebBrowser2, cast(void**)&webBrowser2)) != S_OK)
		{
			return rv;
		}else{
			webBrowser2.put_Left( 0);
			webBrowser2.put_Top( 0);
			webBrowser2.put_Width( rect.right);
			webBrowser2.put_Height( rect.bottom);
			webBrowser2.Release();
			return 0;
		}
	}
	return rv;
}

int navigate( Webbrowser b, string url )
{
	HWND hwnd = b.mainWindow;
	IWebBrowser2 webBrowser2;
	VARIANT myURL;
	IOleObject browserObject = b.browserObject;
	
	if ( S_OK == browserObject.QueryInterface(cast(GUID*) &IID_IWebBrowser2, cast(void**)&webBrowser2))
	{
		VariantInit(&myURL);
		myURL.vt = cast(VARTYPE)VARENUM.VT_BSTR;
		
		myURL.bstrVal = SysAllocString(cast(const(wchar*))url);

		if (!myURL.bstrVal)
		{
			webBrowser2.Release();
			return(-6);
		}
		webBrowser2.Navigate2( &myURL, null, null, null, null);
		VariantClear( &myURL );
		webBrowser2.Release();
		return(0);
	}
	return(-5);
}

void setBrowserSize( Webbrowser b, int width, int height ){
	IWebBrowser2 webBrowser2;
	IOleObject browserObject = b.browserObject;
	
	if ( S_OK == browserObject.QueryInterface(cast(GUID*) &IID_IWebBrowser2, cast(void**)&webBrowser2)) {
		webBrowser2.put_Width( width );
		webBrowser2.put_Height( height );
		webBrowser2.Release();
	}
}



More information about the Digitalmars-d-learn mailing list