DllMain.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "NCOverlayRegistrationHandler.h"
  15. #include "NCOverlayFactory.h"
  16. #include "WinShellExtConstants.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. auto ncOverlayFactory = new NCOverlayFactory(state);
  38. if (ncOverlayFactory) {
  39. hResult = ncOverlayFactory->QueryInterface(riid, ppv);
  40. ncOverlayFactory->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 = NCOverlayRegistrationHandler::RegisterCOMObject(szModule, OVERLAY_DESCRIPTION, guid);
  78. if (!SUCCEEDED(hResult)) {
  79. return hResult;
  80. }
  81. hResult = NCOverlayRegistrationHandler::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 = NCOverlayRegistrationHandler::UnregisterCOMObject(guid);
  93. if (!SUCCEEDED(hResult)) {
  94. return hResult;
  95. }
  96. hResult = NCOverlayRegistrationHandler::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. hResult = RegisterCLSID(OVERLAY_GUID_ERROR, OVERLAY_NAME_ERROR, szModule);
  108. if (!SUCCEEDED(hResult)) { return hResult; }
  109. hResult = RegisterCLSID(OVERLAY_GUID_OK, OVERLAY_NAME_OK, szModule);
  110. if (!SUCCEEDED(hResult)) { return hResult; }
  111. hResult = RegisterCLSID(OVERLAY_GUID_OK_SHARED, OVERLAY_NAME_OK_SHARED, szModule);
  112. if (!SUCCEEDED(hResult)) { return hResult; }
  113. hResult = RegisterCLSID(OVERLAY_GUID_SYNC, OVERLAY_NAME_SYNC, szModule);
  114. if (!SUCCEEDED(hResult)) { return hResult; }
  115. hResult = RegisterCLSID(OVERLAY_GUID_WARNING, OVERLAY_NAME_WARNING, szModule);
  116. return hResult;
  117. }
  118. STDAPI DllUnregisterServer(void)
  119. {
  120. HRESULT hResult = S_OK;
  121. wchar_t szModule[MAX_PATH];
  122. if (GetModuleFileNameW(instanceHandle, szModule, ARRAYSIZE(szModule)) == 0)
  123. {
  124. hResult = HRESULT_FROM_WIN32(GetLastError());
  125. return hResult;
  126. }
  127. hResult = UnregisterCLSID(OVERLAY_GUID_ERROR, OVERLAY_NAME_ERROR);
  128. if (!SUCCEEDED(hResult)) { return hResult; }
  129. hResult = UnregisterCLSID(OVERLAY_GUID_OK, OVERLAY_NAME_OK);
  130. if (!SUCCEEDED(hResult)) { return hResult; }
  131. hResult = UnregisterCLSID(OVERLAY_GUID_OK_SHARED, OVERLAY_NAME_OK_SHARED);
  132. if (!SUCCEEDED(hResult)) { return hResult; }
  133. hResult = UnregisterCLSID(OVERLAY_GUID_SYNC, OVERLAY_NAME_SYNC);
  134. if (!SUCCEEDED(hResult)) { return hResult; }
  135. hResult = UnregisterCLSID(OVERLAY_GUID_WARNING, OVERLAY_NAME_WARNING);
  136. return hResult;
  137. }