DllMain.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #include "stdafx.h"
  15. #include "OCOverlayRegistrationHandler.h"
  16. #include "OCOverlayFactory.h"
  17. HINSTANCE instanceHandle = nullptr;
  18. long dllReferenceCount = 0;
  19. BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
  20. {
  21. switch (dwReason)
  22. {
  23. case DLL_PROCESS_ATTACH:
  24. instanceHandle = hModule;
  25. DisableThreadLibraryCalls(hModule);
  26. break;
  27. case DLL_THREAD_ATTACH:
  28. case DLL_THREAD_DETACH:
  29. case DLL_PROCESS_DETACH:
  30. break;
  31. }
  32. return TRUE;
  33. }
  34. HRESULT CreateFactory(REFIID riid, void **ppv, int state)
  35. {
  36. HRESULT hResult = E_OUTOFMEMORY;
  37. OCOverlayFactory* ocOverlayFactory = new OCOverlayFactory(state);
  38. if (ocOverlayFactory) {
  39. hResult = ocOverlayFactory->QueryInterface(riid, ppv);
  40. ocOverlayFactory->Release();
  41. }
  42. return hResult;
  43. }
  44. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
  45. {
  46. HRESULT hResult = CLASS_E_CLASSNOTAVAILABLE;
  47. GUID guid;
  48. hResult = CLSIDFromString(OVERLAY_GUID_ERROR, (LPCLSID)&guid);
  49. if (!SUCCEEDED(hResult)) { return hResult; }
  50. if (IsEqualCLSID(guid, rclsid)) { return CreateFactory(riid, ppv, State_Error); }
  51. hResult = CLSIDFromString(OVERLAY_GUID_OK, (LPCLSID)&guid);
  52. if (!SUCCEEDED(hResult)) { return hResult; }
  53. if (IsEqualCLSID(guid, rclsid)) { return CreateFactory(riid, ppv, State_OK); }
  54. hResult = CLSIDFromString(OVERLAY_GUID_OK_SHARED, (LPCLSID)&guid);
  55. if (!SUCCEEDED(hResult)) { return hResult; }
  56. if (IsEqualCLSID(guid, rclsid)) { return CreateFactory(riid, ppv, State_OKShared); }
  57. hResult = CLSIDFromString(OVERLAY_GUID_SYNC, (LPCLSID)&guid);
  58. if (!SUCCEEDED(hResult)) { return hResult; }
  59. if (IsEqualCLSID(guid, rclsid)) { return CreateFactory(riid, ppv, State_Sync); }
  60. hResult = CLSIDFromString(OVERLAY_GUID_WARNING, (LPCLSID)&guid);
  61. if (!SUCCEEDED(hResult)) { return hResult; }
  62. if (IsEqualCLSID(guid, rclsid)) { return CreateFactory(riid, ppv, State_Warning); }
  63. return CLASS_E_CLASSNOTAVAILABLE;
  64. }
  65. STDAPI DllCanUnloadNow(void)
  66. {
  67. return dllReferenceCount > 0 ? S_FALSE : S_OK;
  68. }
  69. HRESULT RegisterCLSID(LPCOLESTR guidStr, PCWSTR overlayStr, PCWSTR szModule)
  70. {
  71. HRESULT hResult = S_OK;
  72. GUID guid;
  73. hResult = CLSIDFromString(guidStr, (LPCLSID)&guid);
  74. if (hResult != S_OK) {
  75. return hResult;
  76. }
  77. hResult = OCOverlayRegistrationHandler::RegisterCOMObject(szModule, OVERLAY_GENERIC_NAME, guid);
  78. if (!SUCCEEDED(hResult)) {
  79. return hResult;
  80. }
  81. hResult = OCOverlayRegistrationHandler::MakeRegistryEntries(guid, overlayStr);
  82. return hResult;
  83. }
  84. HRESULT UnregisterCLSID(LPCOLESTR guidStr, PCWSTR overlayStr)
  85. {
  86. HRESULT hResult = S_OK;
  87. GUID guid;
  88. hResult = CLSIDFromString(guidStr, (LPCLSID)&guid);
  89. if (hResult != S_OK) {
  90. return hResult;
  91. }
  92. hResult = OCOverlayRegistrationHandler::UnregisterCOMObject(guid);
  93. if (!SUCCEEDED(hResult)) {
  94. return hResult;
  95. }
  96. hResult = OCOverlayRegistrationHandler::RemoveRegistryEntries(overlayStr);
  97. return hResult;
  98. }
  99. HRESULT _stdcall DllRegisterServer(void)
  100. {
  101. HRESULT hResult = S_OK;
  102. wchar_t szModule[MAX_PATH];
  103. if (GetModuleFileName(instanceHandle, szModule, ARRAYSIZE(szModule)) == 0) {
  104. hResult = HRESULT_FROM_WIN32(GetLastError());
  105. return hResult;
  106. }
  107. // Unregister any obsolete CLSID when we register here
  108. // Those CLSID were removed in 2.1, but we need to make sure to prevent any previous version
  109. // of the extension on the system from loading at the same time as a new version to avoid crashing explorer.
  110. UnregisterCLSID(OVERLAY_GUID_ERROR_SHARED, OVERLAY_NAME_ERROR_SHARED);
  111. UnregisterCLSID(OVERLAY_GUID_SYNC_SHARED, OVERLAY_NAME_SYNC_SHARED);
  112. UnregisterCLSID(OVERLAY_GUID_WARNING_SHARED, OVERLAY_NAME_WARNING_SHARED);
  113. hResult = RegisterCLSID(OVERLAY_GUID_ERROR, OVERLAY_NAME_ERROR, szModule);
  114. if (!SUCCEEDED(hResult)) { return hResult; }
  115. hResult = RegisterCLSID(OVERLAY_GUID_OK, OVERLAY_NAME_OK, szModule);
  116. if (!SUCCEEDED(hResult)) { return hResult; }
  117. hResult = RegisterCLSID(OVERLAY_GUID_OK_SHARED, OVERLAY_NAME_OK_SHARED, szModule);
  118. if (!SUCCEEDED(hResult)) { return hResult; }
  119. hResult = RegisterCLSID(OVERLAY_GUID_SYNC, OVERLAY_NAME_SYNC, szModule);
  120. if (!SUCCEEDED(hResult)) { return hResult; }
  121. hResult = RegisterCLSID(OVERLAY_GUID_WARNING, OVERLAY_NAME_WARNING, szModule);
  122. return hResult;
  123. }
  124. STDAPI DllUnregisterServer(void)
  125. {
  126. HRESULT hResult = S_OK;
  127. wchar_t szModule[MAX_PATH];
  128. if (GetModuleFileNameW(instanceHandle, szModule, ARRAYSIZE(szModule)) == 0)
  129. {
  130. hResult = HRESULT_FROM_WIN32(GetLastError());
  131. return hResult;
  132. }
  133. hResult = UnregisterCLSID(OVERLAY_GUID_ERROR, OVERLAY_NAME_ERROR);
  134. if (!SUCCEEDED(hResult)) { return hResult; }
  135. hResult = UnregisterCLSID(OVERLAY_GUID_OK, OVERLAY_NAME_OK);
  136. if (!SUCCEEDED(hResult)) { return hResult; }
  137. hResult = UnregisterCLSID(OVERLAY_GUID_OK_SHARED, OVERLAY_NAME_OK_SHARED);
  138. if (!SUCCEEDED(hResult)) { return hResult; }
  139. hResult = UnregisterCLSID(OVERLAY_GUID_SYNC, OVERLAY_NAME_SYNC);
  140. if (!SUCCEEDED(hResult)) { return hResult; }
  141. hResult = UnregisterCLSID(OVERLAY_GUID_WARNING, OVERLAY_NAME_WARNING);
  142. return hResult;
  143. }