向导创建的COM组件在网页中使用时,会出现不安全被禁用之类的提示,这时需要将组件标记为安全的,才能避免被禁用。对于ATL和MFC实现的COM组件,标记为安全的方法分别如下。

1.ATL COM组件
a)让自己的CTestATLClass继承下面的类

public IObjectSafetyImpl<CTestATLClass,INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>

b)在BEGIN_COM_MAP(CTestATLClass)和END_COM_MAP()之间加入

COM_INTERFACE_ENTRY(IObjectSafety)

需要注意的是,要使网页中可以正常使用组件,必须在建立ATL对象时选中支持:
01) ISupportErrorInfo
02) 连接点
03) IObjectWithSite

参考文章:
http://support.microsoft.com/kb/168371/en-us

2.MFC ActiveX
a) 添加Cathelp.cpp,Cathelp.h到项目中
a.1) Cathelp.cpp

#include "stdafx.h"
#include "comcat.h"

// Helper function to create a component category and associated
// description
HRESULT CreateComponentCategory(CATID catid, WCHAR* 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

 // Make sure the provided description is not too long.
 // Only copy the first 127 characters if it is
 int len = wcslen(catDescription);
 if (len>127)
  len = 127;
 wcsncpy(catinfo.szDescription, catDescription, len);
 // Make sure the description is null terminated
 catinfo.szDescription[len] = '\0';

 hr = pcr->RegisterCategories(1, &catinfo);
 pcr->Release();

 return hr;
}

// Helper function to register a CLSID as belonging to a component
// category
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;
}

a.2) Cathelp.h

#include "comcat.h"

// Helper function to create a component category and associated
// description
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);

// Helper function to register a CLSID as belonging to a component
// category
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);

b) 修改DllRegisterServer所在的文件
b.1) 在顶部增加如下定义

#include "Cathelp.h"

const CATID CATID_SafeForScripting     =
{0x7dd95801,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};
const CATID CATID_SafeForInitializing  =
{0x7dd95802,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};

//_ctlid就是来自:
//IMPLEMENT_OLECreate_EX(CTestCtrl, "TEST.TestCtrl.1" ...
const GUID CDECL BASED_CODE _ctlid =
{0x84071678,0x1a35,0x4425,{0x92,0x9f,0xc9,0x65,0x25,0xae,0x90, 0x95}};

b.2) 在DllRegisterServer中加入如下函数

if (FAILED( CreateComponentCategory(
 CATID_SafeForScripting,
 L"Controls that are safely scriptable") ))
 return ResultFromScode(SELFREG_E_CLASS);

if (FAILED( CreateComponentCategory(
 CATID_SafeForInitializing,
 L"Controls safely initializable from persistent data") ))
 return ResultFromScode(SELFREG_E_CLASS);

if (FAILED( RegisterCLSIDInCategory(
 _ctlid, CATID_SafeForScripting) ))
 return ResultFromScode(SELFREG_E_CLASS);

if (FAILED( RegisterCLSIDInCategory(
 _ctlid, CATID_SafeForInitializing) ))
 return ResultFromScode(SELFREG_E_CLASS);

参考文章:http://support.microsoft.com/kb/161873/zh-cn


本文链接地址: 将COM组件(ActiveX)标记为安全的方法
http://qingfengju.com/index.asp?id=328

分类:Win32/C++ 查看次数:4877 发布时间:2013-4-26 20:23:30