您的位置:首页 > 理论基础

编程修改windows计算机名。

2005-04-19 09:44 267 查看
在公司软件定制部门工作时研究了改计算机名问题,有了些心得,写了个工具实现. 不但改计算机名,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)"<<endl;
 cout<<"Version: 1.00"<<endl;
 cout<<"Only support win2000, XP or better."<<endl;
  // 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<<argv[1]<<endl;
   cout<<"Note:This change will not take effect until the computer is restarted."<<endl;
  }
  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);
 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息