1.UUID值:
XXXX.idl最下部:
// CMsgPromptCtrl 的类信息
[
uuid(84045072-8AA6-40D2-A641-0097F7F6650F)
]
coclass MsgPrompt
{
[default] dispinterface _DMsgPrompt;
[default, source] dispinterface _DMsgPromptEvents;
};
2.安全代码:
在XXXCtrl.h中添加
#include <objsafe.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);
在XXXXCtrl.Cpp中添加:
//安全代码
BEGIN_INTERFACE_MAP( CMsgPromptCtrl, COleControl )
INTERFACE_PART(CMsgPromptCtrl, IID_IObjectSafety, ObjSafe)
END_INTERFACE_MAP()
//安全代码;
ULONG FAR EXPORT CMsgPromptCtrl::XObjSafe::AddRef()
{
METHOD_PROLOGUE(CMsgPromptCtrl, ObjSafe)
return pThis->ExternalAddRef();
}
ULONG FAR EXPORT CMsgPromptCtrl::XObjSafe::Release()
{
METHOD_PROLOGUE(CMsgPromptCtrl, ObjSafe)
return pThis->ExternalRelease();
}
HRESULT FAR EXPORT CMsgPromptCtrl::XObjSafe::QueryInterface(
REFIID iid, void FAR* FAR* ppvObj)
{
METHOD_PROLOGUE(CMsgPromptCtrl, ObjSafe)
return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}
const DWORD dwSupportedBits =
INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;
HRESULT STDMETHODCALLTYPE CMsgPromptCtrl::XObjSafe::GetInterfaceSafetyOptions(
REFIID riid,
DWORD __RPC_FAR *pdwSupportedOptions,
DWORD __RPC_FAR *pdwEnabledOptions
)
{
METHOD_PROLOGUE(CMsgPromptCtrl, 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 CMsgPromptCtrl::XObjSafe::SetInterfaceSafetyOptions(
REFIID riid,
DWORD dwOptionSetMask,
DWORD dwEnabledOptions
)
{
METHOD_PROLOGUE(CMsgPromptCtrl, 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);
}
3.添加接口和事件:
添加接口:
XXXXLib下的DXXXX右键-->添加-->添加方法
添加事件:
XXXXCtrl右键-->添加-->添加事件