实现对IE而言的安全控件
面向的读者:如果你编写了自己的COM控件,在IE中使用的时候总是被 “不安全的控件” 这样的提示困扰,你就请往下看,如何改造你的控件成为“安全控件”。
1. 包含头文件#include <atlctl.h> 或是 Objsafe.h
2. 为控件接口类增加父类 IobjectSafety
例如:
class ATL_NO_VTABLE CiCAEnrollAssist :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CiCAEnrollAssist, &CLSID_iCAEnrollAssist>,
public IObjectSafety, //为你的接口类添加此行
public IDispatchImpl<IiCAEnrollAssist, &IID_IiCAEnrollAssist, &LIBID_ICAENROLLLib>
{
…….
}
3. 为控件接口类增加COM映射 COM_INTERFACE_ENTRY(IObjectSafety)
BEGIN_COM_MAP(CiCAEnrollAssist)
COM_INTERFACE_ENTRY(IiCAEnrollAssist)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IObjectSafety) //为你的接口类添加此行
END_COM_MAP()
4. 为控件接口类添加方法
GetInterfaceSafetyOptions和SetInterfaceSafetyOptions
将下面代码拷贝到你的接口类定义中即可
别忘了为接口类增加成员变量DWORD m_dwSafety
STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid,DWORD __RPC_FAR *pdwSupportedOptions,
DWORD __RPC_FAR *pdwEnabledOptions)
{
if (riid == IID_IDispatch) {
//Client wants to know
// if object is safe for scripting. Only indicate safe for
// scripting when the interface is safe.
*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
*pdwEnabledOptions = m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER;
return S_OK;
}else if ((riid == IID_IPersistStreamInit) ||
(riid == IID_IPersistStorage)) {
// Client wants to know if object is
// safe for initializing. Only indicate safe for initializing
// when the interface is safe.
*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
*pdwEnabledOptions = m_dwSafety &
INTERFACESAFE_FOR_UNTRUSTED_DATA;
return S_OK;
}else{
// We are saying that no other interfaces in this control are
// safe for initializing or scripting.
*pdwSupportedOptions = 0;
*pdwEnabledOptions = 0;
return E_FAIL;
}
}
STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid,DWORD dwOptionSetMask,
DWORD dwEnabledOptions)
{
// Store our current safety level to return in
// GetInterfaceSafetyOptions
m_dwSafety |= dwEnabledOptions & dwOptionSetMask;
if ((riid == IID_IDispatch) &&
(m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER)) {
// Client wants us to disable any functionality that would
// make the control unsafe for scripting. The same applies to
// any other IDispatch or Dual interfaces your control may
// support. Because our control is safe for scripting by
// default we just return S_OK.
return S_OK;
}else if (((riid == IID_IPersistStreamInit) ||
(riid == IID_IPersistStorage)) &&
(m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_DATA)) {
// Client wants us to make the control safe for initializing
// from persistent data. For these interfaces, this control
// is safe so we return S_OK.
return E_FAIL;
}else{
// This control doesn't allow Initialization or Scripting
// from any other interfaces so return E_FAIL.
return E_FAIL;
}
}

浙公网安备 33010602011771号