internetFeatures.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <main.h>
  2. #include <internetFeatures.h>
  3. #include <shlwapi.h>
  4. #include <strsafe.h>
  5. #define REGISTRY_FEATURE_CONTROL L"Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl"
  6. InternetFeatures::InternetFeatures()
  7. : module(NULL), loadResult(S_FALSE),
  8. CoInternetSetFeatureEnabled(NULL),
  9. CoInternetIsFeatureEnabled(NULL),
  10. processName_(NULL)
  11. {
  12. }
  13. InternetFeatures::~InternetFeatures()
  14. {
  15. if (NULL != module)
  16. {
  17. FreeLibrary(module);
  18. }
  19. if (NULL != processName_)
  20. free(processName_);
  21. }
  22. HRESULT InternetFeatures::LoadModule()
  23. {
  24. if (S_FALSE != loadResult)
  25. return loadResult;
  26. module = LoadLibrary(L"UrlMon.dll");
  27. if (NULL == module)
  28. {
  29. DWORD errorCode = GetLastError();
  30. loadResult = HRESULT_FROM_WIN32(errorCode);
  31. return loadResult;
  32. }
  33. else
  34. {
  35. loadResult = S_OK;
  36. }
  37. CoInternetSetFeatureEnabled = (COINTERNETSETFEATUREENABLED)GetProcAddress(module, "CoInternetSetFeatureEnabled");
  38. CoInternetIsFeatureEnabled = (COINTERNETISFEATUREENABLED)GetProcAddress(module, "CoInternetIsFeatureEnabled");
  39. return loadResult;
  40. }
  41. const wchar_t *InternetFeatures::GetProcessName()
  42. {
  43. if (NULL == processName_)
  44. {
  45. wchar_t buffer[2*MAX_PATH] = {0};
  46. unsigned long length = GetModuleFileNameW(NULL, buffer, ARRAYSIZE(buffer));
  47. if (0 == length)
  48. return NULL;
  49. const wchar_t *fileName = PathFindFileName(buffer);
  50. length = lstrlenW(fileName);
  51. processName_ = (wchar_t *)calloc((length + 1), sizeof(wchar_t));
  52. if (NULL == processName_)
  53. return NULL;
  54. memcpy(processName_, fileName, sizeof(wchar_t)*length);
  55. processName_[length]=L'\0';
  56. }
  57. return processName_;
  58. }
  59. HRESULT InternetFeatures::SetEnabled(INTERNETFEATURELIST FeatureEntry, DWORD dwFlags, BOOL fEnable)
  60. {
  61. HRESULT hr = LoadModule();
  62. if (FAILED(hr)) return hr;
  63. if (NULL == CoInternetSetFeatureEnabled) return E_NOINTERFACE;
  64. return CoInternetSetFeatureEnabled(FeatureEntry, dwFlags, fEnable);
  65. }
  66. HRESULT InternetFeatures::IsEnabled(INTERNETFEATURELIST FeatureEntry, DWORD dwFlags)
  67. {
  68. HRESULT hr = LoadModule();
  69. if (FAILED(hr)) return hr;
  70. if (NULL == CoInternetIsFeatureEnabled) return E_NOINTERFACE;
  71. return CoInternetIsFeatureEnabled(FeatureEntry, dwFlags);
  72. }
  73. HRESULT InternetFeatures::SetDWORDFeature(const wchar_t *featureName, BOOL perUser, unsigned long value)
  74. {
  75. if (NULL == featureName
  76. || L'\0' == *featureName)
  77. {
  78. return E_INVALIDARG;
  79. }
  80. const wchar_t *processName = GetProcessName();
  81. if (NULL == processName)
  82. return E_UNEXPECTED;
  83. HKEY rootKey = (FALSE != perUser) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  84. HKEY key = NULL;
  85. unsigned long disposition = 0;
  86. long errorCode = 0;
  87. HRESULT result = S_OK;
  88. wchar_t buffer[MAX_PATH] = {0};
  89. if (FAILED(StringCchPrintf(buffer, ARRAYSIZE(buffer),
  90. REGISTRY_FEATURE_CONTROL L"\\" L"%s",
  91. featureName)))
  92. {
  93. return E_OUTOFMEMORY;
  94. }
  95. errorCode = RegCreateKeyEx(rootKey, buffer,
  96. NULL, NULL, REG_OPTION_VOLATILE, KEY_WRITE,
  97. NULL, &key, &disposition);
  98. if (ERROR_SUCCESS != errorCode)
  99. return HRESULT_FROM_WIN32(errorCode);
  100. errorCode = RegSetValueEx(key, processName, 0, REG_DWORD, (const BYTE*)&value,
  101. sizeof(unsigned long));
  102. if (ERROR_SUCCESS != errorCode)
  103. {
  104. result = HRESULT_FROM_WIN32(errorCode);
  105. }
  106. else
  107. {
  108. result = S_OK;
  109. }
  110. RegCloseKey(key);
  111. return result;
  112. }
  113. HRESULT InternetFeatures::GetDWORDFeature(const wchar_t *featureName, BOOL perUser, unsigned long *value)
  114. {
  115. if (NULL == featureName
  116. || L'\0' == *featureName)
  117. {
  118. return E_INVALIDARG;
  119. }
  120. if (NULL == value)
  121. return E_POINTER;
  122. const wchar_t *processName = GetProcessName();
  123. if (NULL == processName)
  124. return E_UNEXPECTED;
  125. HKEY rootKey = (FALSE != perUser) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  126. HKEY key = NULL;
  127. long errorCode = 0;
  128. HRESULT result = S_OK;
  129. unsigned long valueType = 0;
  130. unsigned long valueSize = 0;
  131. wchar_t buffer[MAX_PATH] = {0};
  132. if (FAILED(StringCchPrintf(buffer, ARRAYSIZE(buffer),
  133. REGISTRY_FEATURE_CONTROL L"\\" L"%s",
  134. featureName)))
  135. {
  136. return E_OUTOFMEMORY;
  137. }
  138. errorCode = RegOpenKeyEx(rootKey, buffer, 0, KEY_READ, &key);
  139. if (ERROR_SUCCESS != errorCode)
  140. return HRESULT_FROM_WIN32(errorCode);
  141. valueSize = sizeof(unsigned long);
  142. errorCode = RegQueryValueEx(key, processName, 0, &valueType,
  143. (BYTE*)value, &valueSize);
  144. if (ERROR_SUCCESS != errorCode)
  145. {
  146. result = HRESULT_FROM_WIN32(errorCode);
  147. }
  148. else
  149. {
  150. if (REG_DWORD != valueType)
  151. result = E_UNEXPECTED;
  152. else
  153. result = S_OK;
  154. }
  155. RegCloseKey(key);
  156. return result;
  157. }
  158. void InternetFeatures::DeleteFeature(const wchar_t *featureName, BOOL perUser)
  159. {
  160. if (NULL == featureName
  161. || L'\0' == *featureName)
  162. {
  163. return;
  164. }
  165. const wchar_t *processName = GetProcessName();
  166. if (NULL == processName)
  167. return;
  168. HKEY rootKey = (FALSE != perUser) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
  169. HKEY key = NULL;
  170. long errorCode = 0;
  171. wchar_t buffer[MAX_PATH] = {0};
  172. if (FAILED(StringCchPrintf(buffer, ARRAYSIZE(buffer),
  173. REGISTRY_FEATURE_CONTROL L"\\" L"%s",
  174. featureName)))
  175. {
  176. return;
  177. }
  178. errorCode = RegOpenKeyEx(rootKey, buffer, 0, KEY_WRITE, &key);
  179. if (ERROR_SUCCESS != errorCode)
  180. return;
  181. RegDeleteValue(key, processName);
  182. RegCloseKey(key);
  183. }