简体   繁体   中英

sprintf_s failed in release mode, and ok in debug

In C++, with VS2012, i would like to get the Mac adress and convert it in car formatting. I Use sprintf_s function() in order to convert in car mode. In debug mode, all is ok, but, in release mode, the sprint_s function don't execute properly ! It appear to exit my programm !

I don't know why !

Anyone could help me please ?

Here is a little sample which demonstrate my problem. I tested in on severals pc'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;
}

Thanks a lot :)

Best regards,

Nixeus

You can't use %c to print the bytes of a MAC address, as those bytes may not be printable characters.

The code that printf "Physical address" using %.2X is the one to use.

Your code works well in Debug or Release mode for me (VS2012), but as my first NIC has 0x00 as first byte, the final printf prints nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM