COM Event to HTML at IE

When we use COM to implement some function, we may use event to sink to other COM object to process the event happen. If we use VB or VC to receive the event, it has no problem to get the event. But when we use COM object at IE, we found it is very difficulty to receive the event from the COM object when we open the html and trigger the event source. Why?

There has two problem, first we get the IE environment, which is not an development environment and not an language like VB or VC. second, we want use javascript to receive the event. So the code often we implement the event source, we have not found at IE.

If we want event found at IE, we should implement IProvideClassInfo2 interface at COM object. At ATL, we could use IProvideClassInfo2Impl to implement and add two COM_ENTRY_INTERFACE macro between BEGIN_COM_MAP and END_COM_MAP:

COM_INTERFACE_ENTRY(IProvideClassInfo)
COM_INTERFACE_ENTRY(IProvideClassInfo2)

so if we have implement the event we could found the following sentence round at above sentence.

COM_INTERFACE_ENTRY(IConnectionPointContainer)
COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)

so the some COM code is :

const CComBSTR bstrDefaultMsg = _T(“Hello, Internet Explorer!”);

/////////////////////////////////////////////////////////////////////////////
// CAtlCtrl
class ATL_NO_VTABLE CAtlCtrl :
public CComObjectRootEx,
public IDispatchImpl,
public CComControl,
public IPersistStreamInitImpl,
public IOleControlImpl,
public IOleObjectImpl,
public IOleInPlaceActiveObjectImpl,
public IViewObjectExImpl,
public IOleInPlaceObjectWindowlessImpl,
public IConnectionPointContainerImpl,
public CComCoClass,
public CProxy_IAtlCtrlEvents< CAtlCtrl >,
public IProvideClassInfo2Impl<&CLSID_AtlCtrl, &DIID__IAtlCtrlEvents, &LIBID_ATLCONTROLLib>,
public IObjectSafetyImpl,
public IPersistPropertyBagImpl

{
public:
CAtlCtrl()
: m_bstrMessage( bstrDefaultMsg )
, m_fDirty( FALSE )
{
}

DECLARE_REGISTRY_RESOURCEID(IDR_ATLCTRL)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CAtlCtrl)
COM_INTERFACE_ENTRY(IAtlCtrl)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IViewObjectEx)
COM_INTERFACE_ENTRY(IViewObject2)
COM_INTERFACE_ENTRY(IViewObject)
COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
COM_INTERFACE_ENTRY(IOleInPlaceObject)
COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
COM_INTERFACE_ENTRY(IOleControl)
COM_INTERFACE_ENTRY(IOleObject)
COM_INTERFACE_ENTRY(IPersistStreamInit)
COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)
COM_INTERFACE_ENTRY(IProvideClassInfo)
COM_INTERFACE_ENTRY(IProvideClassInfo2)
COM_INTERFACE_ENTRY(IObjectSafety)
COM_INTERFACE_ENTRY(IPersistPropertyBag)
END_COM_MAP()

BEGIN_PROP_MAP(CAtlCtrl)
PROP_DATA_ENTRY(“_cx”, m_sizeExtent.cx, VT_UI4)
PROP_DATA_ENTRY(“_cy”, m_sizeExtent.cy, VT_UI4)
// Example entries
// PROP_ENTRY(“Property Description”, dispid, clsid)
// PROP_PAGE(CLSID_StockColorPage)
END_PROP_MAP()

BEGIN_CONNECTION_POINT_MAP(CAtlCtrl)
CONNECTION_POINT_ENTRY(DIID__IAtlCtrlEvents)
END_CONNECTION_POINT_MAP()

BEGIN_MSG_MAP(CAtlCtrl)
CHAIN_MSG_MAP(CComControl)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
// Handler prototypes:
// LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
// LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
// LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

// IViewObjectEx
DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)

at IE, we could use JavaScript as the following code

<script for="AtlCtrl" language="JavaScript" event="OnMessageChanged">
	alert(  AtlCtrl.Message );
</script>

AtlCtrl is the object id which is the COM object identify at object tag.

抱歉,之前写的一个blog,用的是英文,还是基于ActiveX的内容,对于自己写的ActiveX做事件处理还是有些用处的。

/*
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */
原文地址:https://www.cnblogs.com/ubunoon/p/ATL_Active_Event.html