reg.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 10k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2.  * reg - registry wrappers
  3.  */
  4. #include "tweakui.h"
  5. #pragma BEGIN_CONST_DATA
  6. #pragma END_CONST_DATA
  7. /*****************************************************************************
  8.  *
  9.  *  RegCanModifyKey
  10.  *
  11.  * Returns nonzero if the current user has permission to modify the
  12.  * key.
  13.  *
  14.  *****************************************************************************/
  15. BOOL PASCAL
  16. RegCanModifyKey(HKEY hkRoot, LPCTSTR ptszSubkey)
  17. {
  18.     BOOL fRc;
  19.     if (g_fNT) {
  20. HKEY hk;
  21. DWORD dw;
  22. if (RegCreateKeyEx(hkRoot, ptszSubkey, 0, c_tszNil,
  23.    REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hk,
  24.    &dw) == 0) {
  25.     RegCloseKey(hk);
  26.     fRc = 1;
  27. } else {
  28.     fRc = 0;
  29. }
  30.     } else {
  31. fRc = 1;
  32.     }
  33.     return fRc;
  34. }
  35. /*****************************************************************************
  36.  *
  37.  *  _RegOpenKey
  38.  *
  39.  * Special version for NT that always asks for MAXIMUM_ALLOWED.
  40.  *
  41.  *****************************************************************************/
  42. LONG PASCAL
  43. _RegOpenKey(HKEY hk, LPCTSTR ptszSubKey, PHKEY phkResult)
  44. {
  45.     return RegOpenKeyEx(hk, ptszSubKey, 0, MAXIMUM_ALLOWED, phkResult);
  46. }
  47. /*****************************************************************************
  48.  *
  49.  *  _RegCreateKey
  50.  *
  51.  * Special version for NT that always asks for MAXIMUM_ALLOWED.
  52.  *
  53.  *****************************************************************************/
  54. LONG PASCAL
  55. _RegCreateKey(HKEY hk, LPCTSTR ptszSubKey, PHKEY phkResult)
  56. {
  57.     DWORD dw;
  58.     if (ptszSubKey) {
  59. return RegCreateKeyEx(hk, ptszSubKey, 0, c_tszNil,
  60.       REG_OPTION_NON_VOLATILE, MAXIMUM_ALLOWED, 0,
  61.       phkResult, &dw);
  62.     } else {
  63. return RegOpenKey(hk, ptszSubKey, phkResult);
  64.     }
  65. }
  66. /*****************************************************************************
  67.  *
  68.  *  RegDeleteValues
  69.  *
  70.  *  Deletes all the values under a key.
  71.  *
  72.  *****************************************************************************/
  73. void PASCAL
  74. RegDeleteValues(HKEY hkRoot, LPCTSTR ptszSubkey)
  75. {
  76.     HKEY hk;
  77.     if (_RegOpenKey(hkRoot, ptszSubkey, &hk) == 0) {
  78. DWORD dw, ctch;
  79. TCHAR tszValue[ctchKeyMax];
  80. dw = 0;
  81. while (ctch = cA(tszValue),
  82.        RegEnumValue(hk, dw, tszValue, &ctch, 0, 0, 0, 0) == 0) {
  83.     if (RegDeleteValue(hk, tszValue) == 0) {
  84.     } else {
  85. dw++;
  86.     }
  87. }
  88. RegCloseKey(hk);
  89.     }
  90. }
  91. /*****************************************************************************
  92.  *
  93.  *  RegDeleteTree
  94.  *
  95.  *  Deletes an entire registry tree.
  96.  *
  97.  *  Windows 95's RegDeleteKey will delete an entire tree, but Windows NT
  98.  *  forces you to do it yourself.
  99.  *
  100.  *  Note that you need to watch out for the case where a key is undeletable,
  101.  *  in which case you must skip over the key and continue as best you can.
  102.  *
  103.  *****************************************************************************/
  104. LONG PASCAL
  105. RegDeleteTree(HKEY hkRoot, LPCTSTR ptszSubkey)
  106. {
  107.     HKEY hk;
  108.     LONG lRc;
  109.     lRc = RegOpenKey(hkRoot, ptszSubkey, &hk);
  110.     if (lRc == 0) {
  111. DWORD dw;
  112. TCHAR tszKey[ctchKeyMax];
  113. dw = 0;
  114. while (RegEnumKey(hk, dw, tszKey, cA(tszKey)) == 0) {
  115.     if (RegDeleteTree(hk, tszKey) == 0) {
  116.     } else {
  117. dw++;
  118.     }
  119. }
  120. RegCloseKey(hk);
  121. lRc = RegDeleteKey(hkRoot, ptszSubkey);
  122. if (lRc == 0) {
  123. } else { /* Couldn't delete the key; at least nuke the values */
  124.     RegDeleteValues(hkRoot, ptszSubkey);
  125. }
  126.     }
  127.     return lRc;
  128. }
  129. /*****************************************************************************
  130.  *
  131.  *  hkOpenClsid
  132.  *
  133.  * Open a class id (guid) registry key, returning the hkey.
  134.  *
  135.  *****************************************************************************/
  136. HKEY PASCAL
  137. hkOpenClsid(PCTSTR ptszClsid)
  138. {
  139.     HKEY hk = 0;
  140.     _RegOpenKey(pcdii->hkClsid, ptszClsid, &hk);
  141.     return hk;
  142. }
  143. /*****************************************************************************
  144.  *
  145.  *  GetRegStr
  146.  *
  147.  * Generic wrapper that sucks out a registry key/subkey.
  148.  *
  149.  *****************************************************************************/
  150. BOOL PASCAL
  151. GetRegStr(HKEY hkRoot, LPCTSTR ptszKey, LPCTSTR ptszSubkey,
  152.   LPTSTR ptszBuf, int cbBuf)
  153. {
  154.     HKEY hk;
  155.     BOOL fRc;
  156.     if ((UINT)cbBuf >= cbCtch(1)) {
  157. ptszBuf[0] = TEXT('');
  158.     }
  159.     if (hkRoot && _RegOpenKey(hkRoot, ptszKey, &hk) == 0) {
  160. fRc = RegQueryValueEx(hk, ptszSubkey, 0, 0, ptszBuf, &cbBuf) == 0;
  161. RegCloseKey(hk);
  162.     } else {
  163. fRc = 0;
  164.     }
  165.     return fRc;
  166. }
  167. /*****************************************************************************
  168.  *
  169.  *  GetStrPkl
  170.  *
  171.  * Read a registry key/subkey/string value given a key location.
  172.  *
  173.  *****************************************************************************/
  174. BOOL PASCAL
  175. GetStrPkl(LPTSTR ptszBuf, int cbBuf, PKL pkl)
  176. {
  177.     return GetRegStr(*pkl->phkRoot, pkl->ptszKey, pkl->ptszSubkey,
  178.      ptszBuf, cbBuf);
  179. }
  180. /*****************************************************************************
  181.  *
  182.  *  GetRegDword
  183.  *
  184.  * Read a dword, returning the default if unable.
  185.  *
  186.  *****************************************************************************/
  187. DWORD PASCAL
  188. GetRegDword(HHK hhk, LPCSTR pszKey, LPCSTR pszSubkey, DWORD dwDefault)
  189. {
  190.     DWORD dw;
  191.     if (GetRegStr(hkeyHhk(hhk), pszKey, pszSubkey, (LPBYTE)&dw, sizeof(dw))) {
  192. return dw;
  193.     } else {
  194. return dwDefault;
  195.     }
  196. }
  197. /*****************************************************************************
  198.  *
  199.  *  GetDwordPkl
  200.  *
  201.  * Given a location, read a dword, returning the default if unable.
  202.  *
  203.  *****************************************************************************/
  204. DWORD PASCAL
  205. GetDwordPkl(PKL pkl, DWORD dwDefault)
  206. {
  207.     DWORD dw;
  208.     if (GetRegStr(*pkl->phkRoot, pkl->ptszKey,
  209.   pkl->ptszSubkey, (LPBYTE)&dw, sizeof(dw))) {
  210. return dw;
  211.     } else {
  212. return dwDefault;
  213.     }
  214. }
  215. /*****************************************************************************
  216.  *
  217.  *  GetRegInt
  218.  *
  219.  * Generic wrapper that sucks out a registry key/subkey as an unsigned.
  220.  *
  221.  *****************************************************************************/
  222. UINT PASCAL
  223. GetRegInt(HHK hhk, LPCSTR pszKey, LPCSTR pszSubkey, UINT uiDefault)
  224. {
  225.     TCH tsz[20];
  226.     if (GetRegStr(hkeyHhk(hhk), pszKey, pszSubkey, tsz, cA(tsz))) {
  227. int i = iFromPtsz(tsz);
  228. return i == iErr ? uiDefault : (UINT)i;
  229.     } else {
  230. return uiDefault;
  231.     }
  232. }
  233. /*****************************************************************************
  234.  *
  235.  *  GetIntPkl
  236.  *
  237.  * Generic wrapper that sucks out a registry key/subkey as an unsigned.
  238.  *
  239.  *****************************************************************************/
  240. UINT PASCAL
  241. GetIntPkl(UINT uiDefault, PKL pkl)
  242. {
  243.     return GetRegInt(*pkl->phkRoot, pkl->ptszKey, pkl->ptszSubkey, uiDefault);
  244. }
  245. /*****************************************************************************
  246.  *
  247.  *  RegSetValuePtsz
  248.  *
  249.  * Generic wrapper that writes out a registry key/subkey as a string.
  250.  *
  251.  *****************************************************************************/
  252. void PASCAL
  253. RegSetValuePtsz(HKEY hk, LPCSTR pszSubkey, LPCTSTR ptszVal)
  254. {
  255.     RegSetValueEx(hk, pszSubkey, 0, REG_SZ, (LPBYTE)ptszVal,
  256.   1 + lstrlen(ptszVal));
  257. }
  258. /*****************************************************************************
  259.  *
  260.  *  SetRegStr
  261.  *
  262.  * Generic wrapper that writes out a registry key/subkey.
  263.  *
  264.  * It is an error to call this with a bad hhk.
  265.  *
  266.  *****************************************************************************/
  267. void PASCAL
  268. SetRegStr(HHK hhk, LPCTSTR ptszKey, LPCTSTR ptszSubkey, LPCTSTR ptszVal)
  269. {
  270.     HKEY hk;
  271.     if (RegCreateKey(hkeyHhk(hhk), ptszKey, &hk) == 0) {
  272. RegSetValuePtsz(hk, ptszSubkey, ptszVal);
  273. RegCloseKey(hk);
  274.     }
  275. }
  276. /*****************************************************************************
  277.  *
  278.  *  SetStrPkl
  279.  *
  280.  * Set a registry key/subkey/string value given a key location.
  281.  *
  282.  * It is an error to call this with a bad hkRoot.
  283.  *
  284.  *****************************************************************************/
  285. void PASCAL
  286. SetStrPkl(PKL pkl, LPCTSTR ptszVal)
  287. {
  288.     SetRegStr(*pkl->phkRoot, pkl->ptszKey, pkl->ptszSubkey, ptszVal);
  289. }
  290. /*****************************************************************************
  291.  *
  292.  *  SetRegInt
  293.  *
  294.  * Generic wrapper that writes out a registry key/subkey as an
  295.  * unsigned integer.
  296.  *
  297.  *****************************************************************************/
  298. void PASCAL
  299. SetRegInt(HHK hhk, LPCSTR pszKey, LPCSTR pszSubkey, UINT ui)
  300. {
  301.     TCH tsz[20];
  302.     wsprintf(tsz, c_tszPercentU, ui);
  303.     SetRegStr(hhk, pszKey, pszSubkey, tsz);
  304. }
  305. /*****************************************************************************
  306.  *
  307.  *  SetIntPkl
  308.  *
  309.  * Writes out a registry key/subkey as an unsigned integer.
  310.  *
  311.  *****************************************************************************/
  312. void PASCAL
  313. SetIntPkl(UINT ui, PKL pkl)
  314. {
  315.     SetRegInt(*pkl->phkRoot, pkl->ptszKey, pkl->ptszSubkey, ui);
  316. }
  317. /*****************************************************************************
  318.  *
  319.  *  SetRegDword
  320.  *
  321.  * Generic wrapper that writes out a registry key/subkey as a
  322.  * dword.
  323.  *
  324.  *****************************************************************************/
  325. void PASCAL
  326. SetRegDword(HHK hhk, LPCSTR pszKey, LPCSTR pszSubkey, DWORD dw)
  327. {
  328.     HKEY hk;
  329.     if (RegCreateKey(hkeyHhk(hhk), pszKey, &hk) == 0) {
  330. /* Bad prototype for RegSetValueEx forces me to cast */
  331. RegSetValueEx(hk, pszSubkey, 0, REG_BINARY, (LPBYTE)&dw, sizeof(dw));
  332. RegCloseKey(hk);
  333.     }
  334. }
  335. /*****************************************************************************
  336.  *
  337.  *  SetDwordPkl
  338.  *
  339.  * Generic wrapper that writes out a registry key/subkey as a
  340.  * dword, given a key location.
  341.  *
  342.  *****************************************************************************/
  343. void PASCAL
  344. SetDwordPkl(PKL pkl, DWORD dw)
  345. {
  346.     SetRegDword(*pkl->phkRoot, pkl->ptszKey, pkl->ptszSubkey, dw);
  347. }
  348. /*****************************************************************************
  349.  *
  350.  *  DelPkl
  351.  *
  352.  * Generic wrapper that deletes a registry key/subkey.
  353.  *
  354.  *****************************************************************************/
  355. void PASCAL
  356. DelPkl(PKL pkl)
  357. {
  358.     HKEY hk;
  359.     if (_RegOpenKey(*pkl->phkRoot, pkl->ptszKey, &hk) == 0) {
  360. RegDeleteValue(hk, pkl->ptszSubkey);
  361. RegCloseKey(hk);
  362.     }
  363. }