0

这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma comment(lib,"ws2_32.lib")
#include "stdafx.h"
#include <assert.h>
#include "Bootpd.h"
#include <iostream>
#include <string>
#include <iphlpapi.h>


char *MAC() {
    PIP_ADAPTER_INFO AdapterInfo;
    DWORD dwBufLen = sizeof(AdapterInfo);
    char *mac_addr = (char*)malloc(20);

    AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
    assert(AdapterInfo != NULL); //Error allocating memory

    // Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
    if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
        AdapterInfo = (IP_ADAPTER_INFO *)malloc(dwBufLen);
        assert(AdapterInfo != NULL);
    }

    if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
        PIP_ADAPTER_INFO info = AdapterInfo; //Copy information
        sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
            info->Address[0], info->Address[1],
            info->Address[2], info->Address[3],
            info->Address[4], info->Address[5]);
    }
    free(AdapterInfo);
    return mac_addr;
}

我在 Windows 10 上使用 VS 2015。我正在尝试将网络适配器的 MAC 地址信息格式化为 MAC 地址 ( aa:bb:cc:dd:ee:ff)。我已经尝试在我的陈述上方定义_CRT_SECURE_NO_WARNINGS和禁用警告但没有成功。有什么我遗漏的,或者有人知道解决这个错误的不同方法吗?谢谢。4996#includesprintfvariable may be unsafe

4

1 回答 1

1

编译器抱怨可能会超出 mac_addr 数组。给 sprintf_s 一个机会。https://en.cppreference.com/w/c/io/fprintf

于 2018-07-06T20:23:45.680 回答