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.

147 lines
3.9 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 00
  5. *
  6. * File: regkeyex.inl
  7. *
  8. * Contents: Inline files for CRegKeyEx class
  9. *
  10. * History: 7-Apr-2000 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. /*+-------------------------------------------------------------------------*
  14. * CRegKeyEx::ScQueryString
  15. *
  16. * Loads a string from the given value name.
  17. *
  18. * The template type StringType can be any string class that supports
  19. * MFC's CString interface (i.e. MFC's CString, WTL::CString, or MMC's
  20. * CStr).
  21. *--------------------------------------------------------------------------*/
  22. template<class StringType>
  23. SC ScQueryString (
  24. LPCTSTR pszValueName, /* I:name of value to read */
  25. StringType& strData, /* O:contents of the value */
  26. DWORD* pdwType /*=NULL*/) /* O:REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ*/
  27. {
  28. DECLARE_SC (sc, _T("CRegKeyEx::ScQueryString"));
  29. /*
  30. * clear out existing contents
  31. */
  32. strData.Empty ();
  33. /*
  34. * find out how much space we need to load the string
  35. */
  36. DWORD dwType = REG_SZ;
  37. DWORD cbBuffer = 0;
  38. sc = ScQueryValue (pszValueName, &dwType, NULL, &cbBuffer);
  39. if (sc)
  40. return (sc);
  41. /*
  42. * if we're not loading a string type, return an error
  43. */
  44. if (pdwType != NULL)
  45. *pdwType = dwType;
  46. if ((dwType != REG_SZ) && (dwType != REG_EXPAND_SZ) && (dwType != REG_MULTI_SZ))
  47. return (sc = ScFromWin32 (ERROR_INVALID_DATATYPE));
  48. /*
  49. * allocate a buffer for the string
  50. */
  51. DWORD cchBuffer = cbBuffer / sizeof (TCHAR);
  52. LPTSTR pBuffer = strData.GetBuffer (cchBuffer);
  53. if (pBuffer == NULL)
  54. return (sc = E_OUTOFMEMORY);
  55. /*
  56. * load the string from the registry
  57. */
  58. sc = ScQueryValue (pszValueName, &dwType, pBuffer, &cbBuffer);
  59. strData.ReleaseBuffer (cchBuffer);
  60. if (sc)
  61. return (sc);
  62. return (sc);
  63. }
  64. /*+-------------------------------------------------------------------------*
  65. * CRegKeyEx::ScLoadRegUIString
  66. *
  67. * Wrapper around SHLoadRegUIString, which is used to support MUI.
  68. *
  69. * SHLoadRegUIString will read a string of the form
  70. *
  71. * @[path\]<dllname>,-<strId>
  72. *
  73. * The string with id <strId> is loaded from <dllname>. If no explicit
  74. * path is provided then the DLL will be chosen according to pluggable UI
  75. * specifications, if possible.
  76. *
  77. * If the registry string is not of the special form described here,
  78. * SHLoadRegUIString will return the string intact.
  79. *
  80. * The template type StringType can be any string class that supports
  81. * MFC's CString interface (i.e. MFC's CString, WTL::CString, or MMC's
  82. * CStr).
  83. *--------------------------------------------------------------------------*/
  84. template<class StringType>
  85. SC ScLoadRegUIString (
  86. LPCTSTR pszValueName, /* I:name of value to read */
  87. StringType& strData) /* O:logical contents of the value */
  88. {
  89. DECLARE_SC (sc, _T("CRegKeyEx::ScLoadRegUIString"));
  90. /*
  91. * clear out existing contents
  92. */
  93. strData.Empty ();
  94. const int cbGrow = 256;
  95. int cbBuffer = 0;
  96. do
  97. {
  98. /*
  99. * allocate a larger buffer for the string
  100. */
  101. cbBuffer += cbGrow;
  102. LPTSTR pBuffer = strData.GetBuffer (cbBuffer);
  103. if (pBuffer == NULL)
  104. return (sc = E_OUTOFMEMORY);
  105. /*
  106. * load the string from the registry
  107. * Most of the snapins do not have MUI string so we do not want to trace
  108. * this error as the caller takes care of error condition and reads registry
  109. * directly.
  110. */
  111. SC scNoTrace = SHLoadRegUIString (m_hKey, pszValueName, pBuffer, cbBuffer);
  112. strData.ReleaseBuffer();
  113. if (scNoTrace)
  114. return scNoTrace;
  115. /*
  116. * If we filled up the buffer, we'll pessimistically assume that
  117. * there's more data available. We'll loop around, grow the buffer,
  118. * and try again.
  119. */
  120. } while (strData.GetLength() == cbBuffer-1);
  121. /*
  122. * free up extra space
  123. */
  124. strData.FreeExtra();
  125. return (sc);
  126. }