The previous two articles introduced MFC ActiveX applications and simple instances of using ATL to develop ActiveX, but there are two other issues to address:
1 marks the ActiveX control as a secure control 2) digitally signs the control. This article will combine these two points to carry on the simple introduction.
Building a Safe ActiveX control
How to not think of a way to mark a control as safe will appear as a warning message in the following illustration when the Web page interacts with the control:
The following describes how to mark a control as safe in MFC ActiveX and ATL.
To mark an MFC ActiveX control as safe, you can modify it by imitating the following code:
Implementation of CARDSCAN.CPP:CCARDSCANAPP and DLL registration.
#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 initialization
BOOL ccardscanapp::initinstance ()
{
BOOL binit = Colecontrolmodule::initinstance ();
if (binit)
{
}
return binit;
}
Ccardscanapp::exitinstance-dll terminated.
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
The key is registered.
CategoryInfo Catinfo;
Catinfo.catid = catid;
Catinfo.lcid = 0x0409; 中文版
size_t Len;
Make sure the provided description are not too long.
Only copy the "127" characters if it is.
The second parameter of Stringcchlength is the maximum
Number of characters that May is read into catdescription.
There must is 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 are 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] = ';
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-adding items to the system registry
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-Remove items from the system registry
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;
}