C + + calling the win32 API setComputerName () function can not modify the windows 10 computer name

sir song 1 Reputation point
2020-11-22T14:16:22.797+00:00

C + + calling the win32 API setComputerName () function can not modify the windows 10 computer name

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

//获取计算机名称
wstring getComputerName() {
    DWORD size = 0;
    wstring wstr;
    GetComputerName(NULL, &size); //得到电脑名称长度
    wchar_t *name = new wchar_t[size];
    if (GetComputerName(name, &size)) {
        wstr = name;
    }
    delete[] name;
    return wstr;
}


//设置计算机名称
bool setComputerName(string pcName) {
    if (!pcName.length())
        return false;
    size_t size = pcName.length();
    wchar_t *buffer = new wchar_t[size + 1];
    MultiByteToWideChar(CP_ACP, 0, pcName.c_str(), size, buffer, size * sizeof(wchar_t));
    buffer[size] = 0;
    bool isOk = SetComputerNameEx(ComputerNamePhysicalDnsHostname, buffer);
    delete[] buffer;
    return isOk;
}

void getInput(string title, string &arg) {
    cout << title << endl;
    getline(cin, arg);
}

//打印基本信息
void view_welcome() {
    //设置字体背景色红色
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
    printf("******************修改工具v1.0*****************\n");
    printf("                作者:wangtong                \n\n");
    printf("-------------------包含功能--------------------\n");
    printf("【修改计算机名】--【修改IP】--【修改子网掩码】 \n");
    printf("-----------------------------------------------\n\n\n");
    //设置字体颜色 绿色
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
}
/**
*确认语句
例子:yOrN(“确认要修改以上信息吗?”)
输出:确认要修改以上信息吗?(y/n)
**/
bool yOrN(string desc) {

    cout << desc << "(y/n)";
    int ch = getchar();
    ch = toupper(ch);
    getchar();//这句的作用是清空缓冲区-回车键换行符10
    if (ch == 'Y') {
        return true;
    }
    return false;
}
//修改IP和掩码。调用dos命令
void modifyIpAndMask(string ip, string mask) {
    if (ip.length() == 0 || mask.length() == 0)
        return;
    string cmd = "netsh interface ip set address name=本地连接 static ";
    cmd += ip + " " + mask;
    system(cmd.c_str());
}
//修改电脑主机名,调用windowAPI
bool modifyPCName(string pcName) {

    if (setComputerName(pcName)) {
        return true;
    }
    else {
        printf("Error  %d\n", GetLastError());
        return false;
    }

}

int main() {
    view_welcome();
    wcout << "【ComputerName】" << getComputerName() << endl;
    string pcName;
    string mask;
    string ip;
    getInput("Please enter a new computer name(Enter the default does not change)", pcName);
    getInput("请输入新的ip(回车默认不改)", ip);
    if (ip.length()) {
        getInput("Please enter a new IP(回车默认255.255.255.0)", mask);
        if (!mask.length()) {
            mask = "255.255.255.0";
        }
    }
    if (ip.length() || pcName.length()) {
        printf("您修改了如下信息:\n");
        if (ip.length())
            cout << "【ip地址】:" + ip << "\n【子网掩码】:" + mask << endl;
        if (pcName.length())
            cout << "【ComputerName】:" + pcName << endl;
        if (yOrN("确认要修改以上信息吗?")) {
            if (ip.length())
                modifyIpAndMask(ip, mask);
            if (pcName.length()) {
                if (modifyPCName(pcName)) {
                    printf("修改成功,重启后生效\n");
                    if (yOrN("现在要重启吗?回车默认不重启!")) {
                        system("shutdown /r /t 0");
                    }
                }
                else {
                    printf("修改计算机名发生错误\n");
                }
            }

        }
    }
    printf("按任意键退出!");
    _getch();
    return 0;
}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,546 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 81,831 Reputation points
    2020-11-22T15:19:04.917+00:00

    You must be Administrator and you must reboot the PC

    I tested on my OS (Windows 10, 1909) and the new name appears in

    ms-settings:about