Source code of Windows XP (NT5)
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.

127 lines
3.6 KiB

  1. #include <ole2int.h>
  2. STDAPI GetCatalogHelper ( REFIID riid, void** ppv );
  3. // Global catalog reference used in all activation
  4. IComCatalog *gpCatalog = NULL;
  5. IComCatalogSCM *gpCatalogSCM = NULL;
  6. //+-------------------------------------------------------------------------
  7. //
  8. // Function: InitializeCatalogIfNecessary
  9. //
  10. // Synopsis: Load the catalog DLL and initialize the catalog interface
  11. // if it hasn't already been done
  12. //
  13. // Arguments: none
  14. //
  15. // Returns: S_OK - no errors or inconsistencies found
  16. // E_OUTOFMEMORY - couldn't create catalog object
  17. // FAILED(hr) - return from CCI() of catalog or
  18. // from QI() of catalog for IComCatalogSCM
  19. //
  20. //--------------------------------------------------------------------------
  21. HRESULT InitializeCatalogIfNecessary()
  22. {
  23. if (gpCatalog != NULL && gpCatalogSCM != NULL)
  24. {
  25. return S_OK;
  26. }
  27. HRESULT hr = S_OK;
  28. IComCatalog *pCatalog = NULL;
  29. IComCatalogSCM *pCatalogSCM = NULL;
  30. void* pICEPRet = NULL;
  31. // get the actual catalog object - abort on failure
  32. hr = GetCatalogHelper (IID_IComCatalog, (void**) &pCatalog);
  33. if ( FAILED(hr) || pCatalog == NULL)
  34. {
  35. ComDebOut((DEB_WARN,
  36. "CCI of Catalog failed, hr=0x%x, pCatalog=0x%x",
  37. hr, pCatalog));
  38. }
  39. if ( FAILED(hr) )
  40. {
  41. return hr;
  42. }
  43. if ( pCatalog == NULL )
  44. {
  45. return E_OUTOFMEMORY;
  46. }
  47. // store the catalog object. guard against races by
  48. // releasing if we didn't find a NULL in the variable
  49. pICEPRet = InterlockedCompareExchangePointer ( (void **) &gpCatalog, (void *) pCatalog, NULL);
  50. if ( pICEPRet != NULL )
  51. {
  52. pCatalog->Release();
  53. }
  54. // create the SCM object -- again, abort on failure
  55. hr = GetCatalogHelper (IID_IComCatalogSCM, (void**) &pCatalogSCM);
  56. if ( FAILED(hr) || pCatalog == NULL)
  57. {
  58. ComDebOut((DEB_WARN,
  59. "CCI of Catalog failed, hr=0x%x, pCatalog=0x%x",
  60. hr, pCatalog));
  61. }
  62. if ( FAILED(hr) )
  63. {
  64. return hr;
  65. }
  66. if ( pCatalogSCM == NULL )
  67. {
  68. return E_OUTOFMEMORY;
  69. }
  70. // store the SCM catalog. Again, if it was already non-NULL, the exchange
  71. // didn't happen, and you release the thing you just created.
  72. pICEPRet = InterlockedCompareExchangePointer ( (void **) &gpCatalogSCM, (void *) pCatalogSCM, NULL );
  73. if ( pICEPRet != NULL )
  74. {
  75. pCatalogSCM->Release();
  76. }
  77. return S_OK;
  78. }
  79. //+-------------------------------------------------------------------------
  80. //
  81. // Function: UninitializeCatalog
  82. //
  83. // Synopsis: Load the catalog DLL and initialize the catalog interface
  84. // if it hasn't already been done
  85. //
  86. // Arguments: none
  87. //
  88. // Returns: S_OK - no errors or inconsistencies observed here
  89. //
  90. // Note: This can only be called from a place like wCoUninitialize(),
  91. // where you're sure that nobody's using the catalog or doing
  92. // anything out in the process.
  93. //
  94. //--------------------------------------------------------------------------
  95. HRESULT UninitializeCatalog()
  96. {
  97. IComCatalog *pCatalog = NULL;
  98. IComCatalogSCM *pCatalogSCM = NULL;
  99. pCatalogSCM = (IComCatalogSCM *) InterlockedExchangePointer ((PVOID*)&gpCatalogSCM, NULL);
  100. if ( pCatalogSCM != NULL )
  101. {
  102. pCatalogSCM->Release();
  103. }
  104. pCatalog = (IComCatalog *) InterlockedExchangePointer ((PVOID*)&gpCatalog, NULL);
  105. if ( pCatalog != NULL )
  106. {
  107. pCatalog->FlushCache();
  108. pCatalog->Release();
  109. }
  110. return S_OK;
  111. }