Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8659430
Welcome to Weibo: http://weibo.com/MoreWindows
In the previous article VC ++ getting a computer name and user name, we explained how to use getcomputername and GetUserName to get the computer name and user name. This article describes how to use setcomputername to modify the computer name.
Function prototype:
// By morewindows-(http://blog.csdn.net/MoreWindows)
Boolwinapisetcomputername (
_ In lpctstrlpcomputername
);
There are certain requirements for computer names. The msdn explanation is as follows:
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 parameter des 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 ++ modify computer name // 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 ++ modify computer name \ n"); printf ("-- by morewindows (http://blog.csdn.net/MoreWindows) -- \ 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 ("current computer name: % s \ n ", szbuffer); printf (" do you want to change the name? (Y/n) \ n "); int CH = getch (); CH = toupper (CH); If (CH = 'y ') {printf ("enter a new name:"); gets (szbuffer); If (strlen (szbuffer )! = 0) {If (setcomputername (szbuffer) printf ("Congratulations! Renamed successfully. It takes effect after restart \ n "); elseprintf (" error % d \ n ", getlasterror () ;}} return 0 ;}
Modify
After restarting, you can find that the computer name has been modified.
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8659430
Welcome to Weibo: http://weibo.com/MoreWindows