sprintf_s在发布模式下失败,在调试中没问题

时间:2013-11-27 21:27:30

标签: c++ visual-studio-2010 api visual-c++ printf

在C ++中,使用VS2012,我想获得Mac地址并将其转换为汽车格式。 我使用sprintf_s function()来转换为汽车模式。 在调试模式下,一切正常,但是在发布模式下,sprint_s功能无法正常执行!它似乎退出了我的程序!

我不知道为什么!

有人可以帮我吗?

这是一个展示我问题的小样本。我在几台电脑上测试过:

#include "stdafx.h"
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "iphlpapi.lib")
#define MACADDR_SIZE (6*3)


#define MAC_DIM                 6
#define MACHINE_CODE_DIM        6
#define SOFTWAREKEY_DIM         6

enum RETVALUE
{
    SUCCESS,
    ERROR_API_CALL_FAILED,
    ERROR_FAILURE_WHILE_LOADING_LIBRARY,
    ERROR_OS_VERSION_NOT_SUPPORTED,
    ERROR_SOFTWAREKEY_NOT_FOUND,
    ERROR_CONVERSION_CHAR_2_WCHAR_FAILED
};



int _tmain(int argc, _TCHAR* argv[])
{

    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]);

        printf("Mac adress in car formatting is :");
        printf(mac_addr);   
        getchar();
        free(pAddresses);

    }

    return 0;
}

非常感谢:)

致以最诚挚的问候,

Nixeus

1 个答案:

答案 0 :(得分:2)

您无法使用%c打印MAC地址的字节,因为这些字节可能不是可打印的字符。

使用printf %.2X“物理地址”的代码是可以使用的代码。

您的代码在我的调试或发布模式下运行良好(VS2012),但由于我的第一个NIC的第一个字节为0x00,因此最终的printf不会打印任何内容。