yehao's Blog

VC++开发的ActiveX如何加入安全机制,避免IE中提示“在此页上的ActiveX控件和本页上的其他部分的交互可能不安全,你想允许这种交互吗?”

转自http://www.cnblogs.com/zdxster/archive/2011/01/27/1945868.html

在EOS6的项目中,如果采用VC++开发的ActiveX,那么第一次运行的时候,IE中就会提示,“在此页上的ActiveX控件和本页上的其他部分的交互可能不安全,你想允许这种交互吗?”在网上找了很多资料,原理介绍的多,但是真正如何做,介绍的比较少,因此这里把实际的步骤一步一步的记录下来了,供大家参考。

 

1.1 去除ActiveX访问时的安全提示

ActiveX第一次被访问时,会出现如下提示框:


 

这是IE浏览器的安全机制造成的,我们可以采用下面的步骤来去除这个提示信息:

1.1.1 CDemoCtl的头文件.h中增加对objsave的引用

#include <objsafe.h> 

1.1.2 在其protected声明区增加如下内容
 1//去掉安全警告 BEGIN
 2
 3DECLARE_INTERFACE_MAP()
 4
 5BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
 6
 7STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
 8
 9STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
10
11END_INTERFACE_PART(ObjectSafety)
12
13//去掉安全警告 END


1.1.3 
CDemoCtl的实现类.cppIMPLEMENT_DYNCREATE(CActivexFirstCtrl, COleControl)这一行后增加如下内容:
  1//去掉安全警告 BEGIN
  2BEGIN_INTERFACE_MAP(CAudioCommunicationCtrl, COleControl)
  3
  4    INTERFACE_PART(CAudioCommunicationCtrl, IID_IObjectSafety, ObjectSafety)
  5
  6END_INTERFACE_MAP()
  7
  8// Implementation of IObjectSafety
  9
 10STDMETHODIMP CAudioCommunicationCtrl::XObjectSafety::GetInterfaceSafetyOptions(
 11
 12    REFIID riid,
 13
 14    DWORD __RPC_FAR *pdwSupportedOptions,
 15
 16    DWORD __RPC_FAR *pdwEnabledOptions)
 17
 18{
 19
 20    METHOD_PROLOGUE_EX(CAudioCommunicationCtrl, ObjectSafety)
 21
 22        if (!pdwSupportedOptions || !pdwEnabledOptions)
 23
 24        {
 25
 26            return E_POINTER;
 27
 28        }

 29
 30        *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
 31
 32        *pdwEnabledOptions = 0;
 33
 34        if (NULL == pThis->GetInterface(&riid))
 35
 36        {
 37
 38            TRACE("Requested interface is not supported.\n");
 39
 40            return E_NOINTERFACE;
 41
 42        }

 43
 44        // What interface is being checked out anyhow?
 45
 46        OLECHAR szGUID[39];
 47
 48        int i = StringFromGUID2(riid, szGUID, 39);
 49
 50        if (riid == IID_IDispatch)
 51
 52        {
 53
 54            // Client wants to know if object is safe for scripting
 55
 56            *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
 57
 58            return S_OK;
 59
 60        }

 61
 62        else if (riid == IID_IPersistPropertyBag
 63
 64            || riid == IID_IPersistStreamInit
 65
 66            || riid == IID_IPersistStorage
 67
 68            || riid == IID_IPersistMemory)
 69
 70        {
 71
 72            // Those are the persistence interfaces COleControl derived controls support
 73
 74            // as indicated in AFXCTL.H
 75
 76            // Client wants to know if object is safe for initializing from persistent data
 77
 78            *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
 79
 80            return S_OK;
 81
 82        }

 83
 84        else
 85
 86        {
 87
 88            // Find out what interface this is, and decide what options to enable
 89
 90            TRACE("We didn't account for the safety of this interface, and it's one we support\n");
 91
 92            return E_NOINTERFACE;
 93
 94        }

 95
 96}

 97
 98STDMETHODIMP CAudioCommunicationCtrl::XObjectSafety::SetInterfaceSafetyOptions(
 99
100    REFIID riid,
101
102    DWORD dwOptionSetMask,
103
104    DWORD dwEnabledOptions)
105
106{
107
108    METHOD_PROLOGUE_EX(CAudioCommunicationCtrl, ObjectSafety)
109
110        OLECHAR szGUID[39];
111
112    // What is this interface anyway?
113
114    // We can do a quick lookup in the registry under HKEY_CLASSES_ROOT\Interface
115
116    int i = StringFromGUID2(riid, szGUID, 39);
117
118    if (0 == dwOptionSetMask && 0 == dwEnabledOptions)
119
120    {
121
122        // the control certainly supports NO requests through the specified interface
123
124        // so it"s safe to return S_OK even if the interface isn"t supported.
125
126        return S_OK;
127
128    }

129
130    // Do we support the specified interface?
131
132    if (NULL == pThis->GetInterface(&riid))
133
134    {
135
136        TRACE1("%s is not support.\n", szGUID);
137
138        return E_FAIL;
139
140    }

141
142    if (riid == IID_IDispatch)
143
144    {
145
146        TRACE("Client asking if it's safe to call through IDispatch.\n");
147
148            TRACE("In other words, is the control safe for scripting?\n");
149
150        if (INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwEnabledOptions)
151
152        {
153
154            return S_OK;
155
156        }

157
158        else
159
160        {
161
162            return E_FAIL;
163
164        }

165
166    }

167
168    else if (riid == IID_IPersistPropertyBag
169
170        || riid == IID_IPersistStreamInit
171
172        || riid == IID_IPersistStorage
173
174        || riid == IID_IPersistMemory)
175
176    {
177
178        TRACE("Client asking if it's safe to call through IPersist*.\n");
179
180            TRACE("In other words, is the control safe for initializing from persistent data?\n");
181
182        if (INTERFACESAFE_FOR_UNTRUSTED_DATA == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_DATA == dwEnabledOptions)
183
184        {
185
186            return NOERROR;
187
188        }

189
190        else
191
192        {
193
194            return E_FAIL;
195
196        }

197
198    }

199
200    else
201
202    {
203
204        TRACE1("We didn"t account for the safety of %s, and it"s one we support\n", szGUID);
205
206        return E_FAIL;
207
208    }

209
210}

211
212STDMETHODIMP_(ULONG) CAudioCommunicationCtrl::XObjectSafety::AddRef()
213
214{
215
216    METHOD_PROLOGUE_EX_(CAudioCommunicationCtrl, ObjectSafety)
217
218        return (ULONG)pThis->ExternalAddRef();
219
220}

221
222STDMETHODIMP_(ULONG) CAudioCommunicationCtrl::XObjectSafety::Release()
223
224{
225
226    METHOD_PROLOGUE_EX_(CAudioCommunicationCtrl, ObjectSafety)
227
228        return (ULONG)pThis->ExternalRelease();
229
230}

231
232STDMETHODIMP CAudioCommunicationCtrl::XObjectSafety::QueryInterface(
233
234    REFIID iid, LPVOID* ppvObj)
235
236{
237
238    METHOD_PROLOGUE_EX_(CAudioCommunicationCtrl, ObjectSafety)
239
240        return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
241
242}

243
244//去掉安全警告 END


posted on 2011-07-04 11:33 厚积薄发 阅读(4149) 评论(1)  编辑 收藏 引用 所属分类: Windows编程

评论

# re: VC++开发的ActiveX如何加入安全机制,避免IE中提示“在此页上的ActiveX控件和本页上的其他部分的交互可能不安全,你想允许这种交互吗?” 2012-09-17 22:46 王小亮

不错的 。。

正是 想要 的  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿

随笔分类

文章分类

文章档案

搜索

最新评论