1.添加辅助函数
控件的基本结构中含有xxApp,xxCtrl,xxPropPage三个类。找到xxApp的头文件,添加三个辅助函数。
// Helper functionto create a component category and associated
// description
HRESULT CreateComponentCategory(CATIDcatid, WCHAR* catDescription);
// Helper functionto register a CLSID as belonging to a component
// category
HRESULTRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);
// Helper functionto unregister a CLSID as belonging to a component
// category
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATIDcatid);
找到xxApp的实现文件,添加三个辅助函数的实现。
// Helper functionto create a component category and associated
// description
HRESULTCreateComponentCategory(CATID catid, WCHAR* catDescription)
{
ICatRegister*pcr = NULL ;
HRESULThr = S_OK ;
hr= CoCreateInstance(CLSID_StdComponentCategoriesMgr,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICatRegister,
(void**)&pcr);
if (FAILED(hr))
return hr;
// Make sure the HKCR/ComponentCategories/{..catid...}
// key is registered
CATEGORYINFOcatinfo;
catinfo.catid= catid;
catinfo.lcid= 0x0409 ; // english
// Make sure the provided description is nottoo long.
// Only copy the first 127 characters if itis
int len = wcslen(catDescription);
if (len>127)
len= 127;
wcsncpy(catinfo.szDescription,catDescription, len);
// Make sure the description is nullterminated
catinfo.szDescription[len]= '/0';
hr= pcr->RegisterCategories(1, &catinfo);
pcr->Release();
return hr;
}
// Helper functionto register a CLSID as belonging to a component
// category
HRESULTRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
{
// Register your component categoriesinformation.
ICatRegister*pcr = NULL ;
HRESULThr = 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.
CATIDrgcatid[1] ;
rgcatid[0]= catid;
hr= pcr->RegisterClassImplCategories(clsid, 1, rgcatid);
}
if (pcr != NULL)
pcr->Release();
return hr;
}
// HRESULTUnRegisterCLSIDInCategory - Remove entries from the registry
HRESULTUnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
{
ICatRegister*pcr = NULL ;
HRESULThr = S_OK ;
hr= CoCreateInstance(CLSID_StdComponentCategoriesMgr,
NULL,CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
if (SUCCEEDED(hr))
{
// Unregister this category as being"implemented" by the class.
CATIDrgcatid[1] ;
rgcatid[0]= catid;
hr= pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);
}
if (pcr != NULL)
pcr->Release();
return hr;
}
2.定义GUID
需要定义两个GUID用来注册控件安全性。
const CATIDCATID_SafeForScripting = {0x7dd95801,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};
const CATIDCATID_SafeForInitializing = {0x7dd95802,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};
在控件自动注册完成后,可以在注册表的如下地方看到上面的两个GUID。
HKEY_CLASSES_ROOT/CLSID/{"your controlsGUID"}/Implemented
Categories/{7DD95801-9882-11CF-9FA9-00AA006C42C4}
HKEY_CLASSES_ROOT/CLSID/{"your controls GUID"}/Implemented
Categories/{7DD95802-9882-11CF-9FA9-00AA006C42C4}
同时需要定义要注册为安全的CLSID。
控件有四个UUID,这里我们需要将xxCtrl的UUID注册成为安全的CLSID,因为我们控件的主体功能是在这个类中实现的。
const GUIDCDECL BASED_CODE _tlid =
{ 0x7DE84B6C,0x9969, 0x4DE0, { 0xBE, 0x25, 0xC6, 0xC0, 0x63, 0x20, 0xA3, 0x70 } };
const WORD_wVerMajor = 1;
const WORD_wVerMinor = 0;
const CATIDCLSID_SafeItem =
{0x4e586c5a,0xfd41, 0x4e4c, {0xb6, 0x6d, 0x63, 0xf1, 0x10, 0xc8, 0xc4, 0xb9}};
这里的CLSID_SafeItem就是xxCtrl的UUID。
3.修改注册代码
//DllRegisterServer - 将项添加到系统注册表
STDAPIDllRegisterServer(void)
{
/*这里是原来的注册代码
OLD
AFX_MANAGE_STATE(_afxModuleAddrThis);
if(!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
returnResultFromScode(SELFREG_E_TYPELIB);
if(!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
returnResultFromScode(SELFREG_E_CLASS);
returnNOERROR;*/

//NEW下面是新的注册代码
AFX_MANAGE_STATE(_afxModuleAddrThis);
if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
return ResultFromScode(SELFREG_E_CLASS);
if (FAILED( CreateComponentCategory(
CATID_SafeForScripting,
L"Controls that are safelyscriptable") ))
return ResultFromScode(SELFREG_E_CLASS);
if (FAILED( CreateComponentCategory(
CATID_SafeForInitializing,
L"Controls safely initializable frompersistent data")))
return ResultFromScode(SELFREG_E_CLASS);
if (FAILED( RegisterCLSIDInCategory(
CLSID_SafeItem,CATID_SafeForScripting) ))
return ResultFromScode(SELFREG_E_CLASS);
if (FAILED( RegisterCLSIDInCategory(
CLSID_SafeItem,CATID_SafeForInitializing) ))
return ResultFromScode(SELFREG_E_CLASS);
return NOERROR;
}
//DllUnregisterServer - 将项从系统注册表中移除
STDAPIDllUnregisterServer(void)
{
/*
OLD这里是原来的代码
AFX_MANAGE_STATE(_afxModuleAddrThis);
if(!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
returnResultFromScode(SELFREG_E_TYPELIB);
if(!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
returnResultFromScode(SELFREG_E_CLASS);
returnNOERROR;*/

//NEW下面是新的代码
HRESULThr;
AFX_MANAGE_STATE(_afxModuleAddrThis);
// Remove entries from the registry.
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,
CATID_SafeForInitializing);
if (FAILED(hr))
return hr;
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,
CATID_SafeForScripting);
if (FAILED(hr))
return hr;
if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
return ResultFromScode(SELFREG_E_CLASS);
return NOERROR;
}
到此完成了控件的安全标记。

最新文章

  1. apache 配置rewrite模块,URL中隐藏index.php
  2. KendoUI系列:DropDownList
  3. WinCE项目应用之虚拟仪器(VI)
  4. Ubuntu下matlab快捷键设置
  5. Ranorex 5 发布,支持SAP、Oracle Forms、MS Dynamics等
  6. 请求参数到表述层的类型转换——Struts2
  7. SQL Server中的连接查询【内连接,左连接,右连接,。。。】
  8. 判断java中两个对象是否相等
  9. asp中的几个取整函数fix(),int(),round()的用法
  10. VisualBox ubuntu14.04 64位 android4.4.4源码编译总结
  11. ubuntu中出现警告:Gtk-WARNING**: 无法在模块路径中找到主题引擎:“pixmap”
  12. Git 常用命令汇总
  13. 配置tomcat及如何自动编译jsp文件
  14. C# 按不同的字节编码,通过字节数去截取字符串
  15. git 与 ftp 共同工作
  16. Hibernate的cascade属性 特别是 cascadeType.all的 作用
  17. MySQL慢查询日志相关的配置和使用。
  18. webform 使用富文本编辑器
  19. url文件的格式
  20. uboot下emmc内容烧写(拷贝)步骤

热门文章

  1. Linux下rsync的安装及简单使用
  2. solr 启动报错Cannot load analyzer: org.wltea.analyzer.lucene.IKAnalyzer
  3. liunx crontab 参数代表含义
  4. 2018湘潭大学程序设计竞赛【B】
  5. nginx基础内容
  6. ES6 学习 -- 箭头函数(=>)
  7. VBA字典做数据有效性
  8. Linux 常用命令:解压缩篇
  9. 【JZOJ4616】二进制的世界
  10. 巧用CSS3的calc()宽度计算做响应模式布局