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

Windows Kernel

Development Platform:

Visual C++

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