通过注册表改变Windows下WLAN的Mac地址

本文介绍了如何在Windows系统中通过注册表改变WLAN的Mac地址,包括参考链接、具体代码、操作注意事项及运行步骤。需要注意不同电脑的注册表路径可能不同,并提醒了关于2、6、A、E限制的解决办法,以及管理员权限运行和重启网卡的重要性。

参考链接

  1. 3种方法来在Windows中变更电脑的Mac地址
    https://zh.wikihow.com/在Windows中变更电脑的Mac地址
  2. Registry Functions - Windows applications | Microsoft Docs
    https://docs.microsoft.com/zh-cn/windows/win32/sysinfo/registry-functions

代码

#include <windows.h>
#include <stdio.h>
#include <string.h>

void showErrorText(DWORD error_num);
 
int main()
{
    HKEY hKey;
    DWORD result;
    char sname[13];

    //打开注册表
    result = RegOpenKeyEx(
                 HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}\\0003", 
                 0,              // 保留参数必须填 0
                 KEY_WRITE,      // 打开权限,写入
                 &hKey           // 打开之后的句柄
             );
 
    if (result == ERROR_SUCCESS)
    {
        printf("open success!\n");
    }
    else
    {
        printf("open failed!\n");
        showErrorText(result);
        exit(-1);
    }
	
	//输入新的mac地址
	while(strlen(sname) !=12)
	{
		memset(sname,0,sizeof(sname));
		printf("please input a new mac address:"); 
		scanf("%12s",sname) ;
	}

    // 设置 NetworkAddress
    result = RegSetValueEx(
                 hKey,
                 "NetworkAddress",         // Name字段
                 0,              // 保留参数必须填 0
                 REG_SZ,         // 键值类型为字符串
                 (const unsigned char *)sname, // 字符串首地址
                 strlen(sname)   // 字符串长度
             );

    if (result == ERROR_SUCCESS)
    {
        printf("set success!\n");
    }
    else
    {
        printf("set failed!\n");
        showErrorText(result);
        exit(-2);
    }

    //关闭注册表
    RegCloseKey(hKey);
    // 重启网卡 
    printf("禁用网卡\n");
    system("netsh interface set interface \"WLAN\" disabled");
    printf("启用网卡\n");
    system("netsh interface set interface \"WLAN\" enabled");
    //暂停
	system("pause"); 
    return 0;
}
 
/*
 * 根据错误码输出错误信息
 */
void showErrorText(DWORD error_num)
{
    char *msg = NULL;
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        error_num,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // 使用默认语言
        (LPSTR)&msg,
        0,
        NULL
    );
 
    printf("Error code %d: \n", error_num);
    if (msg == NULL)
    {
        printf("%s\n", "Unknown error");
	}
    else
    {
        printf("%s\n", msg);
	}
	
	system("pause"); 
}

注意

不同的电脑的wlan的注册表地址可能不一样,最后一项可能不是0003,可能是别的数字
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class{4d36e972-e325-11ce-bfc1-08002be10318}\0003
也可以用regedit直接更改注册表

不同的网卡,在禁用、启用网卡的时候输入不同的命令,
也可以在 控制面板→网络和共享中心→更改适配器设置 里面禁用、启用网卡。

运行步骤

先看一下参考链接里的第一条。
程序运行需要管理员权限
如果没用管理员权限运行就会:
在这里插入图片描述
管理员权限运行方法:

  1. 右键→以管理员身份运行
  2. 右键→属性→兼容性→以管理员身份运行此程序
  3. 在程序名中加入setup,比如将程序重命名为change_mac_setup.exe

确认输入格式正确的MAC地址。某些适配器(尤其是某些无线网卡)的MAC地址中第一个八位字节的第二个字符如果是2、6、A、E或以0开头,那么这些地址可能无法被更改。

重启网卡
在这里插入图片描述

绕过2、6、A、E的限制

使用winXP或Linux虚拟机

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值