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.

161 lines
3.8 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // RegistryUtils.cpp
  7. //
  8. // Description:
  9. // Useful functions for manipulating directies.
  10. //
  11. // Maintained By:
  12. // Galen Barbee (GalenB) 10-SEP-2002
  13. //
  14. // Revision History:
  15. //
  16. // Notes:
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "Pch.h"
  20. //////////////////////////////////////////////////////////////////////////////
  21. // Type Definitions
  22. //////////////////////////////////////////////////////////////////////////////
  23. //////////////////////////////////////////////////////////////////////////////
  24. // Forward Declarations.
  25. //////////////////////////////////////////////////////////////////////////////
  26. /////////////////////////////////////////////////////////////////////////////
  27. //++
  28. //
  29. // HrGetDefaultComponentNameFromRegistry
  30. //
  31. // Description:
  32. // Get the default name for the passed in COM component CLSID from
  33. // the registry.
  34. //
  35. // Arguments:
  36. // pclsidIn
  37. // CLSID whose default name is requested.
  38. //
  39. // pbstrComponentNameOut
  40. // Buffer to receive the name.
  41. //
  42. // Return Value:
  43. // S_OK
  44. // Success.
  45. //
  46. // Win32 Error
  47. // something failed.
  48. //
  49. // Remarks:
  50. // None.
  51. //
  52. //--
  53. //////////////////////////////////////////////////////////////////////////////
  54. HRESULT
  55. HrGetDefaultComponentNameFromRegistry(
  56. CLSID * pclsidIn
  57. , BSTR * pbstrComponentNameOut
  58. )
  59. {
  60. TraceFunc( "" );
  61. Assert( pclsidIn != NULL );
  62. Assert( pbstrComponentNameOut != NULL );
  63. HRESULT hr = S_OK;
  64. DWORD sc = ERROR_SUCCESS;
  65. BSTR bstrSubKey = NULL;
  66. HKEY hKey = NULL;
  67. WCHAR szGUID[ 64 ];
  68. int cch = 0;
  69. DWORD dwType = 0;
  70. WCHAR * pszName = NULL;
  71. DWORD cbName = 0;
  72. cch = StringFromGUID2( *pclsidIn, szGUID, RTL_NUMBER_OF( szGUID ) );
  73. Assert( cch > 0 ); // 64 chars should always hold a guid!
  74. //
  75. // Create the subkey string.
  76. //
  77. hr = THR( HrFormatStringIntoBSTR( L"CLSID\\%1!ws!", &bstrSubKey, szGUID ) );
  78. if ( FAILED( hr ) )
  79. {
  80. goto Cleanup;
  81. } // if:
  82. //
  83. // Open the key under HKEY_CLASSES_ROOT.
  84. //
  85. sc = TW32( RegOpenKeyEx( HKEY_CLASSES_ROOT, bstrSubKey, 0, KEY_READ, &hKey ) );
  86. if ( sc != ERROR_SUCCESS )
  87. {
  88. hr = HRESULT_FROM_WIN32( sc );
  89. goto Cleanup;
  90. } // if:
  91. //
  92. // Get the length of the default value.
  93. //
  94. sc = TW32( RegQueryValueExW( hKey, L"", NULL, &dwType, NULL, &cbName ) );
  95. if ( sc != ERROR_SUCCESS )
  96. {
  97. hr = HRESULT_FROM_WIN32( sc );
  98. goto Cleanup;
  99. } // if:
  100. //
  101. // Allocate some space for the string.
  102. //
  103. pszName = new WCHAR[ ( cbName / sizeof( WCHAR ) ) + 1 ];
  104. if ( pszName == NULL )
  105. {
  106. hr = THR( E_OUTOFMEMORY );
  107. goto Cleanup;
  108. } // if:
  109. //
  110. // Get the default value which should be the name of the component.
  111. //
  112. sc = TW32( RegQueryValueExW( hKey, L"", NULL, &dwType, (LPBYTE) pszName, &cbName ) );
  113. if ( sc != ERROR_SUCCESS )
  114. {
  115. hr = HRESULT_FROM_WIN32( sc );
  116. goto Cleanup;
  117. } // if:
  118. Assert( dwType == REG_SZ );
  119. *pbstrComponentNameOut = TraceSysAllocString( pszName );
  120. if ( *pbstrComponentNameOut == NULL )
  121. {
  122. hr = THR( E_OUTOFMEMORY );
  123. goto Cleanup;
  124. } // if:
  125. hr = S_OK;
  126. Cleanup:
  127. if ( hKey != NULL )
  128. {
  129. RegCloseKey( hKey );
  130. } // if:
  131. TraceSysFreeString( bstrSubKey );
  132. delete [] pszName;
  133. HRETURN( hr );
  134. } //*** HrGetDefaultComponentNameFromRegistry