现在的位置: 首页 > 综合 > 正文

ActiveX安全接口

2014年01月16日 ⁄ 综合 ⁄ 共 4965字 ⁄ 字号 评论关闭

第一步:在aaa.cpp中添加以下代码#include   "comcat.h"  
  #include   "strsafe.h"  
  #include   "objsafe.h"  
  //注意这个地方的值取自aaaCtrl.cpp的IMPLEMENT_OLECREATE_EX   函数中  
  const   CATID   CLSID_SafeItem   =  
  {   0xf83c667e,   0x7ab1,   0x4b6d,   0x84,   0x2e,   0x5d,   0x0,   0xb1,   0x88,   0x48,   0xb2};  
     
     
  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  
          size_t   len;  
          //   Make   sure   the   provided   description   is   not   too   long.  
          //   Only   copy   the   first   127   characters   if   it   is.  
          //   The   second   parameter   of   StringCchLength   is   the   maximum  
          //   number   of   characters   that   may   be   read   into   catDescription.  
          //   There   must   be   room   for   a   NULL-terminator.   The   third   parameter  
          //   contains   the   number   of   characters   excluding   the   NULL-terminator.  
          hr   =   StringCchLength((const   char*)catDescription,   STRSAFE_MAX_CCH,   &len);  
          if   (SUCCEEDED(hr))  
                  {  
                  if   (len>127)  
                      {  
                          len   =   127;  
                      }  
                  }        
          else  
                  {  
                      //   TODO:   Write   an   error   handler;  
                  }  
          //   The   second   parameter   of   StringCchCopy   is   128   because   you   need    
          //   room   for   a   NULL-terminator.  
          hr   =   StringCchCopy((char*)catinfo.szDescription,   len   +   1,   (const   char*)catDescription);  
          //   Make   sure   the   description   is   null   terminated.  
          catinfo.szDescription[len   +   1]   =   '/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;  
  }

第2步:修改aaa.cpp中的DllRegisterServer函数  
  DllRegisterServer(void)  
  {  
          HRESULT   hr;         //   HResult   used   by   Safety   Functions  
     
          AFX_MANAGE_STATE(_afxModuleAddrThis);  
     
          if   (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(),   _tlid))  
              return   ResultFromScode(SELFREG_E_TYPELIB);  
     
          if   (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))  
              return   ResultFromScode(SELFREG_E_CLASS);  
     
          //   Mark   the   control   as   safe   for   initializing.  
                                                                                             
          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;  
     
          //   Mark   the   control   as   safe   for   scripting.  
     
          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;  
  } 

 

第三步:修改aaa.cpp中的DllUnregisterServer函数  
  DllUnregisterServer(void)  
  {  
          HRESULT   hr;         //   HResult   used   by   Safety   Functions  
     
          AFX_MANAGE_STATE(_afxModuleAddrThis);  
     
          if   (!AfxOleUnregisterTypeLib(_tlid,   _wVerMajor,   _wVerMinor))  
              return   ResultFromScode(SELFREG_E_TYPELIB);  
     
          if   (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))  
              return   ResultFromScode(SELFREG_E_CLASS);  
     
          //   Remove   entries   from   the   registry.  
     
          hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,    
                                            CATID_SafeForInitializing);  
          if   (FAILED(hr))  
              return   hr;  
     
          hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,    
                                                  CATID_SafeForScripting);  
          if   (FAILED(hr))  
              return   hr;  
     
          return   NOERROR;  
  }  

抱歉!评论已关闭.