控件避免弹出安全警告的类

来源:互联网 发布:游戏书籍 知乎 编辑:程序博客网 时间:2024/06/05 16:00

开始的时候写了一个ActiveX控件,这个ActiveX是有Setup.exe的,刚开始的时候只是在本地写这个ActiveX控件,这时候使用的时候本地打开IE窗口,只是点黄框,激活ActiveX就可以使用了。

后来在www方式下打开我的页面,问题就来了,我的控件被屏蔽了。IE6还可以通过降低安全性来Enable这个ActiveX控件,IE7却是无论如何也搞不定。让我那是极其郁闷啊。

在网上一顿狂找,发现了很多信息,这些信息也是让我走了很多弯路。。。。比如有人说一定要购买数字证书,有人说一定要降低安全性,有的说要手动到Add-on Management里面去修改ActiveX的属性。

还找到说IE7默认就不可能打开ActiveX,ActiveX是Option-in的,总之让我走了太多弯路。

终于找到了一篇文章http://caoyuke.blogdriver.com/caoyuke/434528.html, 那就是在控件内实现IObjectSafety接口。

具体如下(替换MyCtrl为你自己的class name):  


控件的头文件要#include <objsafe.h>

//在控件头文件中加入:

 DECLARE_INTERFACE_MAP()

 BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
  STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
  STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
 END_INTERFACE_PART(ObjectSafety)

//在控件的CPP文件中加入:

BEGIN_INTERFACE_MAP(MyCtrl, COleControl)
  INTERFACE_PART(MyCtrl, IID_IObjectSafety, ObjectSafety)
END_INTERFACE_MAP()
// Implementation of IObjectSafety
STDMETHODIMP MyCtrl::XObjectSafety::GetInterfaceSafetyOptions(
   REFIID riid,
   DWORD __RPC_FAR *pdwSupportedOptions,
   DWORD __RPC_FAR *pdwEnabledOptions)
{
 METHOD_PROLOGUE_EX(MyCtrl, ObjectSafety)

 if (!pdwSupportedOptions || !pdwEnabledOptions)
 {
  return E_POINTER;
 }

 *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
 *pdwEnabledOptions = 0;

 if (NULL == pThis->GetInterface(&riid))
 {
  TRACE("Requested interface is not supported./n");
  return E_NOINTERFACE;
 }

 // What interface is being checked out anyhow?
 OLECHAR szGUID[39];
 int i = StringFromGUID2(riid, szGUID, 39);

 if (riid == IID_IDispatch)
 {
  // Client wants to know if object is safe for scripting
  *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
  return S_OK;
 }
 else if (riid == IID_IPersistPropertyBag
    || riid == IID_IPersistStreamInit
    || riid == IID_IPersistStorage
    || riid == IID_IPersistMemory)
 {
  // Those are the persistence interfaces COleControl derived controls support
  // as indicated in AFXCTL.H
  // Client wants to know if object is safe for initializing from persistent data
  *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
  return S_OK;
 }
 else
 {
  // Find out what interface this is, and decide what options to enable
  TRACE("We didn't account for the safety of this interface, and it's one we support.../n");
  return E_NOINTERFACE;
 } 
}

STDMETHODIMP MyCtrl::XObjectSafety::SetInterfaceSafetyOptions(
  REFIID riid,
  DWORD dwOptionSetMask,
  DWORD dwEnabledOptions)
{
 METHOD_PROLOGUE_EX(MyCtrl, ObjectSafety)

 OLECHAR szGUID[39];
 // What is this interface anyway?
 // We can do a quick lookup in the registry under HKEY_CLASSES_ROOT/Interface
 int i = StringFromGUID2(riid, szGUID, 39);

 if (0 == dwOptionSetMask && 0 == dwEnabledOptions)
 {
  // the control certainly supports NO requests through the specified interface
  // so it's safe to return S_OK even if the interface isn't supported.
  return S_OK;
 }

 // Do we support the specified interface?
 if (NULL == pThis->GetInterface(&riid))
 {
  TRACE1("%s is not support./n", szGUID);
  return E_FAIL;
 }


 if (riid == IID_IDispatch)
 {
  TRACE("Client asking if it's safe to call through IDispatch./n");
  TRACE("In other words, is the control safe for scripting?/n");
  if (INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwEnabledOptions)
  {
   return S_OK;
  }
  else
  {
   return E_FAIL;
  }
 }
 else if (riid == IID_IPersistPropertyBag
    || riid == IID_IPersistStreamInit
    || riid == IID_IPersistStorage
    || riid == IID_IPersistMemory)
 {
  TRACE("Client asking if it's safe to call through IPersist*./n");
  TRACE("In other words, is the control safe for initializing from persistent data?/n");

  if (INTERFACESAFE_FOR_UNTRUSTED_DATA == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_DATA == dwEnabledOptions)
  {
   return NOERROR;
  }
  else
  {
   return E_FAIL;
  }
 }
 else
 {
  TRACE1("We didn't account for the safety of %s, and it's one we support.../n", szGUID);
  return E_FAIL;
 }
}

STDMETHODIMP_(ULONG) MyCtrl::XObjectSafety::AddRef()
{
 METHOD_PROLOGUE_EX_(MyCtrl, ObjectSafety)
 return (ULONG)pThis->ExternalAddRef();
}

STDMETHODIMP_(ULONG) MyCtrl::XObjectSafety::Release()
{
 METHOD_PROLOGUE_EX_(MyCtrl, ObjectSafety)
 return (ULONG)pThis->ExternalRelease();
}

STDMETHODIMP MyCtrl::XObjectSafety::QueryInterface(
 REFIID iid, LPVOID* ppvObj)
{
 METHOD_PROLOGUE_EX_(MyCtrl, ObjectSafety)
 return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}