-
October 22nd, 2008, 07:15 AM
#1
ActiveX and IObjectSafetySiteLock,IObjectWithSite
hello,
i am trying to create a simple MFC activeX control through wizard.
it has nothing fancy. just a method to display message box
however, i want to add sitelock to this control. for that i downloaded the
Sitelock ATL template. form microsoft.
http://www.microsoft.com/downloads/d...displaylang=en
i added the sitelock.h file.to the mfc control.
now if it was a ATL project them i just need to derive from the "IObjectSafetySiteLockImpl" and things works.
but how do i accomplish it in MFC control.
for this i read some article at MSDN.
http://msdn.microsoft.com/en-us/library/ms974305.aspx
for this i code like this
in the .h file
Code:
BEGIN_INTERFACE_PART(ObjSafe, IObjectSafetySiteLock)
STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid,DWORD dwOptionSetMask, DWORD dwEnabledOptions);
STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid,DWORD __RPC_FAR *pdwSupportedOptions,DWORD __RPC_FAR *pdwEnabledOptions);
STDMETHOD(GetCapabilities)(DWORD * pdwCapability);
STDMETHOD(GetApprovedSites)(const SiteList ** pSiteList, DWORD * cSites);
STDMETHOD(GetExpiryDate)(DWORD * pdwLifespan, FILETIME * pExpiryDate);
END_INTERFACE_PART(ObjSafe);
BEGIN_INTERFACE_PART(ObjWithSite, IObjectWithSite)
STDMETHOD(SetSite)(IUnknown *pUnkSite);
STDMETHOD(GetSite)(REFIID riid, void **ppvSite);
STDMETHOD(SetChildSite)(IUnknown* punkChild);
STDMETHOD(SetChildSite)(IUnknown* punkChild, IUnknown* punkParent);
END_INTERFACE_PART(ObjWithSite);
in the .cpp file
Code:
const DWORD dwSupportedBits = INTERFACESAFE_FOR_UNTRUSTED_CALLER |INTERFACESAFE_FOR_UNTRUSTED_DATA;
const DWORD dwNotSupportedBits = ~ dwSupportedBits;
/////////////////////////////////////////////////////////////////////////////
// CSWEnforceActiveXCtrl::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
CMFCSiteCtrl::XObjSafe::GetInterfaceSafetyOptions(
/* [in] */ REFIID riid,
/* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
/* [out] */ DWORD __RPC_FAR *pdwEnabledOptions)
{
METHOD_PROLOGUE(CMFCSiteCtrl, 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 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
CMFCSiteCtrl::XObjSafe::SetInterfaceSafetyOptions(
/* [in] */ REFIID riid,
/* [in] */ DWORD dwOptionSetMask,
/* [in] */ DWORD dwEnabledOptions)
{
METHOD_PROLOGUE(CMFCSiteCtrl, 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);
}
HRESULT STDMETHODCALLTYPE
CMFCSiteCtrl::XObjSafe::GetCapabilities(DWORD * pdwCapability)
{
// Data validation
if (!pdwCapability)
return E_POINTER;
// Return the version if 0 is passed in
if (0 == *pdwCapability)
{
*pdwCapability = SITELOCK_VERSION;
return S_OK;
}
// Return the options if 1 is passed in
if (1 == *pdwCapability)
{
*pdwCapability =
#ifdef SITELOCK_USE_IOLEOBJECT
Capability::UsesIOleObject |
#endif
#ifndef SITELOCK_NO_EXPIRY
Capability::HasExpiry |
#endif
0;
return S_OK;
}
// Return not implemented otherwise
*pdwCapability = 0;
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE
CMFCSiteCtrl::XObjSafe::GetApprovedSites(const SiteList ** pSiteList, DWORD * pcEntries)
{
// Data validation
if (!pSiteList || !pcEntries)
return E_POINTER;
// Return specified site lock entries
#ifdef SITELOCK_USE_MAP
// Use the site lock map
*pSiteList = T::GetSiteLockMapAndCount(*pcEntries);
#else
// Use the static member
*pSiteList = T::rgslTrustedSites;
*pcEntries = cElements(T::rgslTrustedSites);
#endif
return S_OK;
}
HRESULT STDMETHODCALLTYPE
CMFCSiteCtrl::XObjSafe::GetExpiryDate(DWORD * pdwLifespan, FILETIME * pExpiryDate)
{
if (!pdwLifespan || !pExpiryDate)
return E_POINTER;
#ifdef SITELOCK_NO_EXPIRY
*pdwLifespan = 0;
::ZeroMemory((void*)pExpiryDate, sizeof(FILETIME));
return E_NOTIMPL;
#else
*pdwLifespan = T::dwControlLifespan;
// Calculate expiry date from life span
time_t ttExpire = ImageNtHeaders(&__ImageBase)->FileHeader.TimeDateStamp;
ttExpire += T::dwControlLifespan*86400; // seconds per day
_UNIXTimeToFILETIME(ttExpire, pExpiryDate);
return S_OK;
#endif
}
HRESULT STDMETHODCALLTYPE
CMFCSiteCtrl::XObjWithSite::SetSite(IUnknown *pUnkSite)
{
ATLTRACE(atlTraceCOM, 2, _T("IObjectWithSiteImpl::SetSite\n"));
CMFCSiteCtrl* pT = static_cast<CMFCSiteCtrl*>(this);
pT->m_spUnkSite = pUnkSite;
return S_OK;
}
HRESULT STDMETHODCALLTYPE
CMFCSiteCtrl::XObjWithSite::GetSite(REFIID riid, void **ppvSite)
{
ATLTRACE(atlTraceCOM, 2, _T("IObjectWithSiteImpl::GetSite\n"));
CMFCSiteCtrl* pT = static_cast<CMFCSiteCtrl*>(this);
ATLASSERT(ppvSite);
HRESULT hRes = E_POINTER;
if (ppvSite != NULL)
{
if (pT->m_spUnkSite)
hRes = pT->m_spUnkSite->QueryInterface(riid, ppvSite);
else
{
*ppvSite = NULL;
hRes = E_FAIL;
}
}
return hRes;
}
HRESULT STDMETHODCALLTYPE
CMFCSiteCtrl::XObjWithSite::SetChildSite(IUnknown* punkChild)
{
if (punkChild == NULL)
return E_POINTER;
HRESULT hr;
CComPtr<IObjectWithSite> spChildSite;
hr = punkChild->QueryInterface(__uuidof(IObjectWithSite), (void**)&spChildSite);
if (SUCCEEDED(hr))
hr = spChildSite->SetSite((IUnknown*)this);
return hr;
}
HRESULT STDMETHODCALLTYPE
CMFCSiteCtrl::XObjWithSite::SetChildSite(IUnknown* punkChild, IUnknown* punkParent)
{
return AtlSetChildSite(punkChild, punkParent);
}
some of the code i took from ATL template class
as they are template class for i tried to repalce the "T" with "CMFCSiteCtrl"
somewhere they works
but here is my problem
the sitelock declare some variable of type that a "CMFCSiteCtrl" must declare
they are as follows
static const SiteList rgslTrustedSites[6];
enum { dwControlLifespan =365 };
as you could see this is "SiteList" Structure which is defined in the "Sitelock.h" i get undeclare identifier for it. how do i make it accesible??
i mean how do it get this structure defined in the "Interface" in my MFCCtrl Class??
Any help would be greatly Appreciated
regards
Dingo
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|