Hi,
I have main thread that creates an WebBrowser2 COM object. and i want to invoke JScript functions on it from another thread.
i try to use GIT but still doesn't work for me..
there is a problem with marshal WebBrowser2 for JScript?
Printable View
Hi,
I have main thread that creates an WebBrowser2 COM object. and i want to invoke JScript functions on it from another thread.
i try to use GIT but still doesn't work for me..
there is a problem with marshal WebBrowser2 for JScript?
CODE:
MAIN THREAD
{...
ATL::CComPtr<IWebBrowser2> m_spWebBrowser;
m_spWebBrowser.CoCreateInstance(__uuidof(WebBrowser), NULL);
DispEventAdvise((IUnknown*)m_spWebBrowser)
///
IUnknown* pIUnknown = NULL;
IGlobalInterfaceTable* pIGlobalInterfaceTable = NULL;
::CoCreateInstance
(
CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&pIGlobalInterfaceTable
);
if (pIGlobalInterfaceTable)
{
/* QI the original interface pointer for its IUnknown interface. */
m_spWebBrowser -> QueryInterface (IID_IUnknown, (void**)&pIUnknown);
if (pIUnknown)
{
/* Register this interface pointer in GIT. */
/* A cookie, identifying the interface pointer*/
/* is returned. */
/* No need to call pIUnknown -> AddRef(). */
/* Another thread can retrieve the pIUnknown */
/* using the cookie. */
pIGlobalInterfaceTable -> RegisterInterfaceInGlobal
(
pIUnknown,
__uuidof(IWebBrowser2Ptr),
&m_dwCookie
);
pIUnknown -> Release();
pIUnknown = NULL;
}
WORKER THREAD:
DWORD dwCookie = (DWORD)lpvParameter;
IWebBrowser2* pISimpleCOMObject2 = NULL;
IGlobalInterfaceTable* pIGlobalInterfaceTable = NULL;
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr)) return; //Needs to add error log here.
CoCreateInstance
(
CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&pIGlobalInterfaceTable
);
if (pIGlobalInterfaceTable)
{
pIGlobalInterfaceTable -> GetInterfaceFromGlobal
(
dwCookie,
__uuidof(IWebBrowser2Ptr),
(void**)&(pThis->m_spWebBrowserCtrl)
);
}
What doesn't work?
WORKER THREAD:
When i'm tring:
ATL::CComPtr<IDispatch>& spDisp_;
IWebBrowser2* m_spWebBrowserCtrl;
spDisp_ = NULL;
MSHTML::IHTMLDocument2Ptr spDoc = NULL;
HRESULT hr = m_spWebBrowserCtrl->get_Document((IDispatch**)&spDoc);
/////!!!! HERE hr = FAIL!
if (FAILED(hr) || (NULL == spDoc)) {
return false;
}
hr = spDoc->get_Script(&spDisp_);
at the main thread it works.