概述
前面
两
篇文章分
别
介
绍
了
MFC ActiveX
应
用程序和使用
ATL
开发
ActiveX
的
简单实
例,但
还
有
两个问题
需要解
决
:
1)标记ActiveX控件为安全的控件 2)对控件进行数字签名。本文将结合这两点进行简单的介绍。
Building a Safe ActiveX Control
如何不想办法将控件标记为安全的,就会在Web页面与控件进行交互时出现如下图的警告信息:
下面将分别介绍在MFC ActiveX和ATL中如何标记一个控件为安全的控件。
要标记一个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 HKCRComponent 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 ] = '
#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 HKCRComponent Categories{..catid
![](https://file2.kaopuke.com:8081/files_image/2023110901/202311090146049722614.png)
// 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 ] = '