Leaked source code of windows server 2003
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.

142 lines
4.2 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 000
  5. *
  6. * File: regutil.inl
  7. *
  8. * Contents: Inline functions for regutil.h
  9. *
  10. * History: 24-Apr-2000 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #pragma once
  14. #include "regkeyex.h"
  15. /*+-------------------------------------------------------------------------*
  16. * IsIndirectStringSpecifier
  17. *
  18. * Returns true if the given string looks like it might be an indirect
  19. * string specifier acceptable to SHLoadRegUIString, i.e. "@dllname,-id".
  20. *
  21. * Returns false otherwise
  22. *--------------------------------------------------------------------------*/
  23. inline bool IsIndirectStringSpecifier (LPCTSTR psz)
  24. {
  25. return ((psz != NULL) && (psz[0] == _T('@')) && (_tcsstr(psz, _T(",-")) != NULL));
  26. }
  27. /*+-------------------------------------------------------------------------*
  28. * ScGetSnapinNameFromRegistry
  29. *
  30. * Loads the name of a snap-in from the given snap-in CLSID.
  31. *
  32. * The template type StringType can be any string class that supports
  33. * MFC's CString interface (i.e. MFC's CString, WTL::CString, or MMC's
  34. * CStr).
  35. *--------------------------------------------------------------------------*/
  36. template<class StringType>
  37. SC ScGetSnapinNameFromRegistry (
  38. const CLSID& clsid,
  39. StringType& str)
  40. {
  41. DECLARE_SC (sc, _T("ScGetSnapinNameFromRegistry"));
  42. /*
  43. * convert class id to string
  44. */
  45. OLECHAR szClsid[40];
  46. if (!StringFromGUID2 (clsid, szClsid, countof(szClsid)))
  47. return (sc = E_INVALIDARG);
  48. /*
  49. * load the name
  50. */
  51. USES_CONVERSION;
  52. sc = ScGetSnapinNameFromRegistry (OLE2T(szClsid), str);
  53. if (sc)
  54. return (sc);
  55. return (sc);
  56. }
  57. /*+-------------------------------------------------------------------------*
  58. * ScGetSnapinNameFromRegistry
  59. *
  60. * Loads the name of a snap-in from the given snap-in CLSID string.
  61. *
  62. * The template type StringType can be any string class that supports
  63. * MFC's CString interface (i.e. MFC's CString, WTL::CString, or MMC's
  64. * CStr).
  65. *--------------------------------------------------------------------------*/
  66. template<class StringType>
  67. SC ScGetSnapinNameFromRegistry (
  68. LPCTSTR pszClsid,
  69. StringType& str)
  70. {
  71. DECLARE_SC (sc, _T("ScGetSnapinNameFromRegistry"));
  72. /*
  73. * open HKLM\Software\Microsoft\MMC\SnapIns\{CLSID}
  74. */
  75. StringType strKeyName = StringType(SNAPINS_KEY) + _T("\\") + pszClsid;
  76. CRegKeyEx SnapinKey;
  77. LONG lRet = SnapinKey.Open (HKEY_LOCAL_MACHINE, strKeyName, KEY_READ);
  78. if (ERROR_SUCCESS != lRet)
  79. return (sc = HRESULT_FROM_WIN32 (lRet));
  80. /*
  81. * load the string
  82. */
  83. sc = ScGetSnapinNameFromRegistry (SnapinKey, str);
  84. if (sc)
  85. return (sc);
  86. return (sc);
  87. }
  88. /*+-------------------------------------------------------------------------*
  89. * ScGetSnapinNameFromRegistry
  90. *
  91. * Loads the name of a snap-in from the given open snap-in registry key.
  92. * First, we'll try to use SHLoadRegUIString to resolve an indirect name
  93. * in the "NameStringIndirect" value. If that fails (because "NameString-
  94. * Indirect" isn't registered, because the registered DLL can't be loaded,
  95. * etc.), we'll fall back to the old "NameString" entry.
  96. *
  97. * The template type StringType can be any string class that supports
  98. * MFC's CString interface (i.e. MFC's CString, WTL::CString, or MMC's
  99. * CStr).
  100. *--------------------------------------------------------------------------*/
  101. template<class StringType>
  102. SC ScGetSnapinNameFromRegistry (
  103. CRegKeyEx& key, /* I:snap-in's key, opened for read */
  104. StringType& str) /* O:name of snap-in */
  105. {
  106. DECLARE_SC (sc, _T("ScGetSnapinNameFromRegistry"));
  107. /*
  108. * try to load the MUI-friendly name first, ignoring errors
  109. */
  110. sc = key.ScLoadRegUIString (g_szNameStringIndirect, str);
  111. /*
  112. * if an error occurred, or ScLoadReagUIString couldn't resolve the
  113. * @dllname syntax, or the returned string was empty, fall back
  114. * on the legacy registry entry
  115. */
  116. if (sc.IsError() || str.IsEmpty() || IsIndirectStringSpecifier(str))
  117. sc = key.ScQueryString (g_szNameString, str, NULL);
  118. return (sc);
  119. }