在默认环境下,编译的MFC Activex控件,只能在本地代码中运行,即在http://localhost/xxx/xxx.htm中执行,而在http://127.0.0.1/xxx/xxx.htm中提示无相关属性,需要设置其初始化和脚本运行的安全性

ActiveX在远程IE页面上执行,需要实现安全接口。

在ATL写的ActiveX中,用IObjectSafety。

http://support.microsoft.com/kb/168371/en-us

在MFC写的ActiveX中,直接修改注册表。

http://support.microsoft.com/kb/161873/en-us

mfc实现的ocx,要在app实现文件中包括两个文件:
引用:

#include <comcat.h>   
#include <objsafe.h>   
// 本控件的CLSID,注册表用   
const GUID CDECL CLSID_SafeItem ={ 0x12342234, 0x3234, 0x4234,   
                      { 0x52,0x34,0x62,0x34,0x72,0x34,0x82,0x34 } };   
// 版本控制   
const WORD _wVerMajor = 1;   
// 次版本号   
const WORD _wVerMinor = 0;   
实现   
// CTestApp::InitInstance - DLL initialization   
BOOL CTestApp::InitInstance() {   
    return COleControlModule::InitInstance();   
}   
// CTestApp::ExitInstance - DLL termination   
int CTestApp::ExitInstance() {   
    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) {   
    HRESULT hr;   
    AFX_MANAGE_STATE(_afxModuleAddrThis);   
    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))   
        return ResultFromScode(SELFREG_E_TYPELIB);   
    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))   
        return ResultFromScode(SELFREG_E_CLASS);   
    // 标记控件初始化安全.   
    // 创建初始化安全组件种类   
    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) {   
    HRESULT hr;   
    AFX_MANAGE_STATE(_afxModuleAddrThis);   
    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))   
        return ResultFromScode(SELFREG_E_TYPELIB);   
    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))   
        return ResultFromScode(SELFREG_E_CLASS);   
    // 删除控件初始化安全入口.   
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);   
    if (FAILED(hr)) return hr;   
    // 删除控件脚本安全入口   
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);   
    if (FAILED(hr)) return hr;   
    return NOERROR;   
}  

 

现在控件就可以在自注册时就注册为安全控件了.

在修改了IE的安全设置后,依然不能调用控件的方法,需要将访问的站点设置为“受信站点”,则方能正常使用控件
在<html> 和<head>之间插入
<!-- saved from url=(0017)http://localhost/ -->
则在IE窗口上方不会弹出黄色提示条
posted on 2009-07-22 13:22  alonezooo  阅读(1002)  评论(1)    收藏  举报