Implementing ActiveX Control for web browser

2019-09-21 01:42发布

问题:

My requirement is to develop a ActiveX control which can send the byte stream to serial port. The ActiveX control should expose only one function like

SendData(char* data, int nLen, int nPort)

This function should be able to be invoked by pressing a button developed in java rendered in Internet Explorer(both 32 and 64 bit). I am confused whether i should go with windowless ActiveX control using MFC/ATL or Browser Helper Objects. In future i may need to support Firefox also.

Any help will be appreciated.

回答1:

Although BHO would work, ActiveX will suite better your requirements. BHO are very similar to ActiveX - they are both native or managed code running in the browser window. The difference is that BHO is available for all pages (it is instantiated when the browser tab is created) while ActiveX which is not a BHO is instantiated when some Javascript code in the page creating it (or with the <object> tag. The life time of a ActiveX is the page.

ATL is a better choice. Use the VS Wizard, and create an ATL based COM in-proc server. Then add a COM object. Since you want to call it from HTML make sure it is a dual interface (i.e. derived from IDispatch). Add the method that you want to the IDL file. In COM, the calling convention for string arguments is BSTR, and the standard convention for array is SAFEARRAY. How do you plan calling the method from your Javascript code?

Depends how you want to instantiate your object from the HTML page. You can have an <object classid='clsid:'class guid'> or you can use Javascript: var myobj = new ActiveXObject(ProgId). Both methods are valid.