ActiveX控制元件實現安全的初始化和指令碼
阿新 • • 發佈:2019-01-08
要想建立一個能夠在IE中成功載入而沒有“不安全”的警告或者錯誤提示資訊的ActiveX控制元件,我們必須實現安全的初始化和指令碼。基本上,所有要做的工作都是在DllRegisterServer 和DllUnregisterServer這兩個函式中完成的。下面,我們就來一步步地將我們的ActiveX控制元件變成一個“安全的”控制元件。
1. 編輯XXX(此處為控制元件的工程名).cpp並新增如下的程式碼。其中,CLSID_SafeItem的值應該跟XXXCtrl.cpp中的IMPLEMENT_OLECREATE_EX一致,這就等同於你的ActiveX控制元件。同樣,它也應該跟你的HTML頁面中的OBJECT
以下是我工程中為實現安全的初始化和指令碼新增的程式碼,可以與原先IDE自動生成的部分做對比檢視那些部分是新增的,哪些部分是在建立ActiveX控制元件時自動生成的代碼:
[cpp] view plain copy print?- // CCEA.cpp : CCCEAApp 和 DLL 註冊的實現。
- #include "stdafx.h"
- #include "CCEA.h"
- #include "ComCat.h"
- #include "strsafe.h"
- #include "ObjSafe.h"
-
#ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- CCCEAApp theApp;
- /*const GUID CDECL BASED_CODE _tlid =
- { 0x5E5EBDFC, 0x6D73, 0x4652, { 0x85, 0x3, 0x3F, 0xBF, 0x94, 0x7A, 0xCA, 0xD5 } };*/
- const GUID CDECL BASED_CODE _tlid =
- { 0x1f7c5839, 0x4814, 0x4f2b, { 0xbd, 0x9e, 0x81, 0xd6, 0x2b, 0x59, 0x96, 0xaf } };
-
const
- { 0x6f82c754, 0x6c31, 0x43ea, { 0x98, 0x18, 0xe9, 0x5a, 0xd4, 0xe8, 0x72, 0xfc } };
- constWORD _wVerMajor = 1;
- constWORD _wVerMinor = 0;
- // CCCEAApp::InitInstance - DLL 初始化
- BOOL CCCEAApp::InitInstance()
- {
- BOOL bInit = COleControlModule::InitInstance();
- if (bInit)
- {
- // TODO: 在此新增您自己的模組初始化程式碼。
- }
- return bInit;
- }
- // CCCEAApp::ExitInstance - DLL 終止
- int CCCEAApp::ExitInstance()
- {
- // TODO: 在此新增您自己的模組終止程式碼。
- return COleControlModule::ExitInstance();
- }
- //建立Component Categories中的初始化安全和指令碼安全項
- 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;
- // 確認HKCR\Component Categories\{..catid...}鍵值被註冊
- CATEGORYINFO catinfo;
- catinfo.catid = catid;
- catinfo.lcid = 0x0409; // english
- //size_t len;
- // 確認描述不是太長。
- // 只複製開始的127個字元。
- // StringCchLength的第二個引數表示被讀入catDescription的最大字元數。
- // 第三個引數表示字串的長度
- //hr = StringCchLength(catDescription, STRSAFE_MAX_CCH, &len);
- int len = wcslen(catDescription);
- if (SUCCEEDED(hr))
- {
- if (len>127)
- {
- len = 127;
- }
- }
- else
- {
- // TODO: Write an error handler;
- }
- wcsncpy(catinfo.szDescription, catDescription, len);
- //hr = StringCchCopy(catinfo.szDescription, len + 1, catDescription);
- // 新增字串結束符.
- //catinfo.szDescription[len + 1] = '\0';
- catinfo.szDescription[len] = '/0';
- hr = pcr->RegisterCategories(1, &catinfo);
- pcr->Release();
- return hr;
- }
2. 然後需要添加註冊元件分類資訊
同樣是在XXX(此處為控制元件的工程名).cpp並新增如下的程式碼:
[cpp] view plain copy print?- //在CLSID中建立與Component Categories中初始化安全和指令碼安全項中相對應的implemented Categories項
- HRESULT RegisterCLSIDInCategory(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))
- {
- CATID rgcatid[1] ;
- rgcatid[0] = catid;
- hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);
- }
- if (pcr != NULL)
- pcr->Release();
- return hr;
- }
- //登出與CLSID中的相應implemented Categories項,一般用不到,因為其它程式可能也會用到這此項
- 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))
- {
- CATID rgcatid[1] ;
- rgcatid[0] = catid;
- hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);
- }
- if (pcr != NULL)
- pcr->Release();
- return hr;
- }
這兩個方法是全新的必須新增。
3. 需要修改DllRegisterServer函式如下,可以與原先IDE自動生成的部分做對比檢視需要增加的部分:
[cpp] view plain copy print?