IObjectSafety 인터페이스 구현


ActiveX 컨트롤을 맹글때 특별한 코드 추가 없이 위자드로 생성하면
위 그림처럼 “안전하지 않은 ActiveX 컨트롤을 사용하는것을 차단…”
이러한 메세지가 뜨게 된다.

이럴때 IObjectSafety 구현코드를 넣으면 되는데..
-.- 아직은 그 깊게 우케 돌아가는지 모른다..

역시나 ctrl + CV 신공으로~ 열심히 붙여넣으게 되면;;; 으흐흐..

일단 관련코드를 소개하겠다.

물론.. 순수 내 지식은 아니고 ;;

데브피아, 오광섭 님의 미친병아리 서비의 이야기, 늘 보기만 해도

골이 띵한 MSDN 에서 이리저리 뒤져보고 …

머 그런거져;;

xxxCtrl.h

[CODE]#include "ObjSafe.h"[/CODE]

[CODE] DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(ObjSafe, IObjectSafety)
STDMETHOD_(HRESULT, GetInterfaceSafetyOptions) (
/* [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);[/CODE]

xxxCtrl.cpp
ek120000000027.hfk120000000019.cpp

파일출처:미친병아리 http://www.madchick.com

[CODE]#include "helpers.h"[/CODE]

[CODE]const DWORD dwSupportedBits =
INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;
/////////////////////////////////////////////////////////////////////////////
// CxxxCtrl::XObjSafe::GetInterfaceSafetyOptions
// Allows container to query what interfaces are safe for what. We're
// optimizing significantly by ignoring which interface the caller is
// asking for.

HRESULT STDMETHODCALLTYPE
CxxxCtrl::XObjSafe::GetInterfaceSafetyOptions(
/* [in] */ REFIID riid,
/* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
/* [out] */ DWORD __RPC_FAR *pdwEnabledOptions)
{
METHOD_PROLOGUE(CxxxCtrl, 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
}[/CODE]

[CODE]////////////////////////////////////////////////////////////////////////////
// CxxxCtrl::XObjSafe::SetInterfaceSafetyOptions
// since we're always safe, this is a no-brainer-but we do check to make
// 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
CxxxCtrl::XObjSafe::SetInterfaceSafetyOptions(
/* [in] */ REFIID riid,
/* [in] */ DWORD dwOptionSetMask,
/* [in] */ DWORD dwEnabledOptions)
{
METHOD_PROLOGUE(CxxxCtrl, 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);
}[/CODE]

[CODE]/////////////////////////////////////////////////////////////
// Interface map for IObjectSafety
BEGIN_INTERFACE_MAP( CxxxCtrl, COleControl )
INTERFACE_PART(CxxxCtrl, IID_IObjectSafety, ObjSafe)
END_INTERFACE_MAP()[/CODE]

[CODE]/////////////////////////////////////////////////////////////
// IObjectSafety member functions
// Delegate AddRef, Release, QueryInterface
ULONG FAR EXPORT CxxxCtrl::XObjSafe::AddRef()
{
METHOD_PROLOGUE(CxxxCtrl, ObjSafe)
return pThis->ExternalAddRef();
}

ULONG FAR EXPORT CxxxCtrl::XObjSafe::Release()
{
METHOD_PROLOGUE(CxxxCtrl, ObjSafe)
return pThis->ExternalRelease();
}

HRESULT FAR EXPORT CxxxCtrl::XObjSafe::QueryInterface(
REFIID iid, void FAR* FAR* ppvObj)
{
METHOD_PROLOGUE(CxxxCtrl, ObjSafe)
return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}[/CODE]


관련 MSDN :
MSDN Home >
 MSDN Library >
  Win32 and COM Development >
   Component Development >
    ActiveX Controls >
     Technical Articles >
      Signing and Marking ActiveX Controls >

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaxctrl/html/msdn_signmark.asp