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
3
DECLARE_INTERFACE_MAP()
4
5
BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
6
7
STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
8
9
STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
10
11
END_INTERFACE_PART(ObjectSafety)
12
13
//去掉安全警告 END
//去掉安全警告 BEGIN2

3
DECLARE_INTERFACE_MAP()4

5
BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)6

7
STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);8

9
STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);10

11
END_INTERFACE_PART(ObjectSafety)12

13
//去掉安全警告 END
1.1.3 在CDemoCtl的實現類.cpp的IMPLEMENT_DYNCREATE(CActivexFirstCtrl, COleControl)這一行后增加如下內容:
1
//去掉安全警告 BEGIN
2
BEGIN_INTERFACE_MAP(CAudioCommunicationCtrl, COleControl)
3
4
INTERFACE_PART(CAudioCommunicationCtrl, IID_IObjectSafety, ObjectSafety)
5
6
END_INTERFACE_MAP()
7
8
// Implementation of IObjectSafety
9
10
STDMETHODIMP 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
98
STDMETHODIMP 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
212
STDMETHODIMP_(ULONG) CAudioCommunicationCtrl::XObjectSafety::AddRef()
213
214
{
215
216
METHOD_PROLOGUE_EX_(CAudioCommunicationCtrl, ObjectSafety)
217
218
return (ULONG)pThis->ExternalAddRef();
219
220
}
221
222
STDMETHODIMP_(ULONG) CAudioCommunicationCtrl::XObjectSafety::Release()
223
224
{
225
226
METHOD_PROLOGUE_EX_(CAudioCommunicationCtrl, ObjectSafety)
227
228
return (ULONG)pThis->ExternalRelease();
229
230
}
231
232
STDMETHODIMP 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
//去掉安全警告 BEGIN2
BEGIN_INTERFACE_MAP(CAudioCommunicationCtrl, COleControl)3

4
INTERFACE_PART(CAudioCommunicationCtrl, IID_IObjectSafety, ObjectSafety)5

6
END_INTERFACE_MAP()7

8
// Implementation of IObjectSafety9

10
STDMETHODIMP 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 scripting55

56
*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;57

58
return S_OK;59

60
}61

62
else if (riid == IID_IPersistPropertyBag63

64
|| riid == IID_IPersistStreamInit65

66
|| riid == IID_IPersistStorage67

68
|| riid == IID_IPersistMemory)69

70
{71

72
// Those are the persistence interfaces COleControl derived controls support73

74
// as indicated in AFXCTL.H75

76
// Client wants to know if object is safe for initializing from persistent data77

78
*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;79

80
return S_OK;81

82
}83

84
else85

86
{87

88
// Find out what interface this is, and decide what options to enable89

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

98
STDMETHODIMP 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\Interface115

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 interface123

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
else159

160
{161

162
return E_FAIL;163

164
}165

166
}167

168
else if (riid == IID_IPersistPropertyBag169

170
|| riid == IID_IPersistStreamInit171

172
|| riid == IID_IPersistStorage173

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
else191

192
{193

194
return E_FAIL;195

196
}197

198
}199

200
else201

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

212
STDMETHODIMP_(ULONG) CAudioCommunicationCtrl::XObjectSafety::AddRef()213

214
{215

216
METHOD_PROLOGUE_EX_(CAudioCommunicationCtrl, ObjectSafety)217

218
return (ULONG)pThis->ExternalAddRef();219

220
}221

222
STDMETHODIMP_(ULONG) CAudioCommunicationCtrl::XObjectSafety::Release()223

224
{225

226
METHOD_PROLOGUE_EX_(CAudioCommunicationCtrl, ObjectSafety)227

228
return (ULONG)pThis->ExternalRelease();229

230
}231

232
STDMETHODIMP 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
//去掉安全警告 ENDposted on 2011-07-04 11:33 厚積薄發 閱讀(4239) 評論(1) 編輯 收藏 引用 所屬分類: Windows編程


