VC++開發(fā)的ActiveX如何加入安全機(jī)制,避免IE中提示“在此頁上的ActiveX控件和本頁上的其他部分的交互可能不安全,你想允許這種交互嗎?”
轉(zhuǎn)自http://www.cnblogs.com/zdxster/archive/2011/01/27/1945868.html
在EOS6的項目中,如果采用VC++開發(fā)的ActiveX,那么第一次運(yùn)行的時候,IE中就會提示,“在此頁上的ActiveX控件和本頁上的其他部分的交互可能不安全,你想允許這種交互嗎?”在網(wǎng)上找了很多資料,原理介紹的多,但是真正如何做,介紹的比較少,因此這里把實際的步驟一步一步的記錄下來了,供大家參考。
1.1 去除ActiveX訪問時的安全提示
當(dāng)ActiveX第一次被訪問時,會出現(xiàn)如下提示框:

這是IE瀏覽器的安全機(jī)制造成的,我們可以采用下面的步驟來去除這個提示信息:
1.1.1 在CDemoCtl的頭文件.h中增加對objsave的引用
#include <objsafe.h>
1.1.2 在其protected聲明區(qū)增加如下內(nèi)容:
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

2

3

4

5

6

7

8

9

10

11

12

13

1.1.3 在CDemoCtl的實現(xiàn)類.cpp的IMPLEMENT_DYNCREATE(CActivexFirstCtrl, COleControl)這一行后增加如下內(nèi)容:
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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90


91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204


205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

posted on 2011-07-04 11:33 厚積薄發(fā) 閱讀(4219) 評論(1) 編輯 收藏 引用 所屬分類: Windows編程