VC++ 修改计算机名称 SetComputerName

上一篇《VC++得到计算机名和用户名》讲解了如何使用GetComputerNameGetUserName来获取计算机名称及用户名。本篇来讲解用SetComputerName来修改计算机名称。

函数原型:

//By MoreWindows-(http://blog.csdn.net/MoreWindows)

BOOLWINAPISetComputerName(

    __in          LPCTSTRlpComputerName

);

对于计算机名称有一定的要求,MSDN的解释如下:

The computer name that will take effect the next time the computer is started. The name must not be longer than MAX_COMPUTERNAME_LENGTH characters.

The standard character set includes letters, numbers, and the following symbols: ! @ # $ % ^ & ' ) ( . - _ { } ~ . If this parameter contains one or more characters that are outside the standard character set, SetComputerName returns ERROR_INVALID_PARAMETER.

[cpp]  view plain copy
  1. // VC++修改计算机名称    
  2. // http://blog.csdn.net/morewindows/article/details/8659430  
  3. //By MoreWindows-(http://blog.csdn.net/MoreWindows)    
  4. #include <windows.h>  
  5. #include <stdio.h>  
  6. #include <conio.h>  
  7. int main()  
  8. {     
  9.     printf("    VC++修改计算机名称 \n");          
  10.     printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");   
  11.     const int MAX_BUFFER_LEN = 500;  
  12.     char  szBuffer[MAX_BUFFER_LEN];  
  13.     DWORD dwNameLen;  
  14.       
  15.     dwNameLen = MAX_BUFFER_LEN;  
  16.     if (!GetComputerName(szBuffer, &dwNameLen))   
  17.     {  
  18.         printf("Error  %d\n", GetLastError());  
  19.         return -1;  
  20.     }  
  21.     printf("当前计算机名为: %s\n", szBuffer);  
  22.     printf("要改名吗?(y/n)\n");  
  23.     int ch = getch();  
  24.     ch = toupper(ch);  
  25.     if (ch == 'Y')  
  26.     {  
  27.         printf("请输入新名称:");  
  28.         gets(szBuffer);  
  29.         if (strlen(szBuffer) != 0)  
  30.         {  
  31.             if (SetComputerName(szBuffer))  
  32.                 printf("恭喜!改名成功,重启后生效\n");  
  33.             else  
  34.                 printf("Error  %d\n", GetLastError());  
  35.         }  
  36.     }  
  37.     return 0;  
  38. }  

先修改下

 

重启后可以发现计算机的名称已经被修改了。

 

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8659430

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值