1. 程式人生 > >VC6.0編寫IE可執行的ActiveX,增加安全檢測項,不支援64位瀏覽器

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;
}