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.

164 lines
4.4 KiB

  1. /*---------------------------------------------------------------------------
  2. File: RenameComputer.cpp
  3. Comments: Implementation of COM object to change the name of a computer.
  4. This must be run locally on the computer to be renamed.
  5. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  6. Proprietary and confidential to Mission Critical Software, Inc.
  7. REVISION LOG ENTRY
  8. Revision By: Christy Boles
  9. Revised on 02/15/99 11:22:41
  10. ---------------------------------------------------------------------------
  11. */
  12. // RenameComputer.cpp : Implementation of CRenameComputer
  13. #include "stdafx.h"
  14. #include "WorkObj.h"
  15. #include "Rename.h"
  16. #include "Common.hpp"
  17. #include "UString.hpp"
  18. #include "EaLen.hpp"
  19. #include <lm.h>
  20. #include "TReg.hpp"
  21. typedef WINBASEAPI BOOL (WINAPI* PSETCOMPUTERNAMEEXW)
  22. (
  23. IN COMPUTER_NAME_FORMAT NameType,
  24. IN LPCWSTR lpBuffer
  25. );
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CRenameComputer
  28. STDMETHODIMP CRenameComputer::RenameLocalComputer(BSTR bstrNewName)
  29. {
  30. USES_CONVERSION;
  31. HRESULT hr = S_OK;
  32. //
  33. // validate argument - a new name must be passed
  34. //
  35. UINT cchNewName = SysStringLen(bstrNewName);
  36. if (cchNewName == 0)
  37. {
  38. return E_INVALIDARG;
  39. }
  40. //
  41. // only perform if not in test mode
  42. //
  43. if (!m_bNoChange)
  44. {
  45. //
  46. // remove leading backslash characters
  47. //
  48. PCWSTR pszNewName = OLE2CW(bstrNewName);
  49. WCHAR szNewName[LEN_Computer];
  50. if ((cchNewName >= 2) && ((pszNewName[0] == L'\\') && (pszNewName[1] == L'\\')))
  51. {
  52. wcsncpy(szNewName, &pszNewName[2], sizeof(szNewName)/sizeof(szNewName[0]));
  53. }
  54. else
  55. {
  56. wcsncpy(szNewName, pszNewName, sizeof(szNewName)/sizeof(szNewName[0]));
  57. }
  58. szNewName[sizeof(szNewName)/sizeof(szNewName[0]) - 1] = L'\0';
  59. //
  60. // convert the new name to lowercase
  61. //
  62. // the NetBIOS name is passed to this function which is uppercase
  63. // if this name is passed to the SetComputerName functions the DNS
  64. // name will also be uppercase which is not desired the NetBIOS name
  65. // is always converted to uppercase by SetComputerName functions
  66. //
  67. _wcslwr(szNewName);
  68. //
  69. // Attempt to use the SetComputerEx function which sets both the NetBIOS
  70. // and DNS names but is only available with Windows 2000 and later.
  71. //
  72. bool bUseSetComputer = false;
  73. HMODULE hKernel32 = LoadLibrary(L"Kernel32.dll");
  74. if (hKernel32)
  75. {
  76. PSETCOMPUTERNAMEEXW pSetComputerNameExW = (PSETCOMPUTERNAMEEXW) GetProcAddress(hKernel32, "SetComputerNameExW");
  77. if (pSetComputerNameExW)
  78. {
  79. //
  80. // set both the DNS hostname and NetBIOS name to the same value
  81. //
  82. if (!pSetComputerNameExW(ComputerNamePhysicalDnsHostname, szNewName))
  83. {
  84. DWORD dwError = GetLastError();
  85. hr = HRESULT_FROM_WIN32(dwError);
  86. }
  87. }
  88. else
  89. {
  90. bUseSetComputer = true;
  91. }
  92. FreeLibrary(hKernel32);
  93. }
  94. else
  95. {
  96. bUseSetComputer = true;
  97. }
  98. //
  99. // SetComputerNameEx is not available with Windows NT 4.0 and earlier
  100. // therefore use SetComputerName which only sets the NetBIOS name.
  101. // The DNS hostname must then be set by directly updating registry.
  102. //
  103. if (bUseSetComputer)
  104. {
  105. if (SetComputerName(szNewName))
  106. {
  107. TRegKey key(L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters");
  108. DWORD dwError = key.ValueSetStr(L"Hostname", szNewName);
  109. hr = HRESULT_FROM_WIN32(dwError);
  110. }
  111. else
  112. {
  113. DWORD dwError = GetLastError();
  114. hr = HRESULT_FROM_WIN32(dwError);
  115. }
  116. }
  117. }
  118. return hr;
  119. }
  120. STDMETHODIMP CRenameComputer::get_NoChange(BOOL *pVal)
  121. {
  122. (*pVal) = m_bNoChange;
  123. return S_OK;
  124. }
  125. STDMETHODIMP CRenameComputer::put_NoChange(BOOL newVal)
  126. {
  127. m_bNoChange = newVal;
  128. return S_OK;
  129. }