VC++ 修改電腦名稱 SetComputerName

來源:互聯網
上載者:User

轉載請標明出處,原文地址: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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.