注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

jiwenxu的博客

CODE

 
 
 

日志

 
 

将MFC ActiveX 控件标记为安全  

2009-05-26 10:05:05|  分类: C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

在APP.CPP文件,首先包含#include "comcat.h"

在VC6中,默认是没有这个文件的,需要下载

http://www.microsoft.com/downloads/details.aspx?familyid=EBA0128F-A770-45F1-86F3-7AB010B398A3&displaylang=en

这是Windows Server 2003 SP1 Platform SDK的下载地址

声明

const CATID CATID_SafeForScripting =
 {0x7dd95801,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};

const CATID CATID_SafeForInitializing =
 {0x7dd95802,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};

const GUID CDECL BASED_CODE _ctlid =
 { 0x9f225a87, 0xec35, 0x45a7, {0x86, 0xa4, 0xfe, 0xbf, 0x81, 0x11, 0xde, 0xae} };

三个变量,其中_ctlid的值可以是在Ctrl.cpp文件中,找到IMPLEMENT_OLECREATE_EX

在APP.CPP文件中增加以下两个方法

HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription)
{
 ICatRegister* pcr = NULL ;
 HRESULT hr = S_OK ;

 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
                       NULL,
                       CLSCTX_INPROC_SERVER,
                       IID_ICatRegister,
                       (void**)&pcr);
 if (FAILED(hr))
 {
  return hr;
 }

 // Make sure the HKCR\Component Categories\{..catid...}
 // key is registered
 CATEGORYINFO catinfo;
 catinfo.catid = catid;
 catinfo.lcid = 0x0409 ; // english

 // Make sure the provided description is not too long.
 // Only copy the first 127 characters if it is
 int len = wcslen(catDescription);
 if (len>127)
 {
  len = 127;
 }
 wcsncpy(catinfo.szDescription, catDescription, len);
 // Make sure the description is null terminated
 catinfo.szDescription[len] = '\0';

 hr = pcr->RegisterCategories(1, &catinfo);
 pcr->Release();

 return hr;
}

// Helper function to register a CLSID as belonging to a component
// category
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
{
 // Register your component categories information.
 ICatRegister* pcr = NULL ;
 HRESULT hr = S_OK ;
 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
                       NULL,
                       CLSCTX_INPROC_SERVER,
                       IID_ICatRegister,
                       (void**)&pcr);
 if (SUCCEEDED(hr))
 {
  // Register this category as being "implemented" by
  // the class.
  CATID rgcatid[1] ;
  rgcatid[0] = catid;
  hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);
 }

 if (pcr != NULL)
 {
  pcr->Release();
 }

 return hr;
}

修改DllRegisterServer方法

STDAPI DllRegisterServer(void)
{
 AFX_MANAGE_STATE(_afxModuleAddrThis);

 if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
  return ResultFromScode(SELFREG_E_TYPELIB);

 if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
  return ResultFromScode(SELFREG_E_CLASS);

 ////////////////////////////////////////////////////////////////////////////////
 // iobjectsafety
 /*
 HRESULT hr;
 hr = CreateComponentCategory(CATID_SafeForInitializing,
  L"Controls safely initializable from persistent data!");
 if(FAILED(hr))
 {
  return hr;
 }

 hr = RegisterCLSIDInCategory(CLSID_SafeItem,CATID_SafeForInitializing);
 if(FAILED(hr))
 {
  return hr;
 }

 // Mark the control as safe for scripting.
 hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!");
 if(FAILED(hr))
 {
  return hr;
 }

 hr = RegisterCLSIDInCategory(CLSID_SafeItem,CATID_SafeForScripting);
 if(FAILED(hr))
 {
  return hr;
 }
 */
 if (FAILED( CreateComponentCategory(
    CATID_SafeForScripting,
    L"Controls that are safely scriptable") ))
  return ResultFromScode(SELFREG_E_CLASS);

 if (FAILED( CreateComponentCategory(
    CATID_SafeForInitializing,
    L"Controls safely initializable from persistent data") ))
  return ResultFromScode(SELFREG_E_CLASS);

 if (FAILED( RegisterCLSIDInCategory(
    _ctlid, CATID_SafeForScripting) ))
  return ResultFromScode(SELFREG_E_CLASS);

 if (FAILED( RegisterCLSIDInCategory(
    _ctlid, CATID_SafeForInitializing) ))
  return ResultFromScode(SELFREG_E_CLASS);

 ////////////////////////////////////////////////////////////////////////////////

 return NOERROR;
}

  评论这张
 
阅读(263)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018