본문 바로가기

프로그래밍

안전한 ActiveX Control 만들기(Safe for scripting)






출처 : http://blog.naver.com/elastica/50031820994

안전하지 못하는 경고또는 에러 메시지 없이 익스플로러에서 ActiveX Control 을 로딩하기 위해서는

Visaul Studio 가 생성해 주는 기본 코드에 안전하게 초기화 하는 코드를 추가할 필요가 있습니다.

 

기본적인 내용은 MSDN 의 "Safe Initialization and Scripting for ActiveX Controls" 에 나와 있지만

인터넷에 올라와 있는 여러가지 글들을 종합하여 수정할 점을  정리(영문과 짬뽕되어 있음)해 봅니다.

 

Visual Studio2005 를 통해 ActiveX Control 을 하나 생성해 보았습니다.

이름은 TestControl 로 정했습니다.

 

위저드를 통해 생성된 코드를 보면 아래와 같습니다.

 

수정되기 전의 코드

// TestControl.cpp : Implementation of CTestControlApp and DLL registration.

#include "stdafx.h"
#include "TestControl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CTestControlApp theApp;

const GUID CDECL BASED_CODE _tlid =
  { 0xE3F25CDE, 0xC9DB, 0x435A, { 0xA2, 0x69, 0xEF, 0xBF, 0xC6, 0xE4, 0x95, 0x79 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;

 

// CTestControlApp::InitInstance - DLL initialization

BOOL CTestControlApp::InitInstance()
{
 BOOL bInit = COleControlModule::InitInstance();

 if (bInit)
 {
  // TODO: Add your own module initialization code here.
 }

 return bInit;
}

 

// CTestControlApp::ExitInstance - DLL termination

int CTestControlApp::ExitInstance()
{
 // TODO: Add your own module termination code here.

 return COleControlModule::ExitInstance();
}

 

// DllRegisterServer - Adds entries to the system registry

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

 return NOERROR;
}

 

// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
 AFX_MANAGE_STATE(_afxModuleAddrThis);

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

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

 return NOERROR;
}

 

아래는 수정된 코드입니다.

수정된 코드는 빨간색으로 표시를 했습니다.

 

// TestControl.cpp : Implementation of CTestControlApp and DLL registration.
#include "stdafx.h"
#include "TestControl.h"

#include "comcat.h"
#include "strsafe.h"
#include "objsafe.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

 

CTestControlApp theApp;

const GUID CDECL BASED_CODE _tlid =
  { 0xE3F25CDE, 0xC9DB, 0x435A, { 0xA2, 0x69, 0xEF, 0xBF, 0xC6, 0xE4, 0x95, 0x79 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;


// CTestControlApp ::InitInstance - DLL initialization

BOOL CTestControlApp ::InitInstance()
{
 BOOL bInit = COleControlModule::InitInstance();

 if (bInit)
 {
  // TODO: Add your own module initialization code here.
 }

 return bInit;
}

 

// CTestControlApp ::ExitInstance - DLL termination

int CTestControlApp ::ExitInstance()
{
 // TODO: Add your own module termination code here.
 return COleControlModule::ExitInstance();
}

 

//기본적으로 생성된 Ctrl(TestControlCtrl.cpp) 의 IMPLEMENT_OLECREATE_EX 을 참조하셔서 추가하시면 됩니다.

const CATID CLSID_SafeItem =
{ 0x8c937105, 0x69dd, 0x47c5,{ 0x96, 0xe1, 0x7, 0x3b, 0xd3, 0x72, 0x44, 0xb9}};
 
//CreateComponentCategory : ActiveX Contrl 을 안전하게 등록하는데 사용하는 함수입니다.

 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(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(catinfo.szDescription, len + 1, catDescription);
    // Make sure the description is null terminated.
    catinfo.szDescription[len + 1] = '\0';
 
    hr = pcr->RegisterCategories(1, &catinfo);
    pcr->Release();
 
    return hr;
}
 
//RegisterCLSIDInCategory : 설치되는 컴포넌트 카테고리 정보를 등록하는 함수입니다.

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;
}
 
// UnRegisterCLSIDInCategory :  레지스트리로부터 엔트리를 제거하는 함수입니다.
 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;
}

// DllRegisterServer - 엔트리를 시스템 레지스트리에 등록하는 기능이 추가되었습니다.

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

 

// 엔트리정보를 시스템 레지스트리로부터 삭제하는 기능이 추가되었습니다.

STDAPI 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);
 
 hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,
                     CATID_SafeForInitializing);
    if (FAILED(hr))
      return hr;
 
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem,
                        CATID_SafeForScripting);
    if (FAILED(hr))
      return hr;

 return NOERROR;
}