Tuesday, January 15, 2008

Using WebKit on Windows

Since I research WebKit, I try writing how to use WebKit in your application.

WebKit has COM interface, so you need use it via COM interface. This way is same as Trident (MSHTML Web Browser Control) of Internet Explorer.

Initialize

At first, you get IWebView interface using CoCreateInstance(). Then, when you call IWebView::initWithFrame() with window handle of parent window, so you can create WebKit window.

IWebView *pWebView;
HRESULT hr;

hr = ::CoCreateInstance(CLSID_WebView,
                        0,
                        CLSCTX_ALL,
                        IID_IWebView,
                        (void**)&pWebView);
if(hr != S_OK)
    goto error;

hr = pWebView->setHostWindow((OLE_HANDLE) hWnd);
if(hr != S_OK)
    goto error;

::GetClientRect(hWnd,&clientRect);
hr = pWebView->initWithFrame(clientRect, NULL, NULL);
if(hr != S_OK)
    goto error;

Loading URL...

You cannot use takeStringURL like MacOS X sample. This method will return E_NOTIMPL.

If you want to use WebKit on Windows, you get IWebURLRequest object via CoCreateInstance(), then, you call IWebFrame::loadRequest(). If you want to open HTML stirng, you can use other method of IWebView.

HRESULT hr;
BSTR str;
IWebFrame* pWebFrame;
IWebURLRequest * pWebURLRequest;
IWebView* pWebView;

hr = ::CoCreateInstance(CLSID_WebURLRequest,
                        0,
                        CLSCTX_ALL,
                        IID_IWebURLRequest,
                        (void**)&pWebURLRequest);
if(hr != S_OK)
    goto error;

str = ::SysAllocString(L"http://www.apple.com/");
hr = pWebURLRequest->initWithURL(str,
                                 WebURLRequestUseProtocolCachePolicy,
                                 60);
if(hr != S_OK)
    goto error;

hr = pWebView->mainFrame(&pWebFrame);
if(hr != S_OK)
    goto error;

hr = pWebFrame->loadRequest(pWebURLRequest);
if(hr != S_OK)
    goto error;

pWebURLRequest->Release();
pWebFrame->Release();

1 comment:

A.V.Ebrahimi said...

Hi:

I am hardly looking for a way to use WebKit api in windows, I need to layout HTML to WPF, did you have any success in using WebKit in Windows?

Thanks and regards
A.V.Ebrahimi
vakilzadeh at gmail.com