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;
}
到此完成了控件的安全标记。

如何做一个标记为安全的ACTIVEX控件的更多相关文章

  1. 开发ActiveX控件调用另一个ActiveX系列1——开发一个MFC ActiveX控件

    ActiveX开发的教程有很多,我也从中受益匪浅,例如以下这几篇: 基本教程:http://www.cnblogs.com/guenli/articles/1629915.html 注意事项:http ...

  2. OLE、OCX和ActiveX控件之间的比较

      OLE(Object Linking and Embedding,对象连接与嵌入) 一.过去的OLE和今天的OLE 最初的OLE含义是指在程序之间链接和嵌入对象数据,它提供了建立混合文档的手段(资 ...

  3. C#编写ActiveX控件

    用C#编写ActiveX控件 http://www.cnblogs.com/homer/archive/2005/01/04/86473.html http://www.cnblogs.com/hom ...

  4. 用C#编写ActiveX控件,开发浏览器控件,注册ActiveX 控件

    用C#编写ActiveX控件,开发浏览器控件,注册ActiveX 控件用C#编写ActiveX控件 开发浏览器控件这是本控件开发完成后的一个简单应用.我们可以利用它以本地文件夹为单位来批量更新服务器的 ...

  5. 用C#编写ActiveX控件

    http://www.cnblogs.com/homer/archive/2005/01/04/86473.html http://www.cnblogs.com/homer/archive/2005 ...

  6. ActiveX控件(MFC篇)

    目录 第1章 VC++6.0创建控件    1 1.1 目标    1 1.1.1 方法    1 1.1.2 属性    1 1.1.3 事件    1 1.2 创建项目    2 1.3 项目结构 ...

  7. 使用回调接口实现ActiveX控件和它的容器程序的通讯

    本文阅读基础:有一定的C++基础知识(了解继承.回调函数),对MFC的消息机制有一定了解,对COM的基础知识有一定了解,对ActiveX控件有一定了解. 一. 前言 ActiveX控件和它的容器程序如 ...

  8. 修改注册表添加IE信任站点及启用Activex控件

    Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/In ...

  9. [转]使用C#开发ActiveX控件全攻略

    前言: 这段时间因为工作的需要,研究了一下ActiveX控件.总结如下: 先说说ActiveX的基本概念. 根据微软权威的软件开发指南MSDN(Microsoft Developer Network) ...

随机推荐

  1. Maven详解()-- 常用命令

    Maven常用命令: Maven库: http://repo2.maven.org/maven2/ Maven依赖查询: http://mvnrepository.com/ 一,Maven常用命令: ...

  2. Git 学习(三)Git 创建版本库

    获取 Git 仓库 什么是 Git 仓库呢,仓库又名版本库,我们可以把他理解为一个文件夹.这个文件夹里的所有东西都需要被 Git 给管理起来,对立面每个文件的修改.编辑.删除都将被 Git 记录,以便 ...

  3. 注意:字符串substring方法在jkd6,7,8中的差异。

    标题中的substring方法指的是字符串的substring(int beginIndex, int endIndex)方法,这个方法在jdk6,7是有差异的. substring有什么用? sub ...

  4. python语法基础(类)

    一.什么是类? 类是具有相同属性的一类事物 类还有功能和属性,属性就是这类事物的特征,而功能就是它能做什么,也是就是方法或者函数. 在python中类用关键词class来声明 二.类的声明 类的声明方 ...

  5. BBS论坛 注册功能

    三.注册功能 # views.py文件 def register(request): back_dic = {'code': 100, 'msg': ''} form_obj = myforms.My ...

  6. java排序及泛型

    一.用泛型实现快排,可以传入不通类型进行排序,比如String数组,Integer数组. /** * 快速排序 * * @author chx * */ public class QuickSort ...

  7. 【笔记篇】斜率优化dp(一) HNOI2008玩具装箱

    斜率优化dp 本来想直接肝这玩意的结果还是被忽悠着做了两道数论 现在整天浑浑噩噩无心学习甚至都不是太想颓废是不是药丸的表现 各位要知道我就是故意要打删除线并不是因为排版错乱 反正就是一个del标签嘛并 ...

  8. 2019牛客暑期多校训练营(第八场)D-Distance 定期重构

    题目传送门 题意: 在一个三维空间中,给出q次操作,每次操作可以在空间中加上一个固定点,或者询问一个点,对于一个询问操作,输出距离这个点最近的固定点的曼哈顿距离. 思路: 官方题解:先假设所有询问都在 ...

  9. 删除除了特指的某几个文件外的所有文件的Linux指令

    栗子: 不删除 logs文件夹和credential文件夹 1.  rm -rf  !(logs|credential) 2.  ls | grep -v logs |grep -v credenti ...

  10. leetcode-157周赛-5215黄金矿工

    题目描述: 方法一:dfs class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: maxx = 0 R, ...