/****************
// 在ActiveX的类声明文件XXXCtrl.h的头部中添加如下代码
#include <objsafe.h>
// 在ActiveX的类声明文件C..Ctrl.h的类声明内部添加如下代码
// 接口映射
DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(ObjSafe, IObjectSafety)
STDMETHOD_(HRESULT, GetInterfaceSafetyOptions) (
REFIID riid,
DWORD __RPC_FAR *pdwSupportedOptions,
DWORD __RPC_FAR *pdwEnabledOptions
);
STDMETHOD_(HRESULT, SetInterfaceSafetyOptions) (
REFIID riid,
DWORD dwOptionSetMask,
DWORD dwEnabledOptions
);
END_INTERFACE_PART(ObjSafe);
******************************************************************/
/**********************
// 在ActiveX的类实现文件XXXCtrl.cpp中添加如下代码,并将CXXXCtrl更换为你的ActiveX的类名称
// IObjectSafety的接口映射
BEGIN_INTERFACE_MAP( CXXXCtrl, COleControl )
INTERFACE_PART(CXXXCtrl, IID_IObjectSafety, ObjSafe)
END_INTERFACE_MAP()
/////////////////////////////////////////////////////////////////////////////
// IObjectSafety 成员函数
// AddRef, Release, QueryInterface
ULONG FAR EXPORT CXXXCtrl::XObjSafe::AddRef()
{
METHOD_PROLOGUE(CXXXCtrl, ObjSafe)
return pThis-> ExternalAddRef();
}
ULONG FAR EXPORT CXXXCtrl::XObjSafe::Release()
{
METHOD_PROLOGUE(CXXXCtrl, ObjSafe)
return pThis-> ExternalRelease();
}
HRESULT FAR EXPORT CXXXCtrl::XObjSafe::QueryInterface(
REFIID iid, void FAR* FAR* ppvObj)
{
METHOD_PROLOGUE(CXXXCtrl, ObjSafe)
return (HRESULT)pThis-> ExternalQueryInterface(&iid, ppvObj);
}
const DWORD dwSupportedBits =
INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;
/////////////////////////////////////////////////////////////////////////////
// CXXXCtrl::XObjSafe::GetInterfaceSafetyOptions
HRESULT STDMETHODCALLTYPE
CXXXCtrl::XObjSafe::GetInterfaceSafetyOptions(
REFIID riid,
DWORD __RPC_FAR *pdwSupportedOptions,
DWORD __RPC_FAR *pdwEnabledOptions
)
{
METHOD_PROLOGUE(CXXXCtrl, ObjSafe)
HRESULT retval = ResultFromScode(S_OK);
// 接口是否存在
IUnknown FAR* punkInterface;
retval = pThis-> ExternalQueryInterface(&riid, (void * *)&punkInterface);
if (retval != E_NOINTERFACE) // 接口存在
{
punkInterface-> Release(); // 释放引用
}
// We support both kinds of safety and have always both set,
// regardless of interface.
*pdwSupportedOptions = *pdwEnabledOptions = dwSupportedBits;
return retval; // E_NOINTERFACE if QI failed
}
/////////////////////////////////////////////////////////////////////////////
// CXXXCtrl::XObjSafe::SetInterfaceSafetyOptions
HRESULT STDMETHODCALLTYPE
CXXXCtrl::XObjSafe::SetInterfaceSafetyOptions(
REFIID riid,
DWORD dwOptionSetMask,
DWORD dwEnabledOptions
)
{
METHOD_PROLOGUE(CXXXCtrl, ObjSafe)
// 接口是否存在
IUnknown FAR* punkInterface;
pThis-> ExternalQueryInterface(&riid, (void * *)&punkInterface);
if (punkInterface) // 接口存在
{
punkInterface-> Release(); // 释放引用
}
else // 接口不存在
{
return ResultFromScode(E_NOINTERFACE);
}
// Can 't set bits we don 't support.
if (dwOptionSetMask & dwNotSupportedBits)
{
return ResultFromScode(E_FAIL);
}
// Can 't set bits we do support to zero
dwEnabledOptions &= dwSupportedBits;
// (We already know there are no extra bits in mask. )
if ((dwOptionSetMask & dwEnabledOptions) != dwOptionSetMask)
{
return ResultFromScode(E_FAIL);
}
// Don 't need to change anything since we 're always safe.
return ResultFromScode(S_OK);
}
******************************************************************/
原文:http://topic.csdn.net/t/20040327/11/2892634.html