Leaked source code of windows server 2003
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.

298 lines
9.1 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 cchBuff)
  78. {
  79. return GetCurrentUserString(THEMEMGR_REGKEY, pszValueName, pszDefaultValue, pszBuff, cchBuff);
  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. StringCchPrintfW(szKeyName, ARRAYSIZE(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 cchBuff)
  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 = cchBuff * 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 = SafeStringCchCopyW(pszBuff, cchBuff, pszDefaultValue);
  154. if (FAILED(hr))
  155. goto exit;
  156. }
  157. if (dwValType == REG_EXPAND_SZ || wcschr(pszBuff, L'%'))
  158. {
  159. int cchTempBuff = (1 + lstrlen(pszBuff));
  160. LPWSTR pszTempBuff = new WCHAR[cchTempBuff];
  161. if (pszTempBuff)
  162. {
  163. StringCchCopyW(pszTempBuff, cchTempBuff, pszBuff);
  164. DWORD dwChars = ExpandEnvironmentStrings(pszTempBuff, pszBuff, cchBuff);
  165. if (dwChars > cchBuff) // caller's buffer too small
  166. {
  167. hr = MakeError32(ERROR_INSUFFICIENT_BUFFER);
  168. goto exit;
  169. }
  170. delete [] pszTempBuff;
  171. }
  172. }
  173. exit:
  174. RegCloseKey(tmkey);
  175. return hr;
  176. }
  177. //---------------------------------------------------------------------------
  178. HRESULT GetCurrentUserThemeInt(LPCWSTR pszValueName, int iDefaultValue, int *piValue)
  179. {
  180. CCurrentUser hKeyCurrentUser(KEY_READ | KEY_WRITE);
  181. LONG code32;
  182. if (! piValue)
  183. return MakeError32(E_INVALIDARG);
  184. TCHAR valbuff[_MAX_PATH+1];
  185. DWORD dwByteSize = sizeof(valbuff);
  186. RESOURCE HKEY tmkey = NULL;
  187. code32 = RegOpenKeyEx(hKeyCurrentUser, THEMEMGR_REGKEY, NULL, KEY_QUERY_VALUE,
  188. &tmkey);
  189. if (code32 == ERROR_SUCCESS)
  190. {
  191. DWORD dwValType;
  192. code32 = RegQueryValueEx(tmkey, pszValueName, NULL, &dwValType,
  193. (BYTE *)valbuff, &dwByteSize);
  194. }
  195. if (code32 != ERROR_SUCCESS) // call failed - use default value
  196. *piValue = iDefaultValue;
  197. else
  198. {
  199. *piValue = string2number(valbuff);
  200. }
  201. RegCloseKey(tmkey);
  202. return S_OK;
  203. }
  204. //---------------------------------------------------------------------------
  205. HRESULT SetCurrentUserThemeInt(LPCWSTR pszValueName, int iValue)
  206. {
  207. CCurrentUser hKeyCurrentUser(KEY_READ | KEY_WRITE);
  208. TCHAR valbuff[_MAX_PATH+1];
  209. RESOURCE HKEY tmkey = NULL;
  210. LONG code32;
  211. HRESULT hr = S_OK;
  212. //---- create or open existing key ----
  213. code32 = RegCreateKeyEx(hKeyCurrentUser, THEMEMGR_REGKEY, NULL, NULL,
  214. REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &tmkey, NULL);
  215. WIN32_EXIT(code32);
  216. //---- write key value ----
  217. StringCchPrintfW(valbuff, ARRAYSIZE(valbuff), L"%d", iValue);
  218. DWORD len;
  219. len = sizeof(TCHAR)*(1+lstrlen(valbuff));
  220. code32 = RegSetValueEx(tmkey, pszValueName, NULL, REG_SZ,
  221. (BYTE *)valbuff, len);
  222. WIN32_EXIT(code32);
  223. exit:
  224. RegCloseKey(tmkey);
  225. return S_OK;
  226. }
  227. //---------------------------------------------------------------------------
  228. HRESULT DeleteCurrentUserThemeValue(LPCWSTR pszKeyName)
  229. {
  230. CCurrentUser hKeyCurrentUser(KEY_WRITE);
  231. RESOURCE HKEY tmkey = NULL;
  232. LONG code32;
  233. HRESULT hr = S_OK;
  234. //---- create or open existing key ----
  235. code32 = RegCreateKeyEx(hKeyCurrentUser, THEMEMGR_REGKEY, NULL, NULL,
  236. REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &tmkey, NULL);
  237. WIN32_EXIT(code32);
  238. code32 = RegDeleteValue(tmkey, pszKeyName);
  239. WIN32_EXIT(code32);
  240. exit:
  241. RegCloseKey(tmkey);
  242. return hr;
  243. }
  244. //---------------------------------------------------------------------------