NCOverlayRegistrationHandler.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <windows.h>
  16. #include <objbase.h>
  17. #include <iostream>
  18. #include <fstream>
  19. #define REGISTRY_OVERLAY_KEY LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers)"
  20. #define REGISTRY_CLSID L"CLSID"
  21. #define REGISTRY_IN_PROCESS L"InprocServer32"
  22. #define REGISTRY_THREADING L"ThreadingModel"
  23. #define REGISTRY_APARTMENT L"Apartment"
  24. #define REGISTRY_VERSION L"Version"
  25. #define REGISTRY_VERSION_NUMBER L"1.0"
  26. using namespace std;
  27. HRESULT NCOverlayRegistrationHandler::MakeRegistryEntries(const CLSID& clsid, PCWSTR friendlyName)
  28. {
  29. HRESULT hResult = 0;
  30. HKEY shellOverlayKey = nullptr;
  31. // the key may not exist yet
  32. hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_OVERLAY_KEY, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &shellOverlayKey, nullptr));
  33. if (!SUCCEEDED(hResult)) {
  34. hResult = RegCreateKey(HKEY_LOCAL_MACHINE, REGISTRY_OVERLAY_KEY, &shellOverlayKey);
  35. if(!SUCCEEDED(hResult)) {
  36. return hResult;
  37. }
  38. }
  39. HKEY syncExOverlayKey = nullptr;
  40. hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(shellOverlayKey, friendlyName, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &syncExOverlayKey, nullptr));
  41. if (!SUCCEEDED(hResult)) {
  42. return hResult;
  43. }
  44. wchar_t stringCLSID[MAX_PATH];
  45. StringFromGUID2(clsid, stringCLSID, ARRAYSIZE(stringCLSID));
  46. LPCTSTR value = stringCLSID;
  47. hResult = RegSetValueEx(syncExOverlayKey, nullptr, 0, REG_SZ, (LPBYTE)value, (DWORD)((wcslen(value)+1) * sizeof(TCHAR)));
  48. if (!SUCCEEDED(hResult)) {
  49. return hResult;
  50. }
  51. return hResult;
  52. }
  53. HRESULT NCOverlayRegistrationHandler::RemoveRegistryEntries(PCWSTR friendlyName)
  54. {
  55. HRESULT hResult = 0;
  56. HKEY shellOverlayKey = nullptr;
  57. hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGISTRY_OVERLAY_KEY, 0, KEY_WRITE, &shellOverlayKey));
  58. if (!SUCCEEDED(hResult)) {
  59. return hResult;
  60. }
  61. HKEY syncExOverlayKey = nullptr;
  62. hResult = HRESULT_FROM_WIN32(RegDeleteKey(shellOverlayKey, friendlyName));
  63. if (!SUCCEEDED(hResult)) {
  64. return hResult;
  65. }
  66. return hResult;
  67. }
  68. HRESULT NCOverlayRegistrationHandler::RegisterCOMObject(PCWSTR modulePath, PCWSTR friendlyName, const CLSID& clsid)
  69. {
  70. if (!modulePath) {
  71. return E_FAIL;
  72. }
  73. wchar_t stringCLSID[MAX_PATH];
  74. StringFromGUID2(clsid, stringCLSID, ARRAYSIZE(stringCLSID));
  75. HRESULT hResult = 0;
  76. HKEY hKey = nullptr;
  77. hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_CLASSES_ROOT, REGISTRY_CLSID, 0, KEY_WRITE, &hKey));
  78. if (!SUCCEEDED(hResult)) {
  79. return hResult;
  80. }
  81. HKEY clsidKey = nullptr;
  82. hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(hKey, stringCLSID, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &clsidKey, nullptr));
  83. if (!SUCCEEDED(hResult)) {
  84. return hResult;
  85. }
  86. hResult = HRESULT_FROM_WIN32(RegSetValue(clsidKey, nullptr, REG_SZ, friendlyName, (DWORD) wcslen(friendlyName)));
  87. HKEY inprocessKey = nullptr;
  88. hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(clsidKey, REGISTRY_IN_PROCESS, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &inprocessKey, nullptr));
  89. if (!SUCCEEDED(hResult)) {
  90. return hResult;
  91. }
  92. hResult = HRESULT_FROM_WIN32(RegSetValue(inprocessKey, nullptr, REG_SZ, modulePath, (DWORD) wcslen(modulePath)));
  93. if (!SUCCEEDED(hResult)) {
  94. return hResult;
  95. }
  96. hResult = HRESULT_FROM_WIN32(RegSetValueEx(inprocessKey, REGISTRY_THREADING, 0, REG_SZ, (LPBYTE)REGISTRY_APARTMENT, (DWORD)((wcslen(REGISTRY_APARTMENT)+1) * sizeof(TCHAR))));
  97. if (!SUCCEEDED(hResult)) {
  98. return hResult;
  99. }
  100. HKEY versionKey = nullptr;
  101. hResult = HRESULT_FROM_WIN32(RegCreateKeyEx(clsidKey, REGISTRY_VERSION, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &versionKey, nullptr));
  102. if (!SUCCEEDED(hResult)) {
  103. return hResult;
  104. }
  105. hResult = HRESULT_FROM_WIN32(RegSetValueEx(versionKey, nullptr, 0, REG_SZ, (LPBYTE)REGISTRY_VERSION_NUMBER, (DWORD)(wcslen(REGISTRY_VERSION_NUMBER)+1) * sizeof(TCHAR)));
  106. if (!SUCCEEDED(hResult)) {
  107. return hResult;
  108. }
  109. return S_OK;
  110. }
  111. HRESULT NCOverlayRegistrationHandler::UnregisterCOMObject(const CLSID& clsid)
  112. {
  113. wchar_t stringCLSID[MAX_PATH];
  114. StringFromGUID2(clsid, stringCLSID, ARRAYSIZE(stringCLSID));
  115. HRESULT hResult = 0;
  116. HKEY hKey = nullptr;
  117. hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(HKEY_CLASSES_ROOT, REGISTRY_CLSID, 0, DELETE, &hKey));
  118. if (!SUCCEEDED(hResult)) {
  119. return hResult;
  120. }
  121. HKEY clsidKey = nullptr;
  122. hResult = HRESULT_FROM_WIN32(RegOpenKeyEx(hKey, stringCLSID, 0, DELETE, &clsidKey));
  123. if (!SUCCEEDED(hResult)) {
  124. return hResult;
  125. }
  126. hResult = HRESULT_FROM_WIN32(RegDeleteKey(clsidKey, REGISTRY_IN_PROCESS));
  127. if (!SUCCEEDED(hResult)) {
  128. return hResult;
  129. }
  130. hResult = HRESULT_FROM_WIN32(RegDeleteKey(clsidKey, REGISTRY_VERSION));
  131. if (!SUCCEEDED(hResult)) {
  132. return hResult;
  133. }
  134. hResult = HRESULT_FROM_WIN32(RegDeleteKey(hKey, stringCLSID));
  135. if (!SUCCEEDED(hResult)) {
  136. return hResult;
  137. }
  138. return S_OK;
  139. }