In the MFC project environment, make ActiveX OCX to cancel the security prompt of IE browser

Source: Internet
Author: User
Tags microsoft website

Add # pragma once in the next line of the xxxctrl. H (XXX is the project name) header file

// ------- Add ---------------
// Cancel IE Security Prompt
# Include "objsafe. H"
// --------- Add ----------------------------

And declare part of the declaration in the header file class (same as the cxxxctrl () constructor)

// ---------- Added the Security Prompt for canceling ie ----- = ---- declare_interface_map () begin_interface_part (objsafe, IObjectSafety) stdmethod _ (hresult, break) (/* [in] */refiid riid, /* [out] */DWORD _ rpc_far * pdwsupportedoptions,/* [out] */DWORD _ rpc_far * pdwenabledoptions); stdmethod _ (hresult, setinterfacesafetyoptions) (/* [in] */refiid riid,/* [in] */DWORD dwoptionsetmask,/* [in] */DWORD dwenabledoptions); end_interface_part (objsafe ); // --------- add ----------------------------

Implementation file in CPP

After the bool cmax200ctrl: cmax200ctrlfactory: updateregistry (bool bregister) function, add the following

Note that the following code should replace cmax200ctrl with your own class name.

// -------- Add ------------------- // cancel the IE Security Prompt //////////////////////////// //////////////////////////////////////// ///////// interface map for iobjectsafetybegin_interface_map (cmax200ctrl, colecontrol) interface_part (cmax200ctrl, iid_iobjectsafety, objsafe) end_interface_map () //////////////////////////////////////// /// // IObjectSafety member functions // delegate Ddref, release, export far export cmax200ctrl: xobjsafe: addref () {Export (cmax200ctrl, objsafe) return pthis-> externaladdref ();} ulong far export cmax200ctrl: xobjsafe:: release () {method_prologue (cmax200ctrl, objsafe) return pthis-> externalrelease ();} hresult far export cmax200ctrl: xobjsafe: QueryInterface (refiid IID, void far * ppvobj) {method_prologue (cmax200ctrl, Objsafe) Return (hresult) pthis-> externalqueryinterface (& IID, ppvobj);} const DWORD dwsupportedbits = bytes | interfacesafe_for_untrusted_data; const DWORD dwnotsupportedbits = ~ Dwsupportedbits; //////////////////////////////////////// /// // cstoplitectrl:: xobjsafe: getinterfacesafetyoptions // allows container to query what interfaces are safe for what. we're re // optimizing significantly by ignoring which interface the caller is // asking. hresult stdmethodcalltype cmax200ctrl: xobjsafe: getinterfacesafetyoptions (/* [in] */refiid ri ID,/* [out] */DWORD _ rpc_far * pdwsupportedoptions,/* [out] */DWORD _ rpc_far * pdwenabledoptions) {method_prologue (cmax200ctrl, objsafe) hresult retval = resultfromscode (s_ OK); // does interface exist? Iunknown far * punkinterface; retval = pthis-> externalqueryinterface (& riid, (void **) & punkinterface); If (retval! = E_nointerface) {// interface exists punkinterface-> release (); // release it -- just checking!} // We support both kinds of safety and have always both set, // regardless of interface * pdwsupportedoptions = * pdwenabledoptions = dwsupportedbits; return retval; // e_nointerface if Qi Failed }///////////////////////////////// //////////////////////////////////////// ///// cstoplitectrl:: xobjsafe: setinterfacesafetyoptions // since we're always safe, this is a no-brainer -- but we do check to mak E // sure the interface requested exists and that the options we're asked to // set exist and are set on (we don't support unsafe mode ). hresult stdmethodcalltype cmax200ctrl: xobjsafe: Random (/* [in] */refiid riid,/* [in] */DWORD dwoptionsetmask,/* [in] */DWORD dwenabledoptions) {method_prologue (cmax200ctrl, objsafe) // does interface exist? Iunknown far * punkinterface; pthis-> externalqueryinterface (& riid, (void **) & punkinterface); If (punkinterface) {// interface exists punkinterface-> release (); // release it -- just checking!} Else {// interface doesn't exist return resultfromscode (e_nointerface);} // can't set bits we don't support if (dwoptionsetmask & dwnotsupportedbits) {return resultfromscode (e_fail);} // can't set bits we do support to zero dwenabledoptions & = dwsupportedbits; // (we already know there are no extra bits in mask) if (dwoptionsetmask & dwenabledoptions )! = Dwoptionsetmask) {return resultfromscode (e_fail);} // don't need to change anything since we're always safe return resultfromscode (s_ OK);} // ------------- add end ticket --------------------

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////

Official Microsoft website instance

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

We recommend that you use this
Implement the createcomponentcategory and registerclsidincategory helper functions by adding the following cathelp. h and cathelp. cpp files to your project.

  1. Cathelp. h

          #include "comcat.h"      // Helper function to create a component category and associated      // description      HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);      // Helper function to register a CLSID as belonging to a component      // category      HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);
    Cathelp. cpp

          #include "comcat.h"      // Helper function to create a component category and associated      // description      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;      }      // Helper function to register a CLSID as belonging to a component      // category      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;      }
  2. Modify the dllregisterserver to mark the control as safe. locate the implementation of dllregisterserver in. CPP file in your project. you will need to add several things to this. CPP file. include the file that implements createcomponentcategory and
    Registerclsidincategory:

          #include "CatHelp.h"

    Define the guid associated with the safety component categories:

          const CATID CATID_SafeForScripting     =      {0x7dd95801,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};      const CATID CATID_SafeForInitializing  =      {0x7dd95802,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};

    Define the guid associated with your control. For simplicity, you can borrow the guid fromImplement_olecreate_exMacro in the main. cpp file for the control. Adjust the format slightly
    So that it looks like the following:

          const GUID CDECL BASED_CODE _ctlid =      { 0x43bd9e45, 0x328f, 0x11d0,              { 0xa6, 0xb9, 0x0, 0xaa, 0x0, 0xa7, 0xf, 0xc2 } };

    To mark your control as both safe for scripting and initialization, modify the dllregisterserver function as follows:

          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);          if (FAILED( CreateComponentCategory(                  CATID_SafeForScripting,                  L"Controls that are safely scriptable") ))                return ResultFromScode(SELFREG_E_CLASS);          if (FAILED( CreateComponentCategory(                  CATID_SafeForInitializing,                  L"Controls safely initializable from persistent data") ))                return ResultFromScode(SELFREG_E_CLASS);          if (FAILED( RegisterCLSIDInCategory(                  _ctlid, CATID_SafeForScripting) ))                return ResultFromScode(SELFREG_E_CLASS);          if (FAILED( RegisterCLSIDInCategory(                  _ctlid, CATID_SafeForInitializing) ))                return ResultFromScode(SELFREG_E_CLASS);          return NOERROR;      }

You wocould not normally modify the dllunregisterserver function for these two reasons:

  • You wocould not want to remove a component category because other controls may be using it.
  • Although there is an unregisterclsidincategory function defined, by default dllunregisterserver removes the control's entry from the Registry entirely. therefore, removing the category from the control's registration is of little use. ========================================================== ========
<HTML> 

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Another reference

Http://blog.csdn.net/waxgourd0/article/details/7411620

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.