控件安全注册方法3选

控件安全注册方法3选

#include <comcat.h>

#include <afxctl.h>

#include <objsafe.h>

/

// DllRegisterServer – Adds entries to the system registry

/* 原来的代码(被注释)

STDAPI DllRegisterServer(void)

{

    // registers object, typelib and all interfaces in typelib

    return _Module.RegisterServer(TRUE);

}

/

// DllUnregisterServer – Removes entries from the system registry

STDAPI DllUnregisterServer(void)

{

    return _Module.UnregisterServer(TRUE);

}

*/

//用途: 注册组件分类

//说明: 组件安全性种类就是通过组件分类的注册才得以设置组件的安全性

HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription){

    //初始化ICatRegister对象实例指针

    ICatRegister* pcr = NULL ;

    HRESULT hr = S_OK ;

    //获得ICatRegister对象实例

//说明: 这个接口原来注册组件分类

    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,

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

    if (FAILED(hr))

  return hr;

    //初始化分类信息

//说明: 确保注册 HKCR/Component Categories/{..catid…}

    CATEGORYINFO catinfo;

    catinfo.catid = catid;

    catinfo.lcid = 0x0409 ; // english

    //说明: 确保提供的描述不会超长, 仅仅复制前127个字符

    int len = wcslen(catDescription);

    if (len>127)

  len = 127;

    wcsncpy(catinfo.szDescription, catDescription, len);

  // 确保描述使用”/0″结束

  catinfo.szDescription[len] = ‘/0’;

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

  pcr->Release();

  return hr;

}

//用途: 在已经存在的组件分类中进行接口类的注册

HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid) {

    ICatRegister* pcr = NULL ;

    HRESULT hr = S_OK ;

//获得CCM的ICatRegister接口

    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,

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

    if (SUCCEEDED(hr))

    {

       //在组件分类之中注册接口类

       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 ;

//获得CCM的ICatRegister接口

    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,

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

    if (SUCCEEDED(hr))

    {

       //注销已存在的组件分类中的接口类

       CATID rgcatid[1] ;

       rgcatid[0] = catid;

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

    }

    if (pcr != NULL)

  pcr->Release();

  return hr;

}

//注册服务器

STDAPI DllRegisterServer(void){

    HRESULT  hr;    // return for safety functions

    AFX_MANAGE_STATE(_afxModuleAddrThis);

/* 原来的代码

    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))

  return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))

  return ResultFromScode(SELFREG_E_CLASS);

*/

    if (FAILED(_Module.RegisterServer(TRUE))){

  AfxMessageBox(“注册类型库失败”);

}

   //创建初始化安全组件分类

    hr = CreateComponentCategory(CATID_SafeForInitializing,

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

    if (FAILED(hr))

  return hr;

//在上面的分组之中注册接口类

    hr = RegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForInitializing);

    if (FAILED(hr))

  return hr;

    //创建脚本编程安全组件分类

    hr = CreateComponentCategory(CATID_SafeForScripting,

  L”Controls safely scriptable!”);

    if (FAILED(hr))

  return hr;

//在上面的分组之中注册接口类

    hr = RegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForScripting);

    if (FAILED(hr))

  return hr;

    return NOERROR;

}

//用途: 反注册服务器

STDAPI DllUnregisterServer(void) {

    HRESULT hr;    // HResult used by Safety Functions

    AFX_MANAGE_STATE(_afxModuleAddrThis);

/* 原来的代码

    if (!AfxOleUnregisterTypeLib(_tlid))

  return ResultFromScode(SELFREG_E_TYPELIB);

    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))

  return ResultFromScode(SELFREG_E_CLASS);

*/

if(FAILED(_Module.RegisterServer(TRUE))){

  AfxMessageBox(“反注册类型库失败”);

}

    //删除注册表入口

    hr=UnRegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForInitializing);

    if (FAILED(hr))

  return hr;

    hr=UnRegisterCLSIDInCategory(CLSID_psSub, CATID_SafeForScripting);

    if (FAILED(hr))

  return hr;

    return NOERROR;

}

或者

class ….

    public IObjectSafetyImpl<CAgent,INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA>,





BEGIN_COM_MAP(CAgent)

    …

    COM_INTERFACE_ENTRY(IObjectSafety)

    …

END_COM_MAP()

或者

//2001.12.07 – 安全注册支持

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}};

….

//2001.12.07 – 安全注册支持

BEGIN_CATEGORY_MAP(CsubAgent)

    IMPLEMENTED_CATEGORY(CATID_SafeForInitializing)

    IMPLEMENTED_CATEGORY(CATID_SafeForScripting)

END_CATEGORY_MAP()

自己挑吧.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/39802.html

(0)
编程小号编程小号
上一篇 2023-07-20
下一篇 2023-07-20

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注