您的位置:首页 > 其它

如何将MFC ActiveX控件标记为安全

2007-07-04 14:18 483 查看
在**Ctrl.cpp中增加如下函数

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

修改UpdateRegistry函数

BOOL testOCXCtrl::testOCXCtrlFactory::UpdateRegistry(BOOL bRegister)
{
// TODO: 验证您的控件是否符合单元模型线程处理规则。
// 有关更多信息,请参考 MFC 技术说明 64。
// 如果您的控件不符合单元模型规则,则
// 必须修改如下代码,将第六个参数从
// afxRegApartmentThreading 改为 0。

/*if (bRegister)
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_SIPXTOCX,
IDB_SIPXTOCX,
afxRegApartmentThreading,
_dwsipXtOCXOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
else
return AfxOleUnregisterClass(m_clsid, m_lpszProgID);*/
if (bRegister)
{
HRESULT hr = S_OK ;

// register as safe for scripting
hr = CreateComponentCategory(CATID_SafeForScripting,
L"Controls that are safely scriptable");
if(FAILED(hr))
return FALSE;
hr = RegisterCLSIDInCategory(m_clsid, CATID_SafeForScripting);
if(FAILED(hr))
return FALSE;
// register as safe for initializing
hr =CreateComponentCategory(CATID_SafeForInitializing,
L"Controls safely initializable from persistent data");
if(FAILED(hr))
return FALSE;

hr = RegisterCLSIDInCategory(m_clsid, CATID_SafeForInitializing);

if (FAILED(hr))
return FALSE;
return AfxOleRegisterControlClass(
AfxGetInstanceHandle(),
m_clsid,
m_lpszProgID,
IDS_SIPXTOCX,
IDB_SIPXTOCX,
afxRegInsertable | afxRegApartmentThreading,
_dwsipXtOCXOleMisc,
_tlid,
_wVerMajor,
_wVerMinor);
}
else
{
HRESULT hr = S_OK ;

hr=UnRegisterCLSIDInCategory(m_clsid,CATID_SafeForScripting);

if(FAILED(hr))
return FALSE;

hr=UnRegisterCLSIDInCategory(m_clsid, CATID_SafeForInitializing);

if(FAILED(hr))
return FALSE;

return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: