Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1 KiB

  1. #ifndef __SAFE_H_
  2. #define __SAFE_H_
  3. BOOL
  4. CheckHost(
  5. IUnknown* pObject
  6. );
  7. //
  8. // safe class
  9. //
  10. class CSafeObject {
  11. public:
  12. CSafeObject(IObjectWithSite* pObject) :
  13. m_bInitialized(FALSE),
  14. m_bSafe(FALSE) {
  15. m_pObjectWithSite = pObject;
  16. m_pOleObject = NULL;
  17. }
  18. CSafeObject(IOleObject* pObject) :
  19. m_bInitialized(FALSE),
  20. m_bSafe(FALSE) {
  21. m_pOleObject = pObject;
  22. m_pObjectWithSite = NULL;
  23. }
  24. BOOL operator !() {
  25. if (!m_bInitialized) {
  26. IUnknown* pObject = GetSiteObject();
  27. if (pObject) {
  28. m_bSafe = CheckHost(pObject);
  29. m_bInitialized = TRUE;
  30. pObject->Release();
  31. }
  32. }
  33. return m_bSafe == FALSE;
  34. }
  35. BOOL operator ==(BOOL bSafe) {
  36. if (!m_bInitialized) {
  37. IUnknown* pObject = GetSiteObject();
  38. if (pObject) {
  39. m_bSafe = CheckHost(pObject);
  40. m_bInitialized = TRUE;
  41. pObject->Release();
  42. }
  43. }
  44. return m_bSafe == bSafe;
  45. }
  46. IUnknown* GetSiteObject(VOID) {
  47. IUnknown* pObject = NULL;
  48. HRESULT hr;
  49. if (m_pOleObject) {
  50. CComPtr<IOleClientSite> spClientSite;
  51. hr = m_pOleObject->GetClientSite(&spClientSite);
  52. if (SUCCEEDED(hr)) {
  53. pObject = spClientSite;
  54. pObject->AddRef();
  55. }
  56. } else if (m_pObjectWithSite) {
  57. IUnknown* pUnk = NULL;
  58. hr = m_pObjectWithSite->GetSite(IID_IUnknown,
  59. reinterpret_cast<void**>(&pUnk));
  60. if (SUCCEEDED(hr) && pUnk != NULL) {
  61. pObject = pUnk;
  62. }
  63. }
  64. return pObject;
  65. }
  66. private:
  67. IObjectWithSite* m_pObjectWithSite;
  68. IOleObject* m_pOleObject;
  69. BOOL m_bInitialized;
  70. BOOL m_bSafe;
  71. };
  72. #endif // __SAFE_H_