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

简介:

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全部要换成你的控件的类名

目录
相关文章
|
C语言 Python
PyQt5-如何设置应用和窗口的图标?控件的提示信息如何设置?
PyQt5-如何设置应用和窗口的图标?控件的提示信息如何设置?
58 1
|
6月前
[MFC] A对话框调用B对话框
[MFC] A对话框调用B对话框
47 0
|
C++ Windows
C++ Windows窗口程序:子窗口控件之按钮类button
C++ Windows窗口程序:子窗口控件之按钮类button
876 0
PyQt5 技术篇-鼠标移动控件显示提示,Qt Designer控件提示设置方法。
PyQt5 技术篇-鼠标移动控件显示提示,Qt Designer控件提示设置方法。
1043 0
PyQt5 技术篇-鼠标移动控件显示提示,Qt Designer控件提示设置方法。
|
测试技术
MFC中为菜单或按钮添加快捷键功能
1、新建一快捷键资源,ACCELERATOR,关联相应的ID号,下图所示中,其中,第一个ID为自定义快捷键ID,按CTRL+R,此时响应该ID以应的消息响应函数, 第二个ID为菜单ID,此时按CTRL+V,调出ID_NETWORK_PING菜单对应的响应函数。
1376 0
|
安全
OCX控件避免弹出安全警告的类
1.要加一个头文件:         #include 2.在控件头文件中加入: 1 DECLARE_INTERFACE_MAP()2 BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)3 STDMETHOD(GetInter...
916 0
MFC弹出对话框几种方法总结
一、点击菜单按钮弹出对话框 1、在资源视图中(.rc文件夹下)找到menu,选择需要弹框的菜单项; 2、点击菜单项,修改菜单ID,在主程序中用ON_COMMAND()将ID和函数关联,要在主程序的头文件中声明被关联函数; ...
3721 0
VC轻松实现CTreeCtrl控件双击响应
这个陌生的指针类型常使初用树控件的编程者陷入迷茫之中。实际上我们完全不理会它,也可以实现树控件的双击响应,即在OnDblClkTree( )函数中调用树控件的成员函数CTreeCtrl::GetSelectedItem( )以获取树控件的子项句柄HTREEITEM,得到此句柄后,我们便可用下列成员函...
1382 0