ActiveX控制項的MFC設計之旅-第20步

來源:互聯網
上載者:User
網頁知道的並不多,不過似乎關於ActiveX控制項在網頁中的問題在所有關於ActiveX控制項的問題中所佔的比重相當的大,而其中最常問的就是,網頁中彈出來的安全警告。

關於這個問題,並不複雜,網上搜尋一下可以找到一大片的回答,真正的是天下文章一大抄了。

參考資料:
http://www.newebug.com/article/cpp/2207.shtml
http://www.ccw.com.cn/htm/app/aprog/01_3_29_4.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaxctrl/html/msdn_signmark.asp

嘿,反正是抄,咱也跟著抄吧。

建立一控制項tix

方法一:使用“組件類管理器(Component Categories Manager )”將自身在客戶機器上註冊為安全。

1.在tix.cpp中
#include "objsafe.h"
#include "comcat.h"

2.在tix.cpp中添加控制項的CLSID定義
const GUID CDECL CLSID_SafeItem =
        { 0x06aa1f79, 0x1874, 0x445c, { 0xb9, 0x0a, 0x23, 0x36, 0xb7, 0x39, 0xf2, 0x54 } };
//    注意這個GUID一定要與控制項的CLSID相同!
//    可以從初始化類廠的宏中拷貝並整理
//    IMPLEMENT_OLECREATE_EX(CTixCtrl, "TIX.TixCtrl.1",
//        0x6aa1f79, 0x1874, 0x445c, 0xb9, 0xa, 0x23, 0x36, 0xb7, 0x39, 0xf2, 0x54)

3.在tix.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;
}

4.修改DllRegisterServer和DllUnregisterServer,以在註冊控制項的同時註冊安全性

STDAPI DllRegisterServer(void)
{
    AFX_MANAGE_STATE(_afxModuleAddrThis);

    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
        return ResultFromScode(SELFREG_E_CLASS);

    HRESULT hr;
    // 標記控制項初始化安全.
    // 建立初始化安全性群組件種類
    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)
{
    AFX_MANAGE_STATE(_afxModuleAddrThis);

    HRESULT hr;
    // 刪除控制項初始化安全入口.
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
    if (FAILED(hr))
        return hr;
    // 刪除控制項指令碼安全入口
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
    if (FAILED(hr))
        return hr;

    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
        return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
        return ResultFromScode(SELFREG_E_CLASS);

    return NOERROR;
}

好了,這就OK了,接下來就可以在網頁中試試了

方法二,讓控制項實現IObjectSafety介面

1.先用regsvr32 /u 卸載控制項,其實是取消控制項安全性的註冊,然後注釋掉上面我們添加的在DllRegisterServer和DllUnregisterServer中的控制項安全性方面的註冊代碼。

2.在控制項的tixctl.h中加入嵌套類聲明
    DECLARE_EVENT_MAP()

    BEGIN_INTERFACE_PART(ObjectSafety, IObjectSafety)
        STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD __RPC_FAR *pdwSupportedOptions, DWORD __RPC_FAR *pdwEnabledOptions);
        STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions);
    END_INTERFACE_PART(ObjectSafety)
    DECLARE_INTERFACE_MAP()

別忘了#include "objsafe.h"

3.在控制項的tixctl.cpp中添加嵌套類實現
STDMETHODIMP CTixCtrl::XObjectSafety::GetInterfaceSafetyOptions(
            REFIID riid,
            DWORD __RPC_FAR *pdwSupportedOptions,
            DWORD __RPC_FAR *pdwEnabledOptions)
{
    METHOD_PROLOGUE_EX(CTixCtrl, ObjectSafety)

    if (!pdwSupportedOptions || !pdwEnabledOptions)
    {
        return E_POINTER;
    }

    *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
    *pdwEnabledOptions = 0;

    if (NULL == pThis->GetInterface(&riid))
    {
        TRACE("Requested interface is not supported./n");
        return E_NOINTERFACE;
    }

    // What interface is being checked out anyhow?
    OLECHAR szGUID[39];
    int i = StringFromGUID2(riid, szGUID, 39);

    if (riid == IID_IDispatch)
    {
        // Client wants to know if object is safe for scripting
        *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
        return S_OK;
    }
    else if (riid == IID_IPersistPropertyBag
            || riid == IID_IPersistStreamInit
            || riid == IID_IPersistStorage
            || riid == IID_IPersistMemory)
    {
        // Those are the persistence interfaces COleControl derived controls support
        // as indicated in AFXCTL.H
        // Client wants to know if object is safe for initializing from persistent data
        *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
        return S_OK;
    }
    else
    {
        // Find out what interface this is, and decide what options to enable
        TRACE("We didn’t account for the safety of this interface, and it’s one we support.../n");
        return E_NOINTERFACE;
    }
}

STDMETHODIMP CTixCtrl::XObjectSafety::SetInterfaceSafetyOptions(
        REFIID riid,
        DWORD dwOptionSetMask,
        DWORD dwEnabledOptions)
{
    METHOD_PROLOGUE_EX(CTixCtrl, ObjectSafety)

    OLECHAR szGUID[39];
    // What is this interface anyway?
    // We can do a quick lookup in the registry under HKEY_CLASSES_ROOT/Interface
    int i = StringFromGUID2(riid, szGUID, 39);

    if (0 == dwOptionSetMask && 0 == dwEnabledOptions)
    {
        // the control certainly supports NO requests through the specified interface
        // so it’s safe to return S_OK even if the interface isn’t supported.
        return S_OK;
    }

    // Do we support the specified interface?
    if (NULL == pThis->GetInterface(&riid))
    {
        TRACE1("%s is not support./n", szGUID);
        return E_FAIL;
    }

    if (riid == IID_IDispatch)
    {
        TRACE("Client asking if it’s safe to call through IDispatch./n");
        TRACE("In other words, is the control safe for scripting?/n");
        if (INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_CALLER == dwEnabledOptions)
        {
            return S_OK;
        }
        else
        {
            return E_FAIL;
        }
    }
    else if (riid == IID_IPersistPropertyBag
            || riid == IID_IPersistStreamInit
            || riid == IID_IPersistStorage
            || riid == IID_IPersistMemory)
    {
        TRACE("Client asking if it’s safe to call through IPersist*./n");
        TRACE("In other words, is the control safe for initializing from persistent data?/n");

        if (INTERFACESAFE_FOR_UNTRUSTED_DATA == dwOptionSetMask && INTERFACESAFE_FOR_UNTRUSTED_DATA == dwEnabledOptions)
        {
            return NOERROR;
        }
        else
        {
            return E_FAIL;
        }
    }
    else
    {
        TRACE1("We didn’t account for the safety of %s, and it’s one we support.../n", szGUID);
        return E_FAIL;
    }
}

STDMETHODIMP_(ULONG) CTixCtrl::XObjectSafety::AddRef()
{
    METHOD_PROLOGUE_EX_(CTixCtrl, ObjectSafety)
    return (ULONG)pThis->ExternalAddRef();
}

STDMETHODIMP_(ULONG) CTixCtrl::XObjectSafety::Release()
{
    METHOD_PROLOGUE_EX_(CTixCtrl, ObjectSafety)
    return (ULONG)pThis->ExternalRelease();
}

STDMETHODIMP CTixCtrl::XObjectSafety::QueryInterface(
    REFIID iid, LPVOID* ppvObj)
{
    METHOD_PROLOGUE_EX_(CTixCtrl, ObjectSafety)
    return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}

4.加入介面映射表中
在tixctl.h中加入聲明
    DECLARE_INTERFACE_MAP()
在tixctl.cpp中加入

BEGIN_INTERFACE_MAP(CTixCtrl, COleControl)
    INTERFACE_PART(CTixCtrl, IID_IObjectSafety, ObjectSafety)
END_INTERFACE_MAP()

好了,試試,沒問題了吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.