Here's my code:
#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;
}
I am using VS 2015 on Windows 10. I am trying to format my network adapter's MAC address information to look like a MAC address (aa:bb:cc:dd:ee:ff
). I already tried defining _CRT_SECURE_NO_WARNINGS
and disabling warning 4996
above my #include
statements with no success. Is there anything I am missing, or does anybody know a different work around to get rid of the sprintf
error variable may be unsafe
? Thanks.
The compiler is complaining about the possibility of overrunning the mac_addr array. Give sprintf_s a shot. https://en.cppreference.com/w/c/io/fprintf