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.

178 lines
4.3 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // GetComputerName.cpp
  7. //
  8. // Description:
  9. // Getting and setting the computer name.
  10. //
  11. // Maintained By:
  12. // Galen Barbee (GalenB) 31-MAR-2000
  13. //
  14. // Revision History:
  15. //
  16. // Notes:
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "pch.h"
  20. /////////////////////////////////////////////////////////////////////////////
  21. //++
  22. //
  23. // HrGetComputerName()
  24. //
  25. // Description:
  26. // Get name of the computer on which this object is present.
  27. //
  28. // Arguments:
  29. // None.
  30. //
  31. // Return Value:
  32. // S_OK
  33. // Success.
  34. //
  35. // Remarks:
  36. // None.
  37. //
  38. //--
  39. //////////////////////////////////////////////////////////////////////////////
  40. HRESULT
  41. WINAPI
  42. HrGetComputerName(
  43. COMPUTER_NAME_FORMAT cnfIn,
  44. BSTR * pbstrComputerNameOut
  45. )
  46. {
  47. TraceFunc( "" );
  48. HRESULT hr = S_OK;
  49. PWCHAR pszCompName = NULL;
  50. DWORD cchCompName = 0;
  51. DWORD sc;
  52. DWORD dwErr;
  53. BOOL fAppendDomain = FALSE;
  54. PDOMAIN_CONTROLLER_INFO pdci = NULL;
  55. int idx;
  56. if ( pbstrComputerNameOut == NULL )
  57. {
  58. hr = THR( E_POINTER );
  59. goto CleanUp;
  60. } // if:
  61. //
  62. // DsGetDcName will give us access to a usable domain name, regardless of whether we are
  63. // currently in a W2k or a NT4 domain. On W2k and above, it will return a DNS domain name,
  64. // on NT4 it will return a NetBIOS name.
  65. //
  66. dwErr = TW32( DsGetDcName(
  67. NULL
  68. , NULL
  69. , NULL
  70. , NULL
  71. , DS_DIRECTORY_SERVICE_PREFERRED
  72. , &pdci
  73. ) );
  74. if ( dwErr != ERROR_SUCCESS )
  75. {
  76. goto CleanUp;
  77. } // if: DsGetDcName failed
  78. //
  79. // This handles the case when we are a member of a legacy (pre-W2k) Domain.
  80. // In this case, both FQDN and DnsDomain will not receive useful data from GetComputerNameEx.
  81. // What we actually want to get is <computername>.<DomainName> in every case
  82. //
  83. switch( cnfIn )
  84. {
  85. case ComputerNameDnsFullyQualified:
  86. fAppendDomain = TRUE;
  87. cnfIn = ComputerNameDnsHostname;
  88. break;
  89. case ComputerNamePhysicalDnsFullyQualified:
  90. fAppendDomain = TRUE;
  91. cnfIn = ComputerNamePhysicalDnsHostname;
  92. break;
  93. case ComputerNameDnsDomain:
  94. case ComputerNamePhysicalDnsDomain:
  95. *pbstrComputerNameOut = TraceSysAllocString( pdci->DomainName );
  96. if ( *pbstrComputerNameOut == NULL )
  97. {
  98. goto OutOfMemory;
  99. } // if:
  100. goto CleanUp;
  101. }
  102. pszCompName = NULL;
  103. for ( idx = 0; ; idx++ )
  104. {
  105. Assert( idx < 2 );
  106. if ( !GetComputerNameEx( cnfIn, pszCompName, &cchCompName ) )
  107. {
  108. sc = GetLastError();
  109. if ( sc == ERROR_MORE_DATA )
  110. {
  111. if ( fAppendDomain )
  112. {
  113. cchCompName += (DWORD)( wcslen( pdci->DomainName ) + 1 );
  114. } // if:
  115. pszCompName = new WCHAR[ cchCompName ];
  116. if ( pszCompName == NULL )
  117. {
  118. goto OutOfMemory;
  119. } // if: new failed
  120. continue;
  121. } // if: more data...
  122. TW32( sc );
  123. hr = HRESULT_FROM_WIN32( sc );
  124. LogMsg( "GetComputerNameEx failed. hr = 0x%08x", hr );
  125. goto CleanUp;
  126. } // if: GetComputerNameEx() failed
  127. if ( fAppendDomain )
  128. {
  129. wcscat( pszCompName, L"." );
  130. wcscat( pszCompName, pdci->DomainName );
  131. }
  132. *pbstrComputerNameOut = TraceSysAllocString( pszCompName );
  133. if ( *pbstrComputerNameOut == NULL )
  134. {
  135. goto OutOfMemory;
  136. } // if:
  137. hr = S_OK;
  138. break;
  139. } // for: loop to retry the operation.
  140. goto CleanUp;
  141. OutOfMemory:
  142. hr = THR( E_OUTOFMEMORY );
  143. LogMsg( "HrGetComputerName: out of memory." );
  144. CleanUp:
  145. delete [] pszCompName;
  146. if ( pdci != NULL )
  147. {
  148. NetApiBufferFree( pdci );
  149. }
  150. HRETURN( hr );
  151. } //*** HrGetComputerName()