天天看点

ActiveX 安全问题

工作中写了一个MFC ActiveX,测试的时候,发现IE6和IE8修改了安全设置后能够正常运行,IE7和别的浏览器则始终无法正常运行,经过多方查找,发现缺少一些安全信息注册,添加下列代码后能够正常运行了。

 首先定义三个函数:

ActiveX 安全问题

HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription)  

ActiveX 安全问题

{  

ActiveX 安全问题

    ICatRegister *pcr = NULL ;  

ActiveX 安全问题

    HRESULT hr = S_OK ;  

ActiveX 安全问题

    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,   

ActiveX 安全问题

        NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);  

ActiveX 安全问题

    if (FAILED(hr))  

ActiveX 安全问题

        return hr;  

ActiveX 安全问题

    // Make sure the HKCR/Component Categories/{..catid

ActiveX 安全问题

}  

ActiveX 安全问题

    // key is registered.  

ActiveX 安全问题

    CATEGORYINFO catinfo;  

ActiveX 安全问题

    catinfo.catid = catid;  

ActiveX 安全问题

    catinfo.lcid = 0x0409 ; // english  

ActiveX 安全问题

    size_t len;  

ActiveX 安全问题

    // Make sure the provided description is not too long.  

ActiveX 安全问题

    // Only copy the first 127 characters if it is.  

ActiveX 安全问题

    // The second parameter of StringCchLength is the maximum  

ActiveX 安全问题

    // number of characters that may be read into catDescription.  

ActiveX 安全问题

    // There must be room for a NULL-terminator. The third parameter  

ActiveX 安全问题

    // contains the number of characters excluding the NULL-terminator.  

ActiveX 安全问题

    hr = StringCchLength(catDescription, STRSAFE_MAX_CCH, &len);  

ActiveX 安全问题

    if (SUCCEEDED(hr))  

ActiveX 安全问题

    {  

ActiveX 安全问题

        if (len>127)  

ActiveX 安全问题

        {  

ActiveX 安全问题

            len = 127;  

ActiveX 安全问题

        }  

ActiveX 安全问题

    }     

ActiveX 安全问题

    else  

ActiveX 安全问题
ActiveX 安全问题

        // TODO: Write an error handler;  

ActiveX 安全问题

    }  

ActiveX 安全问题

    // The second parameter of StringCchCopy is 128 because you need   

ActiveX 安全问题

    // room for a NULL-terminator.  

ActiveX 安全问题

    hr = StringCchCopy(catinfo.szDescription, len + 1, catDescription);  

ActiveX 安全问题

    // Make sure the description is null terminated.  

ActiveX 安全问题

    catinfo.szDescription[len + 1] = '/0';  

ActiveX 安全问题

    hr = pcr->RegisterCategories(1, &catinfo);  

ActiveX 安全问题

    pcr->Release();  

ActiveX 安全问题

    return hr;  

ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题

HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)

ActiveX 安全问题

{

ActiveX 安全问题

    // Register your component categories information.

ActiveX 安全问题

    ICatRegister *pcr = NULL ;

ActiveX 安全问题

    HRESULT hr = S_OK ;

ActiveX 安全问题

    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, 

ActiveX 安全问题

        NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);

ActiveX 安全问题

    if (SUCCEEDED(hr))

ActiveX 安全问题

    {

ActiveX 安全问题

        // Register this category as being "implemented" by the class.

ActiveX 安全问题

        CATID rgcatid[1] ;

ActiveX 安全问题

        rgcatid[0] = catid;

ActiveX 安全问题

        hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);

ActiveX 安全问题

    }

ActiveX 安全问题

    if (pcr != NULL)

ActiveX 安全问题

        pcr->Release();

ActiveX 安全问题

    return hr;

ActiveX 安全问题

}

ActiveX 安全问题

HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)

ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题

        // Unregister this category as being "implemented" by the class.

ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题

        hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);

ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题

然后在STDAPI DllRegisterServer(void)和STDAPI DllUnregisterServer(void)中添加下列代码:

STDAPI DllRegisterServer(void)中:

ActiveX 安全问题

    HRESULT hr;

ActiveX 安全问题

    hr = CreateComponentCategory(CATID_SafeForInitializing, 

ActiveX 安全问题

        L"Controls safely initializable from persistent data!");

ActiveX 安全问题

    if (FAILED(hr))

ActiveX 安全问题

        return hr;

ActiveX 安全问题

    hr = RegisterCLSIDInCategory(CLSID_SafeItem, 

ActiveX 安全问题

        CATID_SafeForInitializing);

ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题

    // Mark the control as safe for scripting.

ActiveX 安全问题

    hr = CreateComponentCategory(CATID_SafeForScripting, 

ActiveX 安全问题

        L"Controls safely  scriptable!");

ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题

        CATID_SafeForScripting);

ActiveX 安全问题
ActiveX 安全问题

STDAPI DllUnregisterServer(void)中:

ActiveX 安全问题
ActiveX 安全问题

    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, 

ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题
ActiveX 安全问题

其中CLSID_SafeItem就是就是浏览器中使用的clsid。

继续阅读