VC6.0编写IE可运行的ActiveX,增加安全检测项,不支持64位浏览器

综合网络上提供的方法,这个是比较全的App类的实现。

注册控件后,网页中访问时不再提示安全警告,也不必修改ie默认的安全级别。


// IDCardReader.cpp : Implementation of CIDCardReaderApp and DLL registration.


#include "stdafx.h"
#include "IDCardReader.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



#include "Cathelp.h"
#include "objsafe.h"
#include "comcat.h"     //CATID 的定义在这个控件里,很重要这个头文件


CIDCardReaderApp NEAR theApp;

const GUID CDECL BASED_CODE _tlid =
        { 0xb49cfb44, 0x5166, 0x49a1, { 0xb9, 0x68, 0x3e, 0x8a, 0x2d, 0x79, 0x22, 0xa1 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;

//以下是安全接口标识,必须与控件的ID相同
const GUID CDECL CLSID_SafeItem ={0x10946843, 0x7507, 0x44fe, {0xac, 0xe8, 0x2b, 0x34, 0x83, 0xd1, 0x79, 0xb7}};

/*
 根据msdn的提示要定义这两项,其实是vc6.0以下的版本,vc6 引入 comcat.h 即可
 const CATID CATID_SafeForScripting={0x7dd95801,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};
 const CATID CATID_SafeForInitializing=    { 0xb49cfb44, 0x5166, 0x49a1, { 0xb9, 0x68, 0x3e, 0x8a, 0x2d, 0x79, 0x22, 0xa1 } };
*/



// CIDCardReaderApp::InitInstance - DLL initialization

BOOL CIDCardReaderApp::InitInstance()
{
    BOOL bInit = COleControlModule::InitInstance();

    if (bInit)
    {
        // TODO: Add your own module initialization code here.
    }

    return bInit;
}



// CIDCardReaderApp::ExitInstance - DLL termination

int CIDCardReaderApp::ExitInstance()
{
    // TODO: Add your own module termination code here.

    return COleControlModule::ExitInstance();
}


/
//<--新增内容
// 创建组件种类  
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;  
}





// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
    AFX_MANAGE_STATE(_afxModuleAddrThis);

    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
        return ResultFromScode(SELFREG_E_CLASS);
//<--新增内容
    HRESULT hr = S_OK;  
    // 标记控件初始化安全.      
    // 创建初始化安全组件种类      
    hr = CreateComponentCategory(CATID_SafeForInitializing, L"Controls safely initializable from persistent data!");      
    if (FAILED(hr))          
        return hr;      
    // 注册初始化安全      
    hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);      
    if (FAILED(hr))          
        return hr;     
    // 标记控件脚本安全      
    // 创建脚本安全组件种类      
    hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!");      
    if (FAILED(hr))          
        return hr;      
    // 注册脚本安全组件种类      
    hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);      
    if (FAILED(hr))          
        return hr;
    //-->新增内容
    return NOERROR;
}


/
// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
    AFX_MANAGE_STATE(_afxModuleAddrThis);

    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
        return ResultFromScode(SELFREG_E_CLASS);

    return NOERROR;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值