VS2013 ocx去除安全警告

首先在自己的控件头文件中加入#include <ObjSafe.h>

并在头文件中加入

下面的全部要加

         DECLARE_INTERFACE_MAP()

BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
END_INTERFACE_PART(ObjectSafety)

// 构造函数

public:
CCTD_DNP_OCXCtrl();


// 重写
public:
virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid);
virtual void DoPropExchange(CPropExchange* pPX);
virtual void OnResetState();


// 实现
protected:
~CCTD_DNP_OCXCtrl();
DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
END_INTERFACE_PART(ObjectSafety)
DECLARE_OLECREATE_EX(CCTD_DNP_OCXCtrl)    // 类工厂和 guid
DECLARE_OLETYPELIB(CCTD_DNP_OCXCtrl)      // GetTypeInfo
DECLARE_PROPPAGEIDS(CCTD_DNP_OCXCtrl)     // 属性页 ID
DECLARE_OLECTLTYPE(CCTD_DNP_OCXCtrl) // 类型名称和杂项状态


// 消息映射
DECLARE_MESSAGE_MAP()


// 调度映射

DECLARE_DISPATCH_MAP()

然后在控件.cpp中加入

IMPLEMENT_DYNCREATE(CCTD_DNP_OCXCtrl, COleControl)


// 消息映射
//去掉安全警告 BEGIN
BEGIN_INTERFACE_MAP(CCTD_DNP_OCXCtrl, COleControl)
INTERFACE_PART(CCTD_DNP_OCXCtrl, IID_IObjectSafety, ObjectSafety)
END_INTERFACE_MAP()


// Implementation of IObjectSafety
STDMETHODIMP CCTD_DNP_OCXCtrl::XObjectSafety::GetInterfaceSafetyOptions(
REFIID riid,
DWORD __RPC_FAR *pdwSupportedOptions,
DWORD __RPC_FAR *pdwEnabledOptions)
{
METHOD_PROLOGUE_EX(CCTD_DNP_OCXCtrl, ObjectSafety)
if (!pdwSupportedOptions || !pdwEnabledOptions)
{
return E_POINTER;
}
*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
*pdwEnabledOptions = 0;
if (NULL == pThis->GetInterface(&riid))
{
TRACE("Requested interface is not supported.\n");
return E_NOINTERFACE;
}
// What interface is being checked out anyhow?
OLECHAR szGUID[39];
int i = StringFromGUID2(riid, szGUID, 39);
if (riid == IID_IDispatch)
{
// Client wants to know if object is safe for scripting
*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
return S_OK;
}
else if (riid == IID_IPersistPropertyBag
|| riid == IID_IPersistStreamInit
|| riid == IID_IPersistStorage
|| riid == IID_IPersistMemory)
{
// Those are the persistence interfaces COleControl derived controls support
// as indicated in AFXCTL.H
// Client wants to know if object is safe for initializing from persistent data
*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
return S_OK;
}
else
{
// Find out what interface this is, and decide what options to enable
TRACE("We didn't account for the safety of this interface, and it's one we support...\n");
return E_NOINTERFACE;
}
}


STDMETHODIMP CCTD_DNP_OCXCtrl::XObjectSafety::SetInterfaceSafetyOptions(
REFIID riid,
DWORD dwOptionSetMask,
DWORD dwEnabledOptions)
{
METHOD_PROLOGUE_EX(CCTD_DNP_OCXCtrl, ObjectSafety)
OLECHAR szGUID[39];
// What is this interface anyway?
// We can do a quick lookup in the registry under HKEY_CLASSES_ROOT\Interface
int i = StringFromGUID2(riid, szGUID, 39);
if (0 == dwOptionSetMask && 0 == dwEnabledOptions)
{
// the control certainly supports NO requests through the specified interface
// so it"s safe to return S_OK even if the interface isn"t supported.
return S_OK;
}
// Do we support the specified interface?
if (NULL == pThis->GetInterface(&riid))
{
TRACE1("%s is not support.\n", szGUID);
return E_FAIL;
}
if (riid == IID_IDispatch)
{
TRACE("Client asking if it's safe to call through IDispatch.\n");
TRACE("In other words, is the control safe for scripting?\n");
if (INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwEnabledOptions)
{
return S_OK;
}
else
{
return E_FAIL;
}
}
else if (riid == IID_IPersistPropertyBag
|| riid == IID_IPersistStreamInit
|| riid == IID_IPersistStorage
|| riid == IID_IPersistMemory)
{
TRACE("Client asking if it's safe to call through IPersist*.\n");
TRACE("In other words, is the control safe for initializing from persistent data?\n");
if (INTERFACESAFE_FOR_UNTRUSTED_DATA == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_DATA == dwEnabledOptions)
{
return NOERROR;
}
else
{
return E_FAIL;
}
}
else
{
TRACE1("We didn't account for the safety of %s, and it's one we support...\n", szGUID);
return E_FAIL;
}
}
STDMETHODIMP_(ULONG) CCTD_DNP_OCXCtrl::XObjectSafety::AddRef()
{
METHOD_PROLOGUE_EX_(CCTD_DNP_OCXCtrl, ObjectSafety)
return (ULONG)pThis->ExternalAddRef();
}
STDMETHODIMP_(ULONG) CCTD_DNP_OCXCtrl::XObjectSafety::Release()
{
METHOD_PROLOGUE_EX_(CCTD_DNP_OCXCtrl, ObjectSafety)
return (ULONG)pThis->ExternalRelease();
}
STDMETHODIMP CCTD_DNP_OCXCtrl::XObjectSafety::QueryInterface(REFIID iid, LPVOID* ppvObj)
{
METHOD_PROLOGUE_EX_(CCTD_DNP_OCXCtrl, ObjectSafety)
return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}
//去掉安全警告 END
BEGIN_MESSAGE_MAP(CCTD_DNP_OCXCtrl, COleControl)
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


// 调度映射


并把控件类名改为自己的类名

猜你喜欢

转载自blog.csdn.net/xmmdbk/article/details/80318120
今日推荐