Calling C++ methods from javascript
Hi,
I've implemented a web browser using the activeX control.
In previous topic, I had a problem with handling keys events like tab,
delete,enter which were solved using IOleInPlaceActiveObject.
this was my previous post
http://groups.google.com/group/microsoft.public.platformsdk.com_ole/browse_thread/thread/25462b1798e61b62/5c13de4a638c3d61#5c13de4a638c3d61
Now, I need to call C++ methods using javascript window.external. I
have a class that inherits IDocHostUIHandlerDispatch, and i override
the methods GetIDsOfNames() and Invoke(), GetExternal().
i use the TranslateAccelerator() to handle events.
My problem is this:
I can't seem to make both things work together - the keys event and the
external call.
When I add the support for the IDocHostUIHandler, the keys events are
not invoked and vice versa.
Do i need to override the translateAccelerator method of this class?
How do i combine it in the main loop the reads/dispatches messages?
tnx a lot
here is some code:
In the code given bellow, i always get an exception in the
pIOIPAO->TranslateAccelerator(msg) call.
//////////////////////////////////////
//this is the initialization of the browser
void BrowserWindow::initBrowserWindow(){
// window is HWND
window = CreateWindow(windowClassName,
"Loading...",
WS_OVERLAPPEDWINDOW,
(GetSystemMetrics(SM_CXSCREEN)-400)/2,
(GetSystemMetrics(SM_CYSCREEN)-400)/2,
400,
400,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(window,SW_SHOW);
RECT windowRect;
GetClientRect(window, &windowRect) ;
// axwin is CAxWindow
axwin.Create(window, windowRect , "res://",
WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL,0,201) ;
// unknownPointer is IUnknown*
unknownPointer = NULL ;
HRESULT hr = AtlAxGetControl(axwin.m_hWnd, &unknownPointer);
// spBrowser is IWebBrowser2*
// get an interface of the web browser control.
unknownPointer->QueryInterface(IID_IWebBrowser2,
(void**)&spBrowser);
spBrowser->put_Visible(VARIANT_TRUE);
doc = NULL;// doc is IHTMLDocument2*
dispatch = NULL;// dispatch is IDispatch*
pIOIPAO = NULL;// pIOIPAO is IOleInPlaceActiveObject*
//THandler inherits IDocHostUIHandlerDispatch
handler = new THandler(this);
handler->AddRef();
axwin.SetExternalUIHandler(handler);
// Create the event handler. TWatcher extends IDispatch
watcher = new TWatcher(this) ;
watcher->AddRef() ;
AtlAdvise(spBrowser, watcher, DIID_DWebBrowserEvents2, &cookie)
;
}
//////////////////////////////////////
from WinMain():
......
MSG msg ;
HRESULT hr = S_FALSE;
while(GetMessage(&msg, NULL , NULL, NULL) != 0) {
TranslateMessage(&msg);
HWND parent = msg.hwnd ;
// search up until the top is reached
while(GetParent(parent)){
parent = GetParent(parent) ;
}
// All the browser windows are held in a map, the key is the HWND
BrowserWindow* bw = BrowserWindow::getByHwnd(parent);
if (bw != NULL) {
hr = bw->handleMessage(&msg);
}
// if S_OK was returned, the message was already handled.
// this is to prevent the keys events to happen twice
if (hr != S_OK) {
DispatchMessage(&msg);
}
}
.....
//////////////////////////////////////
void BrowserWindow::handleMessage(MSG* msg) {
InitDoc();// document may not exist when the message arrives
if (doc!=NULL){
initIOIPAO();
if (pIOIPAO!= NULL) {// pIOIPAO is a cached
IOleInPlaceActiveObject*
HRESULT hr =
pIOIPAO->TranslateAccelerator(msg);
}
}
}
//////////////////////////////////////////////////////////////////////
/*
cache the html document.
*/
IHTMLDocument2* BrowserWindow::InitDoc(){
if (this->doc==NULL){
HRESULT hr = spBrowser->get_Document(&dispatch);//
dispatch is an
IDispatch*
if (!SUCCEEDED(hr) || dispatch==NULL) {
return NULL;
}
dispatch->QueryInterface(IID_IHTMLDocument2,(void**)&(this->doc));
dispatch->Release();
}
return this->doc;
}
//////////////////////////////////////////////////////////////////////
/*
cache the IOleInPlaceActiveObject.
*/
void BrowserWindow::initIOIPAO() {
InitDoc();// doc may not be initialized yet
if (doc!=NULL){
doc->QueryInterface(IID_IOleInPlaceActiveObject,
(void**)&pIOIPAO);
}