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.

93 lines
2.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. /////////////////////////////////////////////////////////////////////////////
  22. // CRenameComputer
  23. STDMETHODIMP CRenameComputer::RenameLocalComputer(BSTR NewName)
  24. {
  25. HRESULT hr = S_OK;
  26. WCHAR * newNameW = (WCHAR*)NewName;
  27. WCHAR nameW[LEN_Computer];
  28. DWORD rc = 0;
  29. if ( newNameW[0] == L'\\' )
  30. {
  31. safecopy(nameW,newNameW+2);
  32. }
  33. else
  34. {
  35. safecopy(nameW,newNameW);
  36. }
  37. // convert the name to uppercase
  38. for ( int i = 0 ; nameW[i] ; i++ )
  39. {
  40. nameW[i] = towupper(nameW[i]);
  41. }
  42. if ( ! m_bNoChange )
  43. {
  44. if ( ! SetComputerName(nameW) )
  45. {
  46. DWORD rc = GetLastError();
  47. hr = HRESULT_FROM_WIN32(rc);
  48. }
  49. else
  50. {
  51. // Set the host name or the NVHostname value as required
  52. LPWKSTA_INFO_100 pBuf = NULL;
  53. rc = NetWkstaGetInfo(NULL, 100, (LPBYTE*)&pBuf);
  54. if( !rc )
  55. {
  56. TRegKey network(L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters");
  57. if ( pBuf->wki100_ver_major < 5 )
  58. rc = network.ValueSetStr(L"Hostname", nameW);
  59. else
  60. rc = network.ValueSetStr(L"NVHostname", nameW);
  61. if ( rc )
  62. hr = HRESULT_FROM_WIN32(GetLastError());
  63. NetApiBufferFree(pBuf);
  64. }
  65. else
  66. hr = HRESULT_FROM_WIN32(GetLastError());
  67. }
  68. }
  69. return hr;
  70. }
  71. STDMETHODIMP CRenameComputer::get_NoChange(BOOL *pVal)
  72. {
  73. (*pVal) = m_bNoChange;
  74. return S_OK;
  75. }
  76. STDMETHODIMP CRenameComputer::put_NoChange(BOOL newVal)
  77. {
  78. m_bNoChange = newVal;
  79. return S_OK;
  80. }