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.

166 lines
5.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: cobjsaf.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "cobjsaf.h"
  11. // This class provides a simple implementation for IObjectSafety for
  12. // for object that are either always safe or always unsafe for scripting
  13. // and/or initializing with persistent data.
  14. //
  15. // The constructor takes an IUnknown interface on an outer object and delegates
  16. // all IUnknown calls through that object. Because of this, the object must
  17. // be explicitly destroyed using C++ (rather than COM) mechanisms, either by
  18. // using "delete" or by making the object an embedded member of some other class.
  19. //
  20. // The constructor also takes two booleans telling whether the object is safe
  21. // for scripting and initializing from persistent data.
  22. #if 0
  23. // Return the interface setting options on this object
  24. STDMETHODIMP CObjectSafety::GetInterfaceSafetyOptions(
  25. /*IN */ REFIID iid, // Interface that we want options for
  26. /*OUT*/ DWORD * pdwSupportedOptions, // Options meaningful on this interface
  27. /*OUT*/ DWORD * pdwEnabledOptions) // current option values on this interface
  28. {
  29. *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER || INTERFACESAFE_FOR_UNTRUSTED_DATA;
  30. if (iid==IID_IDispatch)
  31. {
  32. *pdwEnabledOptions = m_fSafeForScripting ?
  33. INTERFACESAFE_FOR_UNTRUSTED_CALLER :
  34. 0;
  35. return S_OK;
  36. }
  37. else if (iid==IID_IPersistStorage || iid==IID_IPersistStream)
  38. {
  39. *pdwEnabledOptions = m_fSafeForInitializing ?
  40. INTERFACESAFE_FOR_UNTRUSTED_DATA :
  41. 0;
  42. return S_OK;
  43. }
  44. else
  45. {
  46. return E_NOINTERFACE;
  47. }
  48. }
  49. // Attempt to set the interface setting options on this object.
  50. // Since these are assumed to be fixed, we basically just check
  51. // that the attempted settings are valid.
  52. STDMETHODIMP CObjectSafety::SetInterfaceSafetyOptions(
  53. /*IN */ REFIID iid, // Interface to set options for
  54. /*IN */ DWORD dwOptionsSetMask, // Options to change
  55. /*IN */ DWORD dwEnabledOptions) // New option values
  56. {
  57. // If they haven't asked for anything, we can certainly provide that
  58. if ((dwOptionsSetMask & dwEnabledOptions) == 0)
  59. return S_OK;
  60. if (iid==IID_IDispatch)
  61. {
  62. // Make sure they haven't asked for an option we don't support
  63. if ((dwEnabledOptions & dwOptionsSetMask) != INTERFACESAFE_FOR_UNTRUSTED_CALLER)
  64. return E_FAIL;
  65. return m_fSafeForScripting ? S_OK : E_FAIL;
  66. }
  67. else if (iid==IID_IPersistStorage || iid==IID_IPersistStream)
  68. {
  69. if ((dwEnabledOptions & dwOptionsSetMask) != INTERFACESAFE_FOR_UNTRUSTED_DATA)
  70. return E_FAIL;
  71. return m_fSafeForInitializing ? S_OK : E_FAIL;
  72. }
  73. else
  74. {
  75. return E_NOINTERFACE;
  76. }
  77. }
  78. // Helper function to create a component category and associated description
  79. HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription)
  80. {
  81. ICatRegister* pcr = NULL ;
  82. HRESULT hr = S_OK ;
  83. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  84. NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
  85. if (FAILED(hr))
  86. return hr;
  87. // Make sure the HKCR\Component Categories\{..catid...}
  88. // key is registered
  89. CATEGORYINFO catinfo;
  90. catinfo.catid = catid;
  91. catinfo.lcid = 0x0409 ; // english
  92. // Make sure the provided description is not too long.
  93. // Only copy the first 127 characters if it is
  94. int len = wcslen(catDescription);
  95. if (len>127)
  96. len = 127;
  97. wcsncpy(catinfo.szDescription, catDescription, len);
  98. // Make sure the description is null terminated
  99. catinfo.szDescription[len] = '\0';
  100. hr = pcr->RegisterCategories(1, &catinfo);
  101. pcr->Release();
  102. return hr;
  103. }
  104. #endif
  105. // Helper function to register a CLSID as belonging to a component category
  106. HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
  107. {
  108. // Register your component categories information.
  109. ICatRegister* pcr = NULL ;
  110. HRESULT hr = S_OK ;
  111. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  112. NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
  113. if (SUCCEEDED(hr))
  114. {
  115. // Register this category as being "implemented" by
  116. // the class.
  117. CATID rgcatid[1] ;
  118. rgcatid[0] = catid;
  119. hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);
  120. }
  121. if (pcr != NULL)
  122. pcr->Release();
  123. return hr;
  124. }
  125. // Helper function to unregister a CLSID as belonging to a component category
  126. HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
  127. {
  128. ICatRegister* pcr = NULL ;
  129. HRESULT hr = S_OK ;
  130. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  131. NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);
  132. if (SUCCEEDED(hr))
  133. {
  134. // Unregister this category as being "implemented" by
  135. // the class.
  136. CATID rgcatid[1] ;
  137. rgcatid[0] = catid;
  138. hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);
  139. }
  140. if (pcr != NULL)
  141. pcr->Release();
  142. return hr;
  143. }