1.要加一个头文件:
#include <objsafe.h>
2.在控件头文件中加入:
1 DECLARE_INTERFACE_MAP()
2 BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
3 STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
4 STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
5 END_INTERFACE_PART(ObjectSafety)
3.在控件的CPP文件中加入:
1 BEGIN_INTERFACE_MAP(CVP2PCtrl, COleControl)
2 INTERFACE_PART(CVP2PCtrl, IID_IObjectSafety, ObjectSafety)
3 END_INTERFACE_MAP()
4 // Implementation of IObjectSafety
5 STDMETHODIMP CVP2PCtrl::XObjectSafety::GetInterfaceSafetyOptions(
6 REFIID riid,
7 DWORD __RPC_FAR *pdwSupportedOptions,
8 DWORD __RPC_FAR *pdwEnabledOptions)
9 {
10 METHOD_PROLOGUE_EX(CVP2PCtrl, ObjectSafety)
11
12 if (!pdwSupportedOptions || !pdwEnabledOptions)
13 {
14 return E_POINTER;
15 }
16
17 *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
18 *pdwEnabledOptions = 0;
19
20 if (NULL == pThis->GetInterface(&riid))
21 {
22 TRACE("Requested interface is not supported.\n");
23 return E_NOINTERFACE;
24 }
25
26 // What interface is being checked out anyhow?
27 OLECHAR szGUID[39];
28 int i = StringFromGUID2(riid, szGUID, 39);
29
30 if (riid == IID_IDispatch)
31 {
32 // Client wants to know if object is safe for scripting
33 *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
34 return S_OK;
35 }
36 else if (riid == IID_IPersistPropertyBag
37 || riid == IID_IPersistStreamInit
38 || riid == IID_IPersistStorage
39 || riid == IID_IPersistMemory)
40 {
41 // Those are the persistence interfaces COleControl derived controls support
42 // as indicated in AFXCTL.H
43 // Client wants to know if object is safe for initializing from persistent data
44 *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
45 return S_OK;
46 }
47 else
48 {
49 // Find out what interface this is, and decide what options to enable
50 TRACE("We didn't account for the safety of this interface, and it's one we support...\n");
51 return E_NOINTERFACE;
52 }
53 }
54
55 STDMETHODIMP CVP2PCtrl::XObjectSafety::SetInterfaceSafetyOptions(
56 REFIID riid,
57 DWORD dwOptionSetMask,
58 DWORD dwEnabledOptions)
59 {
60 METHOD_PROLOGUE_EX(CVP2PCtrl, ObjectSafety)
61
62 OLECHAR szGUID[39];
63 // What is this interface anyway?
64 // We can do a quick lookup in the registry under HKEY_CLASSES_ROOT\Interface
65 int i = StringFromGUID2(riid, szGUID, 39);
66
67 if (0 == dwOptionSetMask && 0 == dwEnabledOptions)
68 {
69 // the control certainly supports NO requests through the specified interface
70 // so it's safe to return S_OK even if the interface isn't supported.
71 return S_OK;
72 }
73
74 // Do we support the specified interface?
75 if (NULL == pThis->GetInterface(&riid))
76 {
77 TRACE1("%s is not support.\n", szGUID);
78 return E_FAIL;
79 }
80
81 if (riid == IID_IDispatch)
82 {
83 TRACE("Client asking if it's safe to call through IDispatch.\n");
84 TRACE("In other words, is the control safe for scripting?\n");
85 if (INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwEnabledOptions)
86 {
87 return S_OK;
88 }
89 else
90 {
91 return E_FAIL;
92 }
93 }
94 else if (riid == IID_IPersistPropertyBag
95 || riid == IID_IPersistStreamInit
96 || riid == IID_IPersistStorage
97 || riid == IID_IPersistMemory)
98 {
99 TRACE("Client asking if it's safe to call through IPersist*.\n");
100 TRACE("In other words, is the control safe for initializing from persistent data?\n");
101
102 if (INTERFACESAFE_FOR_UNTRUSTED_DATA == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_DATA == dwEnabledOptions)
103 {
104 return NOERROR;
105 }
106 else
107 {
108 return E_FAIL;
109 }
110 }
111 else
112 {
113 TRACE1("We didn't account for the safety of %s, and it's one we support...\n", szGUID);
114 return E_FAIL;
115 }
116 }
117
118 STDMETHODIMP_(ULONG) CVP2PCtrl::XObjectSafety::AddRef()
119 {
120 METHOD_PROLOGUE_EX_(CVP2PCtrl, ObjectSafety)
121 return (ULONG)pThis->ExternalAddRef();
122 }
123
124 STDMETHODIMP_(ULONG) CVP2PCtrl::XObjectSafety::Release()
125 {
126 METHOD_PROLOGUE_EX_(CVP2PCtrl, ObjectSafety)
127 return (ULONG)pThis->ExternalRelease();
128 }
//OK!不会再弹出那个“与ActiveX控件交互不安全“的对话框了~~~
//其中CVP2PCtrl全部要换成你的控件的类名