您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > 开发安全标记的MFC ActiveX 控件及在IIS网页中使用和发布
开发安全标记的MFCActiveX控件及在IIS网页中使用和发布问题引入:在VC6.0使用MFCActiveXControlWizard开发ActiveX控件时,默认情况下MFCActiveX控件未标记为安全的脚本和初始化的安全。所以导致在控件在IIS中发布时,造成可以加载但是不能正常构造和初始化(即在网页中调用时显示一个红色叉,但其实通过调试发现控件实际是加载了的)。这个问题得解决涉及两方面的问题即1、实现IObjectSafety接口的控件使得在Internet浏览器的上下文中运行“安全”。2、修改该控件的DllRegisterServer函数来标记该控件在注册表中安全。本文将以一个CCircleCtrl控件实例来说明实现方法,下面分别说明具体实现方法。1、生成默认ActiveX控件及控件的调试(老鸟跳过)。新建工程选择类型选择MFCActiveXContronWizard在Projectname中输入”Circle”点击”OK”,其余均按默认完成向导。将工程编译、连接后,你就已经实现了一个ActiveX控件,并且已经注册到你的计算机。你可以通过在注册表的中找到HKEY_CLASSES_ROOT\CIRCLE.CircleCtrl.1,CLSID中得默认值就是Circle控件的注册号(唯一标志ID),形如04D9986E-E2D7-4827-A8F6-BFE003E64D54但是注意不同计算机生成的这个值是不一样的,下面使用这个值时请将它替换为你的注册号。调试:ActiveX可以使用ActiveXControlTestContainer和浏览器来或其他使用此控件的应用程序来调试。如下图:(1)使用ActiveXControlTestContainer调试:F5,在ActiveXControlTestContainer启动后,右键选择“插入新控件”,选择我们刚刚生成的控件CircleControl。插入后你将看到一个显示了圆形的控件。控件运行成功。(2)使用浏览器调试:新建记事本写入如下代码:(注意clsid用你自己的)headtitleActivXTest/title/headbodycenterobjectclassid=clsid:04D9986E-E2D7-4827-A8F6-BFE003E64D54codebase=circle.ocx#version=1,0,0,0height=400pxwidth=400px/object/center/body将文件名改为Circle.html更改调试方式为DefaultWebBrowser;F5(因为已经注册了控件你也可以直接点击这个.html文件查看效果)在地址栏中输入以上html文件的路径,并允许阻止内容。预料中的“圆形”也如期而至。但是把Circle.html拷贝至IIS根目录,在浏览器中输入,按理说我们的计算机已经注册了这个控件,应该显示出“圆形”但是结果却是出现了一个红色的叉。控件在网络发布的时候出现了问题。这就是浏览器阻止了非安全标记的ActiveX控件。要解决这个问题得实现下面的2、3步骤。(解决IIS的安装及配置和使用等网络上资源很多)。2、实现IObjectSafety接口的控件在CircleCtl.h加入#ifdefL_IMPL_OBJECTSAFETY#includeobjsafe.h#endif//L_IMPL_OBJECTSAFETY在CCircleCtrl类定义中的DECLARE_MESSAGE_MAP()语句后加入:public:#ifdefL_IMPL_OBJECTSAFETYBEGIN_INTERFACE_PART(ObjectSafety,IObjectSafety)STDMETHOD(GetInterfaceSafetyOptions)(REFIIDriid,DWORD__RPC_FAR*pdwSupportedOptions,DWORD__RPC_FAR*pdwEnabledOptions);STDMETHOD(SetInterfaceSafetyOptions)(REFIIDriid,DWORDdwOptionSetMask,DWORDdwEnabledOptions);END_INTERFACE_PART(ObjectSafety)DECLARE_INTERFACE_MAP();#endif//L_IMPL_OBJECTSAFETY在CricleCtl.cpp文件中IMPLEMENT_DYNCREATE(CCircleCtrl,COleControl)语句后加入:#ifdefL_IMPL_OBJECTSAFETYBEGIN_INTERFACE_MAP(CCircleCtrl,COleControl)INTERFACE_PART(CCircleCtrl,IID_IObjectSafety,ObjectSafety)END_INTERFACE_MAP()#endif//L_IMPL_OBJECTSAFETY在CricleCtl.cpp文件末尾加入:#ifdefL_IMPL_OBJECTSAFETY//ImplementationofIObjectSafetySTDMETHODIMPCCircleCtrl::XObjectSafety::GetInterfaceSafetyOptions(REFIIDriid,DWORD__RPC_FAR*pdwSupportedOptions,DWORD__RPC_FAR*pdwEnabledOptions){METHOD_PROLOGUE_EX(CCircleCtrl,ObjectSafety)if(!pdwSupportedOptions||!pdwEnabledOptions){returnE_POINTER;}*pdwSupportedOptions=INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA;*pdwEnabledOptions=0;if(NULL==pThis-GetInterface(&riid)){//TRACE(Requestedinterfaceisnotsupported.\n);returnE_NOINTERFACE;}//Whatinterfaceisbeingcheckedoutanyhow?OLECHARszGUID[39];inti=StringFromGUID2(riid,szGUID,39);if(riid==IID_IDispatch){//Clientwantstoknowifobjectissafeforscripting*pdwEnabledOptions=INTERFACESAFE_FOR_UNTRUSTED_CALLER;returnS_OK;}elseif(riid==IID_IPersistPropertyBag||riid==IID_IPersistStreamInit||riid==IID_IPersistStorage||riid==IID_IPersistMemory){//ThosearethepersistenceinterfacesCOleControlderivedcontrolssupport//asindicatedinAFXCTL.H//Clientwantstoknowifobjectissafeforinitializingfrompersistentdata*pdwEnabledOptions=INTERFACESAFE_FOR_UNTRUSTED_DATA;returnS_OK;}else{//Findoutwhatinterfacethisis,anddecidewhatoptionstoenable//TRACE(Wedidn'taccountforthesafetyofthisinterface,andit'sonewesupport...\n);returnE_NOINTERFACE;}}STDMETHODIMPCCircleCtrl::XObjectSafety::SetInterfaceSafetyOptions(REFIIDriid,DWORDdwOptionSetMask,DWORDdwEnabledOptions){METHOD_PROLOGUE_EX(CCircleCtrl,ObjectSafety)OLECHARszGUID[39];//Whatisthisinterfaceanyway?//WecandoaquicklookupintheregistryunderHKEY_CLASSES_ROOT\Interfaceinti=StringFromGUID2(riid,szGUID,39);if(0==dwOptionSetMask&&0==dwEnabledOptions){//thecontrolcertainlysupportsNOrequeststhroughthespecifiedinterface//soit'ssafetoreturnS_OKeveniftheinterfaceisn'tsupported.returnS_OK;}//Dowesupportthespecifiedinterface?if(NULL==pThis-GetInterface(&riid)){TRACE1(%sisnotsupport.\n,szGUID);returnE_FAIL;}if(riid==IID_IDispatch){//TRACE(Clientaskingifit'ssafetocallthroughIDispatch.\n);//TRACE(Inotherwords,isthecontrolsafeforscripting?\n);if(INTERFACESAFE_FOR_UNTRUSTED_CALLER==dwOptionSetMask&&INTERFACESAFE_FOR_UNTRUSTED_CALLER==dwEnabledOptions){returnS_OK;}else{returnE_FAIL;}}elseif(riid==IID_IPersistPropertyBag||riid==IID_IPersistStreamInit||riid==IID_IPersistStorage||riid==IID_IPersistMemory){//TRACE(Clientaskingifit'ssafetocallthroughIPersist*.\n);//TRACE(Inotherwords,isthecontrolsafeforinitializingfrompersistentdata?\n);if(INTERFACESAFE_FOR_UNTRUSTED_DATA==dwOptionSetMask&&INTERFACESAFE_FOR_UNTRUSTED_DATA==dwEnabledOptions){returnNOERROR;}else{returnE_FAIL;}}else{TRACE1(Wedidn'taccountforthesafetyof%s,andit'sonewesupport...\n,szGUID);returnE_FAIL;}}STDMETHODIMP_(ULONG)CCircleCtrl::XObjectSafety::AddRef(){METHOD_PROLOGUE_EX_(CCircleCtrl,ObjectSafety)return(ULONG)pThis-ExternalAddRef();}STDMETHODIMP_(ULONG)CCircleCtrl::XObjectSafety::Release(){METHOD_PROLOGUE
本文标题:开发安全标记的MFC ActiveX 控件及在IIS网页中使用和发布
链接地址:https://www.777doc.com/doc-325523 .html