博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ActiveX控件安全初始化之二:修改注册表
阅读量:3986 次
发布时间:2019-05-24

本文共 5382 字,大约阅读时间需要 17 分钟。

转自:http://www.cnblogs.com/phinecos/archive/2008/08/07/1263270.html

要标记一个MFC ActiveX控件为安全,可以仿照下面代码修改而得:

//
 CardScan.cpp : CCardScanApp 和DLL 注册的实现。
#include 
"
stdafx.h
"
#include 
"
CardScan.h
"
#include 
"
comcat.h
"
#include 
"
strsafe.h
"
#include 
"
objsafe.h
"
CCardScanApp theApp;
const
 GUID CDECL BASED_CODE _tlid 
=
        { 
0x29959268
0x9729
0x458E
, { 
0xA8
0x39
0xBB
0x39
0x2E
0xCB
0x7E
0x37
 } };
const
 WORD _wVerMajor 
=
 
1
;
const
 WORD _wVerMinor 
=
 
0
;
const
 CATID CLSID_SafeItem 
=
{
0xB548F3C7
,
0x2135
,
0x4242
,{
0x92
,
0x0B
,
0xA7
,
0xBD
,
0xEE
,
0x6D
,
0x2B
,
0xA3
}};
//
{ 0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}};
//
 CCardScanApp::InitInstance - DLL 初始化
BOOL CCardScanApp::InitInstance()
{
    BOOL bInit 
=
 COleControlModule::InitInstance();
    
if
 (bInit)
    {
    }
    
return
 bInit;
}
//
 CCardScanApp::ExitInstance - DLL 终止
int
 CCardScanApp::ExitInstance()
{
    
return
 COleControlModule::ExitInstance();
}
HRESULT CreateComponentCategory(CATID catid, CHAR 
*
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
    size_t len;
    
//
 Make sure the provided description is not too long.
    
//
 Only copy the first 127 characters if it is.
    
//
 The second parameter of StringCchLength is the maximum
    
//
 number of characters that may be read into catDescription.
    
//
 There must be room for a NULL-terminator. The third parameter
    
//
 contains the number of characters excluding the NULL-terminator.
    hr 
=
 StringCchLength(catDescription, STRSAFE_MAX_CCH, 
&
len);
    
if
 (SUCCEEDED(hr))
    {
        
if
 (len
>
127
)
        {
            len 
=
 
127
;
        }
    }   
    
else
    {
        
//
 TODO: Write an error handler;
    }
    
//
 The second parameter of StringCchCopy is 128 because you need 
    
//
 room for a NULL-terminator.
    hr 
=
 StringCchCopy(COLE2T(catinfo.szDescription), len 
+
 
1
, catDescription);
    
//
 Make sure the description is null terminated.
    catinfo.szDescription[len 
+
 
1
=
 
'
\0
'
;
    hr 
=
 pcr
->
RegisterCategories(
1
&
catinfo);
    pcr
->
Release();
    
return
 hr;
}
//
 HRESULT RegisterCLSIDInCategory -
//
      Register your component categories information
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;
}
//
 HRESULT UnRegisterCLSIDInCategory - Remove entries from the registry
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
{
    ICatRegister 
*
pcr 
=
 NULL ;
    HRESULT hr 
=
 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.
        CATID rgcatid[
1
] ;
        rgcatid[
0
=
 catid;
        hr 
=
 pcr
->
UnRegisterClassImplCategories(clsid, 
1
, rgcatid);
    }
    
if
 (pcr 
!=
 NULL)
        pcr
->
Release();
    
return
 hr;
}
//
 DllRegisterServer - 将项添加到系统注册表
STDAPI DllRegisterServer(
void
)
{
    HRESULT hr;
    AFX_MANAGE_STATE(_afxModuleAddrThis);
    
if
 (
!
AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
        
return
 ResultFromScode(SELFREG_E_TYPELIB);
    
if
 (
!
COleObjectFactoryEx::UpdateRegistryAll(TRUE))
        
return
 ResultFromScode(SELFREG_E_CLASS);
    
//
 Mark the control as safe for initializing.
    hr 
=
 CreateComponentCategory(CATID_SafeForInitializing, 
        _T(
"
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, 
        _T(
"
Controls safely  scriptable!
"
));
    
if
 (FAILED(hr))
        
return
 hr;
    hr 
=
 RegisterCLSIDInCategory(CLSID_SafeItem, 
        CATID_SafeForScripting);
    
if
 (FAILED(hr))
        
return
 hr;
    
return
 NOERROR;
}
//
 DllUnregisterServer - 将项从系统注册表中移除
STDAPI DllUnregisterServer(
void
)
{
    HRESULT hr;
    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;
}

     这里值得注意的一个地方是DllUnregisterServer函数,在这段代码中,我是将

hr
=
UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
hr
=
UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);

这两句代码放在

if
 (
!
AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
           
return
 ResultFromScode(SELFREG_E_TYPELIB);
      
if
 (
!
COleObjectFactoryEx::UpdateRegistryAll(FALSE))
           
return
 ResultFromScode(SELFREG_E_CLASS);

这两句代码的前面,如果你查阅MSDN,将会发现它上面的顺序和我是相反的,这应该是微软的一个错误代码,如果按照MSDN的代码来写,则你使用regsvr32 -u CardScan.ocx反注册时会报下面的错误:

调整为我所说的顺序就没问题了。

你可能感兴趣的文章
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
网页设计里的浮动 属性
查看>>
Maximum Subsequence Sum
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>