GetAdaptersInfo получает разные результаты в режиме отладки и выпуска

Я пытаюсь получить первый Mac-адрес для чтения в Windows. (Windows Vista, Windows7, Windows8).

Я использую эту функцию:

char* CSoftwareKey::getMAC()
{
PIP_ADAPTER_INFO AdapterInfo;
DWORD dwBufLen = sizeof(AdapterInfo);
char *mac_addr = (char*)malloc(17);

AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
if (AdapterInfo == NULL) {
MessageBox(NULL, L"Error allocating memory needed to call GetAdaptersinfo 1 ", L"Error", MB_ICONERROR);

}

// 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);
if (AdapterInfo == NULL) {
MessageBox(NULL, L"Error allocating memory needed to call GetAdaptersinfo 2 ", L"Error", MB_ICONERROR);
}
}

if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR)
{
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
do {

sprintf_s(mac_addr, 32, "%c%c%c%c%c%c",
pAdapterInfo->Address[0], pAdapterInfo->Address[1],
pAdapterInfo->Address[2], pAdapterInfo->Address[3],
pAdapterInfo->Address[4], pAdapterInfo->Address[5]);
printf("Address: %s, mac: %s\n", pAdapterInfo->IpAddressList.IpAddress.String, mac_addr);
free(AdapterInfo);

return mac_addr;

printf("\n");
pAdapterInfo = pAdapterInfo->Next;
} while (pAdapterInfo);
}
free(AdapterInfo);
}

Мой Mac-адрес хорош в отладке и плох в режиме релиза! Кроме того, в режиме отладки адрес mac всегда отличается.

Я отключаю все оптимизации, и результаты такие же, плохие в релизе modd и хорошие в режиме отладки.

У кого-нибудь есть какие-то идеи?
Я использую VS2013 Pro.

Большое спасибо,

С наилучшими пожеланиями,

Nixeus

РЕДАКТИРОВАТЬ :

Я полностью исправил свой код, используя GetAdapterAdresses вместо GetAdapaterInfos.
Mac adresse, кажется, то же самое в моей функции, но возвращение, кажется, плохо в режиме выпуска:

#define MACADDR_SIZE (6*3)

char* CSoftwareKey::getMAC()
{
char * mac_addr = (char*)malloc(MACADDR_SIZE + 1);

// Declare and initialize variables
DWORD dwSize = 0;
DWORD dwRetVal = 0;
int i = 0;
ULONG flags, outBufLen = 0, family;
LPVOID lpMsgBuf;
PIP_ADAPTER_ADDRESSES pAddresses;
PIP_ADAPTER_ADDRESSES pCurrAddresses;
PIP_ADAPTER_UNICAST_ADDRESS pUnicast;
PIP_ADAPTER_ANYCAST_ADDRESS pAnycast;
PIP_ADAPTER_MULTICAST_ADDRESS pMulticast;
IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
IP_ADAPTER_PREFIX *pPrefix = NULL;

// Set the flags to pass to GetAdaptersAddresses
flags = GAA_FLAG_INCLUDE_PREFIX;
// default to unspecified address family (both)
family = AF_UNSPEC;
lpMsgBuf = NULL;
pAddresses = NULL;

pCurrAddresses = NULL;
pUnicast = NULL;
pAnycast = NULL;
pMulticast = NULL;

family = AF_INET;

outBufLen = sizeof (IP_ADAPTER_ADDRESSES);
pAddresses = (IP_ADAPTER_ADDRESSES *)malloc(outBufLen);
if (pAddresses == NULL)
{
printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct!\n");
exit(1);
}
else
printf("Memory allocation for IP_ADAPTER_ADDRESSES struct is OK!\n");

// Make an initial call to GetAdaptersAddresses to get the
// size needed into the outBufLen variable
if (GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW)
{
printf("Not enough buffer! Re-allocating...\n");
free(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES *)malloc(outBufLen);
}
else
printf("Buffer allocation is OK!\n");

if (pAddresses == NULL)
{
printf("Memory allocation failed for IP_ADAPTER_ADDRESSES struct!\n");
exit(1);
}
else
printf("Memory allocation for IP_ADAPTER_ADDRESSES struct is OK!\n");

// Make a second call to GetAdapters Addresses to get the actual data we want
printf("Memory allocated for GetAdapterAddresses = %d bytes\n", outBufLen);
printf("Calling GetAdaptersAddresses function with family = ");
if (family == AF_INET)
printf("AF_INET\n");

dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);

if (dwRetVal == NO_ERROR)
{
// If successful, output some information from the data we received
pCurrAddresses = pAddresses;

if (pCurrAddresses->PhysicalAddressLength != 0)
{
printf("\tPhysical address: ");
for (i = 0; i < (int)pCurrAddresses->PhysicalAddressLength; i++)
{
if (i == (pCurrAddresses->PhysicalAddressLength - 1))
printf("%.2X\n", (int)pCurrAddresses->PhysicalAddress[i]);
else
printf("%.2X-", (int)pCurrAddresses->PhysicalAddress[i]);
}
}

sprintf_s(mac_addr, MACADDR_SIZE+1, "%c%c%c%c%c%c",
pCurrAddresses->PhysicalAddress[0], pCurrAddresses->PhysicalAddress[1],
pCurrAddresses->PhysicalAddress[2], pCurrAddresses->PhysicalAddress[3],
pCurrAddresses->PhysicalAddress[4], pCurrAddresses->PhysicalAddress[5]);

free(pAddresses);

}
return mac_addr;
}

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]