GetAdaptersInfo在调试和发布模式下获得不同的结果

时间:2013-11-26 13:30:40

标签: c++ c visual-studio-2010 mac-address

我正试图让Windows中的第一个mac地址可读。 (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 个答案:

没有答案