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.

296 lines
8.7 KiB

  1. //---------------------------------------------------------------------------
  2. // TmReg.cpp - theme manager registry access routines
  3. //---------------------------------------------------------------------------
  4. #include "stdafx.h"
  5. #include "TmReg.h"
  6. #include "Utils.h"
  7. //---------------------------------------------------------------------------
  8. // --------------------------------------------------------------------------
  9. // CCurrentUser::CCurrentUser
  10. //
  11. // Arguments: samDesired = Desired access to the HKEY.
  12. //
  13. // Returns: <none>
  14. //
  15. // Purpose: Constructor for CCurrentUser. This class transparently allows
  16. // access to HKEY_CURRENT_USER while impersonating a user.
  17. //
  18. // History: 2000-08-11 vtan created
  19. // --------------------------------------------------------------------------
  20. CCurrentUser::CCurrentUser (REGSAM samDesired) :
  21. _hKeyCurrentUser(NULL)
  22. {
  23. (BOOL)RegOpenCurrentUser(samDesired, &_hKeyCurrentUser);
  24. }
  25. // --------------------------------------------------------------------------
  26. // CCurrentUser::~CCurrentUser
  27. //
  28. // Arguments: <none>
  29. //
  30. // Returns: <none>
  31. //
  32. // Purpose: Destructor for CCurrentUser. Close opened resources.
  33. //
  34. // History: 2000-08-11 vtan created
  35. // --------------------------------------------------------------------------
  36. CCurrentUser::~CCurrentUser (void)
  37. {
  38. if (_hKeyCurrentUser != NULL)
  39. {
  40. (LONG)RegCloseKey(_hKeyCurrentUser);
  41. _hKeyCurrentUser = NULL;
  42. }
  43. }
  44. // --------------------------------------------------------------------------
  45. // CCurrentUser::operator HKEY
  46. //
  47. // Arguments: <none>
  48. //
  49. // Returns: HKEY
  50. //
  51. // Purpose: Magical C++ operator to convert object to HKEY.
  52. //
  53. // History: 2000-08-11 vtan created
  54. // --------------------------------------------------------------------------
  55. CCurrentUser::operator HKEY (void) const
  56. {
  57. return(_hKeyCurrentUser);
  58. }
  59. //---------------------------------------------------------------------------
  60. //---------------------------------------------------------------------------
  61. //---------------------------------------------------------------------------
  62. HRESULT SetCurrentUserThemeString(LPCWSTR pszValueName, LPCWSTR pszValue)
  63. {
  64. return SetCurrentUserString(THEMEMGR_REGKEY, pszValueName, pszValue);
  65. }
  66. //---------------------------------------------------------------------------
  67. HRESULT SetCurrentUserThemeStringExpand(LPCWSTR pszValueName, LPCWSTR pszValue)
  68. {
  69. WCHAR szResult[_MAX_PATH + 1];
  70. LPCWSTR pszPath = pszValue;
  71. if (UnExpandEnvironmentString(pszValue, L"%SystemRoot%", szResult, ARRAYSIZE(szResult)))
  72. pszPath = szResult;
  73. return SetCurrentUserThemeString(pszValueName, pszPath);
  74. }
  75. //---------------------------------------------------------------------------
  76. HRESULT GetCurrentUserThemeString(LPCWSTR pszValueName, LPCWSTR pszDefaultValue,
  77. LPWSTR pszBuff, DWORD dwMaxBuffChars)
  78. {
  79. return GetCurrentUserString(THEMEMGR_REGKEY, pszValueName, pszDefaultValue, pszBuff, dwMaxBuffChars);
  80. }
  81. //---------------------------------------------------------------------------
  82. HRESULT SetCurrentUserString(LPCWSTR pszKeyName, LPCWSTR pszValueName, LPCWSTR pszValue)
  83. {
  84. CCurrentUser hKeyCurrentUser(KEY_READ | KEY_WRITE);
  85. RESOURCE HKEY tmkey = NULL;
  86. LONG code32;
  87. HRESULT hr = S_OK;
  88. if (! pszValue)
  89. pszValue = L"";
  90. //---- create or open existing key ----
  91. code32 = RegCreateKeyEx(hKeyCurrentUser, pszKeyName, NULL, NULL,
  92. REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &tmkey, NULL);
  93. WIN32_EXIT(code32);
  94. //---- write key value ----
  95. DWORD len;
  96. len = sizeof(WCHAR)*(1+lstrlen(pszValue));
  97. DWORD dwValType;
  98. dwValType = REG_SZ;
  99. if (wcschr(pszValue, '%'))
  100. dwValType = REG_EXPAND_SZ;
  101. code32 = RegSetValueEx(tmkey, pszValueName, NULL, dwValType, (BYTE *)pszValue, len);
  102. WIN32_EXIT(code32);
  103. exit:
  104. RegCloseKey(tmkey);
  105. return hr;
  106. }
  107. //---------------------------------------------------------------------------
  108. BOOL IsRemoteThemeDisabled()
  109. {
  110. //---- has Terminal Server written a special key to turn themes off ----
  111. //---- for this session? ----
  112. CCurrentUser hKeyCurrentUser(KEY_READ | KEY_WRITE);
  113. BOOL fDisabled = FALSE;
  114. BOOL fRemote = GetSystemMetrics(SM_REMOTESESSION);
  115. if (fRemote) // running TS remote session
  116. {
  117. //---- build the remote key name ----
  118. WCHAR szKeyName[MAX_PATH];
  119. wsprintf(szKeyName, L"%s\\Remote\\%d", THEMEMGR_REGKEY, NtCurrentPeb()->SessionId);
  120. //---- see if the root key exists ----
  121. HKEY tmkey;
  122. LONG code32 = RegOpenKeyEx(hKeyCurrentUser, szKeyName, NULL, KEY_QUERY_VALUE,
  123. &tmkey);
  124. if (code32 == ERROR_SUCCESS)
  125. {
  126. fDisabled = TRUE; // key itself is sufficient
  127. RegCloseKey(tmkey);
  128. }
  129. }
  130. return fDisabled;
  131. }
  132. //---------------------------------------------------------------------------
  133. HRESULT GetCurrentUserString(LPCWSTR pszKeyName, LPCWSTR pszValueName, LPCWSTR pszDefaultValue,
  134. LPWSTR pszBuff, DWORD dwMaxBuffChars)
  135. {
  136. CCurrentUser hKeyCurrentUser(KEY_READ | KEY_WRITE);
  137. HRESULT hr = S_OK;
  138. LONG code32;
  139. RESOURCE HKEY tmkey = NULL;
  140. if (! pszBuff)
  141. return MakeError32(E_INVALIDARG);
  142. DWORD dwByteSize = dwMaxBuffChars * sizeof(WCHAR);
  143. DWORD dwValType = 0;
  144. code32 = RegOpenKeyEx(hKeyCurrentUser, pszKeyName, NULL, KEY_QUERY_VALUE,
  145. &tmkey);
  146. if (code32 == ERROR_SUCCESS)
  147. {
  148. code32 = RegQueryValueEx(tmkey, pszValueName, NULL, &dwValType, (BYTE *)pszBuff,
  149. &dwByteSize);
  150. }
  151. if (code32 != ERROR_SUCCESS) // error - use default value
  152. {
  153. hr = hr_lstrcpy(pszBuff, pszDefaultValue, dwMaxBuffChars);
  154. if (FAILED(hr))
  155. goto exit;
  156. }
  157. if (dwValType == REG_EXPAND_SZ || wcschr(pszBuff, L'%'))
  158. {
  159. int len = sizeof(WCHAR) * (1 + lstrlen(pszBuff));
  160. LPWSTR szTempBuff = (LPWSTR)alloca(len);
  161. if (szTempBuff)
  162. {
  163. lstrcpy(szTempBuff, pszBuff);
  164. DWORD dwChars = ExpandEnvironmentStrings(szTempBuff, pszBuff, dwMaxBuffChars);
  165. if (dwChars > dwMaxBuffChars) // caller's buffer too small
  166. {
  167. hr = MakeError32(ERROR_INSUFFICIENT_BUFFER);
  168. goto exit;
  169. }
  170. }
  171. }
  172. exit:
  173. RegCloseKey(tmkey);
  174. return hr;
  175. }
  176. //---------------------------------------------------------------------------
  177. HRESULT GetCurrentUserThemeInt(LPCWSTR pszValueName, int iDefaultValue, int *piValue)
  178. {
  179. CCurrentUser hKeyCurrentUser(KEY_READ | KEY_WRITE);
  180. LONG code32;
  181. if (! piValue)
  182. return MakeError32(E_INVALIDARG);
  183. TCHAR valbuff[_MAX_PATH+1];
  184. DWORD dwByteSize = sizeof(valbuff);
  185. RESOURCE HKEY tmkey = NULL;
  186. code32 = RegOpenKeyEx(hKeyCurrentUser, THEMEMGR_REGKEY, NULL, KEY_QUERY_VALUE,
  187. &tmkey);
  188. if (code32 == ERROR_SUCCESS)
  189. {
  190. DWORD dwValType;
  191. code32 = RegQueryValueEx(tmkey, pszValueName, NULL, &dwValType,
  192. (BYTE *)valbuff, &dwByteSize);
  193. }
  194. if (code32 != ERROR_SUCCESS) // call failed - use default value
  195. *piValue = iDefaultValue;
  196. else
  197. {
  198. *piValue = string2number(valbuff);
  199. }
  200. RegCloseKey(tmkey);
  201. return S_OK;
  202. }
  203. //---------------------------------------------------------------------------
  204. HRESULT SetCurrentUserThemeInt(LPCWSTR pszValueName, int iValue)
  205. {
  206. CCurrentUser hKeyCurrentUser(KEY_READ | KEY_WRITE);
  207. TCHAR valbuff[_MAX_PATH+1];
  208. RESOURCE HKEY tmkey = NULL;
  209. LONG code32;
  210. HRESULT hr = S_OK;
  211. //---- create or open existing key ----
  212. code32 = RegCreateKeyEx(hKeyCurrentUser, THEMEMGR_REGKEY, NULL, NULL,
  213. REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &tmkey, NULL);
  214. WIN32_EXIT(code32);
  215. //---- write key value ----
  216. wsprintf(valbuff, L"%d", iValue);
  217. DWORD len;
  218. len = sizeof(TCHAR)*(1+lstrlen(valbuff));
  219. code32 = RegSetValueEx(tmkey, pszValueName, NULL, REG_SZ,
  220. (BYTE *)valbuff, len);
  221. WIN32_EXIT(code32);
  222. exit:
  223. RegCloseKey(tmkey);
  224. return S_OK;
  225. }
  226. //---------------------------------------------------------------------------
  227. HRESULT DeleteCurrentUserThemeValue(LPCWSTR pszKeyName)
  228. {
  229. CCurrentUser hKeyCurrentUser(KEY_WRITE);
  230. RESOURCE HKEY tmkey = NULL;
  231. LONG code32;
  232. HRESULT hr = S_OK;
  233. //---- create or open existing key ----
  234. code32 = RegCreateKeyEx(hKeyCurrentUser, THEMEMGR_REGKEY, NULL, NULL,
  235. REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &tmkey, NULL);
  236. WIN32_EXIT(code32);
  237. code32 = RegDeleteValue(tmkey, pszKeyName);
  238. WIN32_EXIT(code32);
  239. exit:
  240. RegCloseKey(tmkey);
  241. return hr;
  242. }
  243. //---------------------------------------------------------------------------