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

Windows Kernel

Development Platform:

Visual C++

  1. //////////////////////////////////////////////////////////////////////
  2. //                      Microsoft Internet Explorer                 //
  3. //                Copyright(c) Microsoft Corp., 1995-1996           //
  4. //////////////////////////////////////////////////////////////////////
  5. //
  6. // REGUTIL.C - registry functions common between MSHTML and INETCPL.
  7. //
  8. // HISTORY:
  9. //
  10. // 8/7/96   t-gpease    created.
  11. //
  12. #include "inetcplp.h"
  13. //
  14. // Definintions
  15. //
  16. #define SMALLBUFFER 64
  17. //
  18. // Procedures
  19. //
  20. const TCHAR g_cszYes[] = TEXT("yes");
  21. const TCHAR g_cszNo[]  = TEXT("no");
  22. // Conveters and int into a string... ONLY BYTE VALUES!!
  23. TCHAR *MyIntToStr(TCHAR *pBuf, BYTE iVal)
  24. {
  25.     int i, t;
  26.     ASSERT(iVal < 1000);
  27.     i=0;
  28.     if (t = iVal/100)
  29.     {
  30.         pBuf[i] = L'0' + t;
  31.         i++;
  32.     }
  33.     if ((t = (iVal % 100) / 10) || (i!=0))
  34.     {
  35.         pBuf[i] = L'0' + t;
  36.         i++;
  37.     }
  38.     pBuf[i] = L'0' + iVal % 10;
  39.     i++;
  40.     pBuf[i] = L'';
  41.     return pBuf;
  42. }
  43. // Read the registry for a string (REG_SZ) of comma separated RGB values
  44. COLORREF RegGetColorRefString( HUSKEY huskey, LPTSTR RegValue, COLORREF Value)
  45. {
  46.     TCHAR SmallBuf[SMALLBUFFER];
  47.     TCHAR *pBuf;
  48.     DWORD cb;
  49. int iRed, iGreen, iBlue;
  50.     cb = ARRAYSIZE(SmallBuf);
  51.     if (SHRegQueryUSValue(huskey,
  52.                           RegValue,
  53.                           NULL,
  54.                           (LPBYTE)&SmallBuf,
  55.                           &cb,
  56.                           FALSE,
  57.                           NULL,
  58.                           NULL) == ERROR_SUCCESS)
  59.     {
  60.         iRed = StrToInt(SmallBuf);
  61.         pBuf = SmallBuf;
  62.         // find the next comma
  63.         while(pBuf && *pBuf && *pBuf!=L',')
  64.             pBuf++;
  65.         
  66.         // if valid and not NULL...
  67.         if (pBuf && *pBuf)
  68.             pBuf++;         // increment
  69.         iGreen = StrToInt(pBuf);
  70.         // find the next comma
  71.         while(pBuf && *pBuf && *pBuf!=L',')
  72.             pBuf++;
  73.         // if valid and not NULL...
  74.         if (pBuf && *pBuf)
  75.             pBuf++;         // increment
  76.         iBlue = StrToInt(pBuf);
  77.         // make sure all values are valid
  78. iRed    %= 256;
  79. iGreen  %= 256;
  80. iBlue   %= 256;
  81.     Value = RGB(iRed, iGreen, iBlue);
  82.     }
  83.     return Value;
  84. }
  85. // Writes the registry for a string (REG_SZ) of comma separated RGB values
  86. COLORREF RegSetColorRefString( HUSKEY huskey, LPTSTR RegValue, COLORREF Value)
  87. {
  88.     TCHAR SmallBuf[SMALLBUFFER];
  89.     TCHAR DigitBuf[4];  // that all we need for '255'
  90.     int iRed, iGreen, iBlue;
  91.     iRed   = GetRValue(Value);
  92.     iGreen = GetGValue(Value);
  93.     iBlue  = GetBValue(Value);
  94.     ASSERT(ARRAYSIZE(SmallBuf) >= 3 + 3 + 3 + 2 + 1) // "255,255,255"
  95.     MyIntToStr(SmallBuf, (BYTE)iRed);
  96.     StrCat(SmallBuf, TEXT(","));
  97.     StrCat(SmallBuf, MyIntToStr(DigitBuf, (BYTE)iGreen));
  98.     StrCat(SmallBuf, TEXT(","));
  99.     StrCat(SmallBuf, MyIntToStr(DigitBuf, (BYTE)iBlue));
  100.     SHRegWriteUSValue(huskey,
  101.                       RegValue,
  102.                       REG_SZ,
  103.                       (LPVOID)&SmallBuf,
  104.                       (lstrlen(SmallBuf)+1) * sizeof(TCHAR),
  105.                       SHREGSET_DEFAULT);
  106.     //
  107.     // BUGBUG: Should we do something if this fails?
  108.     //
  109.     return Value;
  110. }
  111. // Read the registry for a string (REG_SZ = "yes" | "no") and return a BOOL value
  112. BOOL RegGetBooleanString(HUSKEY huskey, LPTSTR pszRegValue, BOOL bValue)
  113. {
  114.     TCHAR   szBuf[SMALLBUFFER];
  115.     LPCTSTR  pszDefault;
  116.     DWORD   cb;
  117.     DWORD   cbDef;
  118.     // get the default setting
  119.     if (bValue)
  120.         pszDefault = g_cszYes;
  121.     else
  122.         pszDefault = g_cszNo;
  123.     
  124.     cb = ARRAYSIZE(szBuf);
  125.     cbDef = (lstrlen(pszDefault)+1)*sizeof(TCHAR); // +1 for null term
  126.     if (SHRegQueryUSValue(huskey,
  127.                           pszRegValue,
  128.                           NULL,
  129.                           (LPVOID)&szBuf,
  130.                           &cb,
  131.                           FALSE,
  132.                           (LPVOID)pszDefault,
  133.                           cbDef) == ERROR_SUCCESS)
  134.     {
  135.         if (!StrCmpI(szBuf, g_cszYes))
  136.             bValue = TRUE;
  137.         else if (!StrCmpI(szBuf, g_cszNo))
  138.             bValue = FALSE;
  139.         // else fall thru and return the Default that was passed in
  140.     }
  141.     return bValue;
  142. }
  143. // Write the registry for a string (REG_SZ) TRUE = "yes", FALSE = "no"
  144. BOOL RegSetBooleanString(HUSKEY huskey, LPTSTR pszRegValue, BOOL bValue)
  145. {
  146.     TCHAR szBuf[SMALLBUFFER];
  147.     if (bValue)
  148.         StrCpyN(szBuf, g_cszYes, ARRAYSIZE(szBuf));
  149.     else
  150.         StrCpyN(szBuf, g_cszNo, ARRAYSIZE(szBuf));
  151.     SHRegWriteUSValue(huskey,
  152.                       pszRegValue,
  153.                       REG_SZ,
  154.                       (LPVOID)&szBuf, 
  155.                       (lstrlen(szBuf)+1) * sizeof(TCHAR),
  156.                       SHREGSET_DEFAULT);
  157.     //
  158.     // BUGBUG: Should we try to do something if this fails?
  159.     //
  160.     return bValue;
  161. }