C++編寫安全OCX,IE不彈出安全提示

來源:互聯網
上載者:User
下面將分別介紹在MFC ActiveX和ATL中如何標記一個控制項為安全的控制項。

 

     要標記一個MFC ActiveX控制項為安全,可以仿照下面代碼修改而得:

 

// CardScan.cpp : CCardScanApp 和DLL 註冊的實現。
#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 初始化
BOOL CCardScanApp::InitInstance()
{
    BOOL bInit = COleControlModule::InitInstance();
    if (bInit)
    {
    }
    return bInit;
}
// CCardScanApp::ExitInstance - DLL 終止
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}
    // key is registered.
    CATEGORYINFO catinfo;
    catinfo.catid = catid;
    catinfo.lcid = 0x0409 ; // english
    size_t len;
    // Make sure the provided description is not too long.
    // Only copy the first 127 characters if it is.
    // The second parameter of StringCchLength is the maximum
    // number of characters that may be read into catDescription.
    // There must be 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 is 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] = '/0';
    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 - 將項添加到系統註冊表

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 - 將項從系統註冊表中移除

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

     這裡值得注意的一個地方是DllUnregisterServer函數,在這段代碼中,我是將

hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);

hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);

這兩句代碼放在

if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))

           return ResultFromScode(SELFREG_E_TYPELIB);

      if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))

           return ResultFromScode(SELFREG_E_CLASS);

這兩句代碼的前面,如果你查閱MSDN,將會發現它上面的順序和我是相反的,這應該是微軟的一個錯誤碼,如果按照MSDN的代碼來寫,則你使用regsvr32 -u CardScan.ocx反註冊時會報下面的錯誤:

調整為我所說的順序就沒問題了。

2)要標記使用ATL寫的ActiveX控制項為安全的控制項,這比MFC要簡單的多,只需要在控制項標頭檔中增加幾行代碼就可以了:

class ATL_NO_VTABLE CTestCtrl :
    …
    public IObjectSafetyImpl<CTestCtrl, INTERFACESAFE_FOR_UNTRUSTED_CALLER| INTERFACESAFE_FOR_UNTRUSTED_DATA>,

然後在COM映射表中增加一項:

BEGIN_COM_MAP(CTestCtrl)
    …
    COM_INTERFACE_ENTRY(IObjectSafety)
END_COM_MAP()

Building a Signed ActiveX Control

      ActiveX控制項是個危險的東西,如果不對其合法性進行數位簽章和驗證,IE是會拒絕其安裝的。

      工具包準備:CABARC.exe, cert2spc.exe, makecab.exe, makecert.exe, signcode.exe(或新版本中的signtool),以上小工具都可以在VS的安裝路徑下"Common7"Tools"Bin找到,或去微軟官方網站上下載。

ActiveX控制項的安裝過程中,一部分工作就是自註冊,這需要控制項在VERSIONINFO結構中定義OLESelfRegister值,你可以對資源檔進行編輯如下

BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "080403a8"
        BEGIN
            VALUE "CompanyName", "TODO: <公司名>"
            VALUE "FileDescription", "TODO: <檔案說明>"
            VALUE "FileVersion", "1.0.0.1"
            VALUE "InternalName", "CardScan.ocx"
            VALUE "LegalCopyright", "TODO: (C) <公司名>。著作權所有,並保留一切權利。"
            VALUE "OLESelfRegister", "/0"
            VALUE "OriginalFilename", "CardScan.ocx"
            VALUE "ProductName", "TODO: <產品名>"
            VALUE "ProductVersion", "1.0.0.1"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x804, 936
    END
END

打包為CAB檔案

因為ActiveX控制項要放在網站上供客戶下載到本地,因此壓縮是必需的。一段典型的html代碼如下:

<OBJECT ID="FuckATL1"  
CODEBASE ="http://localhost:8080/CardScan.cab"
CLASSID="CLSID:B548F3C7-2135-4242-920B-A7BDEE6D2BA3" WIDTH=300 HEIGHT=200
/>

CODEBASE就指明了要下載的壓縮包,其中包含了oxc,dll控制項等所需要的檔案。

通常CAB檔案包含了一個INF檔案,它用來描述CAB檔案的所有細節資訊,下面舉個簡單例子,代碼如下:

; Sample INF file for SCRIPTABLEACTIVEX.DLL
[version] 
; version signature (same for both NT and Win95) do not remove
signature="$CHICAGO$"
AdvancedINF=2.0  

[Add.Code]
CardScan.ocx=CardScan.ocx
CardScan.inf=CardScan.inf

[CardScan.ocx]
file-win32-x86=thiscab
clsid={B548F3C7-2135-4242-920B-A7BDEE6D2BA3} 
FileVersion=1,0,0,1 
RegisterServer=yes

[CardScan.inf]
file=thiscab
; end of INF file
 
著作權聲明:轉載時請以超連結形式標明文章原始出處和作者資訊及本聲明
http://xingzhesun.blogbus.com/logs/53822346.html

 

看著xf的電腦中顯示的網頁,不停晃動的我可愛的ACTIVEX,我高興起來。
整理開發activex並在網站上發布的控制項過程如下。
@1本項目的開發工具是vs2005
@2本文參考了David Marcionek的“a comlete ActiveX Web control Tutorial”的文章http://www.codeproject.com/KB/COM/CompleteActiveX.aspx
一、控制項的修改
1.建立ACTIVEX項目,可以使用MFC,可以使用ATL,在mfcActivex控制項嚮導的第二步,“應用程式設定”頁面不選"運行時許可證",因為我不想購買運行許可。
2.建立工程後,在開啟rc檔案進行編輯,在version結構中添加activex的自註冊功能。 VALUE "OLESelfRegister", "/0"
// Version
//

3.編寫在WEB頁面中禁止提示的代碼,主要是修改APP的註冊函數,增加了兩個函數
 @HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription)
 @HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
修改了兩個函數
 @STDAPI DllRegisterServer(void)
 @HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
這裡修要定義一個常量,該值為classid 在MyActivexCtrl.ccp和IDL檔案中找到。
 const CATID CLSID_SafeItem ={0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}};
當我們自己開發的使用只需要講CLSID_SafeItem的修改為我們自己控制項的classID即可。
代碼如下:
#include "comcat.h"
#include "strsafe.h"
#include "objsafe.h"

const CATID CLSID_SafeItem ={0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}};
HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription)
{
   }

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

// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
    HRESULT hr;    // HResult used by Safety Functions

 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, L"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, L"Controls safely scriptable!");
    if (FAILED(hr))
        return hr;

    hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
    if (FAILED(hr))
        return hr;

 return NOERROR;
}

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

// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
    HRESULT hr;    // HResult used by Safety Functions

 AFX_MANAGE_STATE(_afxModuleAddrThis);

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

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

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

 return NOERROR;
}
4.修改編譯串連的項目配置,配置屬性->常規->項目預設值->MFC的使用=在靜態庫中使用MFC,這樣的設定目的是為保證ACTIVEX能夠在沒有MFCdll的客戶機上運行。
二、發布前的準備
1.使用MS Activex Control Pad建立html測試頁。當然你也可以自己寫html。測試成功。
2.快速建立一個網站。vs2005很方便的。在iis中設定預設網站的ip,主目錄和主檔案。
3.製作CAB檔案。
 因為用戶端在開啟包含activex控制項的頁面時,首先要下載該頁面,然後自註冊安裝,再運行。ms要求將ACTIVEX控制項depend的檔案包為cab檔案。
這裡需要使用ms的cabarc.exe進行打包,並且寫inf檔案,在打包時需要將inf檔案也加入。inf檔案運行activex的安裝。
在進行打包時使用David Marcionek寫好的批次檔即可,我們可以按照自己的需要進行修改。雙擊mkcal.bat檔案建立myactivex.cab檔案
4.修改測試的html檔案加入codebase屬性。
這裡需要注意codebase屬性指出了cab檔案的具體位置,用戶端從這位置下載cab檔案,如果codebase的值不正確則造成,用戶端不能夠正確下載activex,David Marcionek在文章中給大家留下了這個bug。
<HEAD>
<TITLE>MyActiveX CODEBASE</TITLE>
</HEAD>
<BODY>
<center>
MyActiveX With CODEBASE Example
<p></p>

<OBJECT ID="MyActiveX1" WIDTH=350 HEIGHT=50
 CODEBASE ="http://192.168.1.101/??/myactivex.cab"
 CLASSID="CLSID:36299202-09EF-4ABF-ADB9-47C599DBE778">
    <PARAM NAME="_Version" VALUE="65536">
    <PARAM NAME="_ExtentX" VALUE="2646">
    <PARAM NAME="_ExtentY" VALUE="1323">
    <PARAM NAME="_StockProps" VALUE="0">
</OBJECT>

</center>
</BODY>
</HTML>

5.將cab檔案拷貝到網站的指定位置。
三、ie安全屬性的設定。
又有我們沒有購買數位憑證,所以我們的控制項是不安全的需要在IE中進行有關activex的設定
@activex控制項自動提示=啟用
@對已標記為安全的Activex控制項進行初始化和指令碼=啟用
@對沒有標記為安全的Activex控制項進行初始化和指令碼=提示
@二進位和指令碼行為=啟用
@下載未簽名的ACTIVEX控制項=提示
@下載已簽名的ACTIVEX控制項=提示
@運行ACTIVEX控制項和外掛程式=提示
重設為=安全級-中
我們這樣設定首先保證了已簽名的activex的正確下載安裝和運行,同時對未簽名的ACTIVEX控制項都給與了“提示”,這樣用戶端的使用者就可以根據自己的判斷決定是否下載和安裝ACTIVE.


四、在伺服器端測試
1.首先將activex控制項刪除。因為vs在運行時已經註冊了activex。我們的目的是通過網頁自動下載安裝activex。有些開發人員說在伺服器activex運行正常,在客戶機上不能正常運行。很可能是因為activex已經註冊了。問題可能在web上。在本機上運行時應該出現提示是否下載cab檔案。確定下載。運行正常。

五、在客戶機上測試
1.首先保證客戶機能正常訪問你的web
2.在地址欄輸入:http://192.168.1.101/index.htm
此時也提示你是否下載CAB檔案。確定下載,你會在狀態列看到提示。
3.ie下載cab檔案後根據inf檔案安裝檔案,自註冊,初始化,運行Active。
4.OK!

 

參考文獻:

A Complete ActiveX Web Control Tutorial

http://www.codeproject.com/KB/COM/CompleteActiveX.aspx

 

編寫在瀏覽器中不彈出警告的ActiveX控制項

 

http://www.vckbase.com/document/viewdoc/?id=728

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.