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.

299 lines
9.2 KiB

  1. //------------------------------------------------------------------------------------
  2. //
  3. // File: REGUTILS.CPP
  4. //
  5. // Helper functions that handle reading and writing strings to the system registry.
  6. //
  7. //------------------------------------------------------------------------------------
  8. #include "precomp.hxx"
  9. #pragma hdrstop
  10. BOOL _GetRegValueString( HKEY hkey, LPCTSTR lpszValName, LPTSTR lpszValue, int cchSize );
  11. // our own atoi function so we don't have to link to the C runtimes...
  12. INT AtoI( LPCTSTR pValue )
  13. {
  14. INT i = 0;
  15. INT iSign = 1;
  16. while( pValue && *pValue )
  17. {
  18. if (*pValue == TEXT('-'))
  19. {
  20. iSign = -1;
  21. }
  22. else
  23. {
  24. i = (i*10) + (*pValue - TEXT('0'));
  25. }
  26. pValue++;
  27. }
  28. return (i * iSign);
  29. }
  30. void ItoA ( INT val, LPTSTR buf, UINT radix )
  31. {
  32. LPTSTR p; /* pointer to traverse string */
  33. LPTSTR firstdig; /* pointer to first digit */
  34. TCHAR temp; /* temp char */
  35. INT digval; /* value of digit */
  36. p = buf;
  37. if (val < 0) {
  38. /* negative, so output '-' and negate */
  39. *p++ = TEXT('-');
  40. val = -val;
  41. }
  42. firstdig = p; /* save pointer to first digit */
  43. do {
  44. digval = (val % radix);
  45. val /= radix; /* get next digit */
  46. /* convert to ascii and store */
  47. if (digval > 9)
  48. *p++ = (TCHAR) (digval - 10 + TEXT('a')); /* a letter */
  49. else
  50. *p++ = (TCHAR) (digval + TEXT('0')); /* a digit */
  51. } while (val > 0);
  52. /* We now have the digit of the number in the buffer, but in reverse
  53. order. Thus we reverse them now. */
  54. *p-- = TEXT('\0'); /* terminate string; p points to last digit */
  55. do {
  56. temp = *p;
  57. *p = *firstdig;
  58. *firstdig = temp; /* swap *p and *firstdig */
  59. --p;
  60. ++firstdig; /* advance to next two digits */
  61. } while (firstdig < p); /* repeat until halfway */
  62. }
  63. //------------------------------------------------------------------------------------
  64. //
  65. // IconSet/GetRegValueString()
  66. //
  67. // Versions of Get/SetRegValueString that go to the user classes section.
  68. //
  69. // Returns: success of string setting / retrieval
  70. //
  71. //------------------------------------------------------------------------------------
  72. BOOL IconSetRegValueString(const CLSID* pclsid, LPCTSTR lpszSubKey, LPCTSTR lpszValName, LPCTSTR lpszValue)
  73. {
  74. HKEY hkey;
  75. if (SUCCEEDED(SHRegGetCLSIDKey(*pclsid, lpszSubKey, TRUE, TRUE, &hkey)))
  76. {
  77. DWORD dwRet = SHRegSetPath(hkey, NULL, lpszValName, lpszValue, 0);
  78. RegCloseKey(hkey);
  79. return (dwRet == ERROR_SUCCESS);
  80. }
  81. return FALSE;
  82. }
  83. BOOL IconGetRegValueString(const CLSID* pclsid, LPCTSTR lpszSubKey, LPCTSTR lpszValName, LPTSTR lpszValue, int cchValue)
  84. {
  85. HKEY hkey;
  86. if (SUCCEEDED(SHRegGetCLSIDKey(*pclsid, lpszSubKey, TRUE, FALSE, &hkey)) ||
  87. SUCCEEDED(SHRegGetCLSIDKey(*pclsid, lpszSubKey, FALSE, FALSE, &hkey)))
  88. {
  89. BOOL fRet = _GetRegValueString(hkey, lpszValName, lpszValue, cchValue);
  90. RegCloseKey(hkey);
  91. return fRet;
  92. }
  93. return FALSE;
  94. }
  95. //------------------------------------------------------------------------------------
  96. //
  97. // GetRegValueString()
  98. //
  99. // Just a little helper routine, gets an individual string value from the
  100. // registry and returns it to the caller. Takes care of registry headaches,
  101. // including a paranoid length check before getting the string.
  102. //
  103. // Returns: success of string retrieval
  104. //
  105. //------------------------------------------------------------------------------------
  106. BOOL GetRegValueString( HKEY hMainKey, LPCTSTR lpszSubKey, LPCTSTR lpszValName, LPTSTR lpszValue, int cchValue )
  107. {
  108. HKEY hKey; // cur open key
  109. LONG lRet = RegOpenKeyEx( hMainKey, lpszSubKey, (DWORD)0, KEY_QUERY_VALUE, (PHKEY)&hKey );
  110. if( lRet == ERROR_SUCCESS )
  111. {
  112. BOOL fRet = _GetRegValueString(hKey, lpszValName, lpszValue, cchValue);
  113. // close subkey
  114. RegCloseKey( hKey );
  115. return fRet;
  116. }
  117. return FALSE;
  118. }
  119. BOOL _GetRegValueString( HKEY hKey, LPCTSTR lpszValName, LPTSTR lpszValue, int cchValue )
  120. {
  121. // now do our paranoid check of data size
  122. DWORD dwType;
  123. DWORD dwSize = cchValue * sizeof(*lpszValue);
  124. LONG lRet = RegQueryValueEx( hKey, lpszValName, NULL, &dwType, (LPBYTE)lpszValue, &dwSize );
  125. return ( ERROR_SUCCESS == lRet );
  126. }
  127. //------------------------------------------------------------------------------------
  128. //
  129. // GetRegValueInt()
  130. //
  131. // Just a little helper routine, gets an individual string value from the
  132. // registry and returns it to the caller as an int. Takes care of registry headaches,
  133. // including a paranoid length check before getting the string.
  134. //
  135. // Returns: success of string retrieval
  136. //
  137. //------------------------------------------------------------------------------------
  138. BOOL GetRegValueInt( HKEY hMainKey, LPCTSTR lpszSubKey, LPCTSTR lpszValName, int* piValue )
  139. {
  140. TCHAR szValue[16];
  141. *szValue = NULL;
  142. BOOL bOK = GetRegValueString( hMainKey, lpszSubKey, lpszValName, szValue, 16 );
  143. *piValue = AtoI( szValue );
  144. return bOK;
  145. }
  146. //------------------------------------------------------------------------------------
  147. //
  148. // SetRegValueString()
  149. //
  150. // Just a little helper routine that takes string and writes it to the registry.
  151. //
  152. // Returns: success writing to Registry, should be always TRUE.
  153. //
  154. //------------------------------------------------------------------------------------
  155. BOOL SetRegValueString( HKEY hMainKey, LPCTSTR lpszSubKey, LPCTSTR lpszValName, LPCTSTR lpszValue )
  156. {
  157. HKEY hKey; // cur open key
  158. BOOL bOK = TRUE;
  159. // open this subkey
  160. LONG lRet = RegOpenKeyEx( hMainKey, lpszSubKey, 0, KEY_SET_VALUE, &hKey );
  161. // check that you got a good key here
  162. if( lRet != ERROR_SUCCESS )
  163. {
  164. DWORD dwDisposition;
  165. // **********************************************************************************
  166. // based on the sketchy documentation we have for this Reg* and Error stuff, we're
  167. // guessing that you've ended up here because this totally standard, Windows-defined
  168. // subkey name just doesn't happen to be defined for the current user.
  169. // **********************************************************************************
  170. // SO: Just create this subkey for this user, and maybe it will get used after you create and set it.
  171. // still successful so long as can create new subkey to write to
  172. lRet = RegCreateKeyEx( hMainKey, lpszSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
  173. KEY_SET_VALUE, NULL, &hKey, &dwDisposition );
  174. if( lRet != ERROR_SUCCESS )
  175. {
  176. bOK = FALSE;
  177. }
  178. }
  179. lRet = SHRegSetPath( hKey, NULL, lpszValName, lpszValue, 0);
  180. bOK = bOK && (lRet == ERROR_SUCCESS);
  181. RegCloseKey( hKey );
  182. return (bOK);
  183. }
  184. //------------------------------------------------------------------------------------
  185. //
  186. // SetRegValueInt()
  187. //
  188. // Just a little helper routine that takes an int and writes it as a string to the
  189. // registry.
  190. //
  191. // Returns: success writing to Registry, should be always TRUE.
  192. //
  193. //------------------------------------------------------------------------------------
  194. BOOL SetRegValueInt( HKEY hMainKey, LPCTSTR lpszSubKey, LPCTSTR lpszValName, int iValue )
  195. {
  196. TCHAR lpszValue[16];
  197. ItoA( iValue, lpszValue, 10 );
  198. return SetRegValueString( hMainKey, lpszSubKey, lpszValName, lpszValue );
  199. }
  200. //------------------------------------------------------------------------------------
  201. //
  202. // SetRegValueDword()
  203. //
  204. // Just a little helper routine that takes an dword and writes it to the
  205. // supplied location.
  206. //
  207. // Returns: success writing to Registry, should be always TRUE.
  208. //
  209. //------------------------------------------------------------------------------------
  210. BOOL SetRegValueDword( HKEY hk, LPCTSTR pSubKey, LPCTSTR pValue, DWORD dwVal )
  211. {
  212. HKEY hkey = NULL;
  213. BOOL bRet = FALSE;
  214. if (ERROR_SUCCESS == RegOpenKey( hk, pSubKey, &hkey ))
  215. {
  216. if (ERROR_SUCCESS == RegSetValueEx(hkey, pValue, 0, REG_DWORD, (LPBYTE)&dwVal, sizeof(DWORD)))
  217. {
  218. bRet = TRUE;
  219. }
  220. }
  221. if (hkey)
  222. {
  223. RegCloseKey( hkey );
  224. }
  225. return bRet;
  226. }
  227. //------------------------------------------------------------------------------------
  228. //
  229. // GetRegValueDword()
  230. //
  231. // Just a little helper routine that takes an dword and writes it to the
  232. // supplied location.
  233. //
  234. // Returns: success writing to Registry, should be always TRUE.
  235. //
  236. //------------------------------------------------------------------------------------
  237. DWORD GetRegValueDword( HKEY hk, LPCTSTR pSubKey, LPCTSTR pValue )
  238. {
  239. HKEY hkey = NULL;
  240. DWORD dwVal = REG_BAD_DWORD;
  241. if (ERROR_SUCCESS == RegOpenKey( hk, pSubKey, &hkey ))
  242. {
  243. DWORD dwType, dwSize = sizeof(DWORD);
  244. RegQueryValueEx( hkey, pValue, NULL, &dwType, (LPBYTE)&dwVal, &dwSize );
  245. }
  246. if (hkey)
  247. {
  248. RegCloseKey( hkey );
  249. }
  250. return dwVal;
  251. }