简体   繁体   中英

How to get a reference to an object (COM) in JavaScript that can be passed outside the browser

I have a hyrid type application (web and forms). It's a .net compact framework app. On one of the forms I have a WebBrowser control.

I want to communicate between the WebBrowser control and the form that host/contains the WebBrowser control.

To do this I plan to create an Activex (COM) object in C++ compiled for the windows mobile device.

I plan to use JavaScript to create an instance of the ActiveX control on the web page that is displayed in the WebBrowser control.

How can I get a reference to this ActiveX control that I can then send to the form?


My objective is to send a reference of the ActiveX control instance to the windows mobile form that contains the WebBrowser control so that both the web page and form can use/access the same instance of the ActiveX control.

I created a way to send strings from the ActiveX control to the form. Is there a way to convert a reference of the ActiveX control to a string then pass the string to the form and re-create a reference to the object instance on the form side?

I hope this makes sense.

You can get an IDispatch reference to the window using something like this:

CComPtr<IWebBrowser2> m_webBrowser(/* create, assign, whatever to get the pointer */
CComQIPtr<IHTMLWindow2> m_htmlWin;
CComPtr<IDispatch> m_htmlDocDisp;
CComQIPtr<IDispatch> m_htmlWindDisp;

m_webBrowser->get_Document(&m_htmlDocDisp);
CComQIPtr<IHTMLDocument2> doc(m_htmlDocDisp);
assert(doc);
doc->get_parentWindow(&m_htmlWin);
assert(m_htmlWin);

m_htmlWindDisp = m_htmlWin;
assert(m_htmlWin);

Once you have that, you can use IDispatch methods to either query the value of a property on the window object or you can set the value of such a property. For example, if you create an IDispatch object that exposes methods and properties then you use the m_htmlWindDisp object to Invoke with PROPERTYPUTREF that object as "foo" then you could access that object from javascript using "window.foo". Alternatley, using Invoke with PROPERTYGET you can get the IDispatch handle for an object that you set on window, such as "window.foo = someFooBaredObject"

Hope that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM