?? 有關activex控件安全注冊的例子.txt
字號:
作者:rick1126
email: rickzhang@sina.com
日期:2001-3-26 11:56:16
#include <comcat.h>
#include <afxctl.h>
#include <objsafe.h>
...
/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry
/* 原來的代碼(被注釋)
STDAPI DllRegisterServer(void)
{
// registers object, typelib and all interfaces in typelib
return _Module.RegisterServer(TRUE);
}
/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
return _Module.UnregisterServer(TRUE);
}
*/
//用途: 注冊組件分類
//說明: 組件安全性種類就是通過組件分類的注冊才得以設置組件的安全性
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription){
//初始化ICatRegister對象實例指針
ICatRegister* pcr = NULL ;
HRESULT hr = S_OK ;
//獲得ICatRegister對象實例
//說明: 這個接口原來注冊組件分類
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
//說明: 確保提供的描述不會超長, 僅僅復制前127個字符
int len = wcslen(catDescription);
if (len>127)
len = 127;
wcsncpy(catinfo.szDescription, catDescription, len);
// 確保描述使用"\0"結束
catinfo.szDescription[len] = '\0';
hr = pcr->RegisterCategories(1, &catinfo);
pcr->Release();
return hr;
}
//用途: 在已經存在的組件分類中進行接口類的注冊
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid) {
ICatRegister* pcr = NULL ;
HRESULT hr = S_OK ;
//獲得CCM的ICatRegister接口
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;
}
//用途: 反注冊已存在組件分類中的接口類
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid){
ICatRegister* pcr = NULL ;
HRESULT hr = S_OK ;
//獲得CCM的ICatRegister接口
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;
}
//注冊服務器
STDAPI DllRegisterServer(void){
HRESULT hr; // return for safety functions
AFX_MANAGE_STATE(_afxModuleAddrThis);
/* 原來的代碼
if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
return ResultFromScode(SELFREG_E_CLASS);
*/
if (FAILED(_Module.RegisterServer(TRUE))){
AfxMessageBox("注冊類型庫失敗");
}
//創建初始化安全組件分類
hr = CreateComponentCategory(CATID_SafeForInitializing,
L"Controls safely initializable from persistent data!");
if (FAILED(hr))
return hr;
//在上面的分組之中注冊接口類
hr = RegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForInitializing);
if (FAILED(hr))
return hr;
//創建腳本編程安全組件分類
hr = CreateComponentCategory(CATID_SafeForScripting,
L"Controls safely scriptable!");
if (FAILED(hr))
return hr;
//在上面的分組之中注冊接口類
hr = RegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForScripting);
if (FAILED(hr))
return hr;
return NOERROR;
}
//用途: 反注冊服務器
STDAPI DllUnregisterServer(void) {
HRESULT hr; // HResult used by Safety Functions
AFX_MANAGE_STATE(_afxModuleAddrThis);
/* 原來的代碼
if (!AfxOleUnregisterTypeLib(_tlid))
return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
return ResultFromScode(SELFREG_E_CLASS);
*/
if(FAILED(_Module.RegisterServer(TRUE))){
AfxMessageBox("反注冊類型庫失敗");
}
//刪除注冊表入口
hr=UnRegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForInitializing);
if (FAILED(hr))
return hr;
hr=UnRegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForScripting);
if (FAILED(hr))
return hr;
return NOERROR;
}
注意: 上面的CLSID_XXX就是你的自己的控件的CLSID替代
--
電郵地址: rickzhang@kali.com.cn
主頁地址: www.rickzhang.com
OICQ號碼: 329170
ICQ 號碼: 37175293
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -