C++开发ActiveX控件相关事项

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右键-->添加-->添加事件

        

转载于:https://www.cnblogs.com/ZhJRecord/p/5175398.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值