您好,欢迎来到叨叨游戏网。
搜索
您的当前位置:首页VC++ 修改计算机名称 SetComputerName

VC++ 修改计算机名称 SetComputerName

来源:叨叨游戏网

欢迎关注微博:

 

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

函数原型:

//By MoreWindows-(http:///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.

// VC++修改计算机名称  
// http:///morewindows/article/details/8659430
//By MoreWindows-(http:///MoreWindows)  
#include <windows.h>
#include <stdio.h>
#include <conio.h>
int main()
{	
	printf("    VC++修改计算机名称 \n");        
	printf(" -- By MoreWindows( http:///MoreWindows ) --\n\n"); 
	const int MAX_BUFFER_LEN = 500;
	char  szBuffer[MAX_BUFFER_LEN];
	DWORD dwNameLen;
	
	dwNameLen = MAX_BUFFER_LEN;
	if (!GetComputerName(szBuffer, &dwNameLen)) 
	{
		printf("Error  %d\n", GetLastError());
		return -1;
	}
	printf("当前计算机名为: %s\n", szBuffer);
	printf("要改名吗?(y/n)\n");
	int ch = getch();
	ch = toupper(ch);
	if (ch == 'Y')
	{
		printf("请输入新名称:");
		gets(szBuffer);
		if (strlen(szBuffer) != 0)
		{
			if (SetComputerName(szBuffer))
				printf("恭喜!改名成功,重启后生效\n");
			else
				printf("Error  %d\n", GetLastError());
		}
	}
	return 0;
}

先修改下

 

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

 

欢迎关注微博:

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.net 版权所有 湘ICP备2024080961号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务