编程修改windows计算机名。

在公司软件定制部门工作时研究了改计算机名问题,有了些心得,写了个工具实现. 不但改计算机名,netbios名也同时更新.win2000和winxp下测试通过.

   
#include "stdafx.h"
#include "ChgCompName.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern "C" int getopt(int, char * const *, const char *);
extern "C" char *optarg;
/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;
void usage(char *progname);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
 int nRetCode = 0;
 cout<<"The Utility help you to change the workstations name (both NetBIOS and Host)"< cout<<"Version: 1.00"< cout<<"Only support win2000, XP or better."<  // initialize MFC and print and error on failure
 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
 {
  // TODO: change error code to suit your needs
  cerr << _T("Fatal Error: MFC initialization failed") << endl;
  nRetCode = 1;
 }
 else
 {
  if (argc==1)
  {
   usage(argv[0]);
   return 2;
  }
  char temp=NULL;
  CString sComputerName("");
  CString sWorkgroupName("");
  
  while((temp=getopt(argc,argv,"c:?h"))!=-1)
  {
   switch (temp)
   {
    case 'c':
     sComputerName=optarg;
     break; 
    default:
     usage(argv[0]);
     return 2;
   }
  }


  if (SetComputerNameEx(ComputerNamePhysicalDnsHostname,argv[1]))  // 改变计算机名,这个是关键.
  {
   cout<<"The computer name has been successfully changed to ";
   cout<   cout<<"Note:This change will not take effect until the computer is restarted."<  }
  else
  {
   fprintf(stderr,"/nFailed to change computer name.");
   return 3;
  }
 }

 return nRetCode;
}


void usage(char *progname)
{

 fprintf(stderr,"/n/nUsage:%s [-c NewComputerName] [-?]/n",progname);
 
}

你可能感兴趣的:(计算机软件技术.)