Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1589468
  • 博文数量: 441
  • 博客积分: 20087
  • 博客等级: 上将
  • 技术积分: 3562
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-19 15:35
文章分类

全部博文(441)

文章存档

2014年(1)

2012年(1)

2011年(8)

2010年(16)

2009年(15)

2008年(152)

2007年(178)

2006年(70)

分类: C/C++

2008-12-09 17:40:43

自己开发了ActiveX控件用在网页上, 但是用的时候却会出现安全提示, 而且往往被IE的安全功能给干掉了,为了避免这个问题, 可以按照如下的方法来消除这种安全提示:


////////////////////////////////////////////////////////////////

    // Copied from the ActiveX SDK

    // This code is used to register and unregister a

    // control as safe for initialization and safe for scripting

    /*****************************************
    在 xxxctl.cpp 文件中添加如下两个头文件引用
    *****************************************/

    #include "comcat.h"
    #include "objsafe.h"


    /*****************************************
    然后在 xxxctl.cpp 文件中添加如下代码:
    *****************************************/

   

    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;
    }
    
    
    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(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;
    }
   
    
    /*****************************************
    修改如下函数
    *****************************************/

    BOOL CxxxCtrl::CxxxCtrlFactory::UpdateRegistry(BOOL bRegister)
    {
    // TODO: Verify that your control follows apartment-model threading rules.

    // Refer to MFC TechNote 64 for more information.

    // If your control does not conform to the apartment-model rules, then

    // you must modify the code below, changing the 6th parameter from

    // afxRegInsertable | afxRegApartmentThreading to afxRegInsertable.

    
    if (bRegister)
    {
    HRESULT hr = S_OK ;
    
    // register as safe for scripting

    hr = CreateComponentCategory(CATID_SafeForScripting,
    L"Controls that are safely scriptable");
    
    if (FAILED(hr))
    return FALSE;
    
    hr = RegisterCLSIDInCategory(m_clsid, CATID_SafeForScripting);
    
    if (FAILED(hr))
    return FALSE;
    
    // register as safe for initializing

    hr = CreateComponentCategory(CATID_SafeForInitializing,
    L"Controls safely initializable from persistent data");
    
    if (FAILED(hr))
    return FALSE;
    
    hr = RegisterCLSIDInCategory(m_clsid, CATID_SafeForInitializing);
    
    if (FAILED(hr))
    return FALSE;
    
    return AfxOleRegisterControlClass(
    AfxGetInstanceHandle(),
    m_clsid,
    m_lpszProgID,
    IDS_DIEROLL,
    IDB_DIEROLL,
    afxRegInsertable | afxRegApartmentThreading,
    _dwDierollOleMisc,
    _tlid,
    _wVerMajor,
    _wVerMinor);
    }
    else
    {
    HRESULT hr = S_OK ;
    
    hr = UnRegisterCLSIDInCategory(m_clsid, CATID_SafeForScripting);
    
    if (FAILED(hr))
    return FALSE;
    
    hr = UnRegisterCLSIDInCategory(m_clsid, CATID_SafeForInitializing);
    
    if (FAILED(hr))
    return FALSE;
    
    return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
    }
    }
     


2点补充:
 1.在xxxctl.h 文件中添加如下两个头文件
    #include "comcat.h"
    #include "objsafe.h"
    注意:要加在类的声明前

 2.在UpdateRegistry(BOOL bRegister)中
    IDS_DIEROLL
    应为IDS_XXXXXXX
   
    IDB_DIEROLL
    应为IDS_XXXXXXX
   
    _dwDierollOleMisc
    应为_dwXXXXOleMisc
   
    其中的XXXX为你的工程名。

阅读(1664) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~