轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8659430
歡迎關注微博:http://weibo.com/MoreWindows
上一篇《VC++得到電腦名稱和使用者名稱》講解了如何使用GetComputerName和GetUserName來擷取電腦名稱及使用者名稱。本篇來講解用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.
// VC++修改電腦名稱 // http://blog.csdn.net/morewindows/article/details/8659430//By MoreWindows-(http://blog.csdn.net/MoreWindows) #include <windows.h>#include <stdio.h>#include <conio.h>int main(){printf(" VC++修改電腦名稱 \n"); printf(" -- By MoreWindows( http://blog.csdn.net/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");elseprintf("Error %d\n", GetLastError());}}return 0;}
先修改下
重啟後可以發現電腦的名稱已經被修改了。
轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8659430
歡迎關注微博:http://weibo.com/MoreWindows