gpt4 book ai didi

c++ - sprintf_s 在 Release模式下失败,在 Debug模式下正常

转载 作者:行者123 更新时间:2023-11-30 04:11:23 25 4
gpt4 key购买 nike

在 C++ 中,使用 VS2012,我想获取 Mac 地址并将其转换为汽车格式。我使用 sprintf_s function() 以便在汽车模式下进行转换。在 Debug模式下,一切正常,但在 Release模式下,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;
}

非常感谢:)

最好的问候,

尼克修斯

最佳答案

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

使用%.2Xprintf“物理地址”的代码就是要用到的。

你的代码对我来说在调试或 Release模式下运行良好(VS2012),但由于我的第一个 NIC 的第一个字节是 0x00,所以最后的 printf 什么都不打印。

关于c++ - sprintf_s 在 Release模式下失败,在 Debug模式下正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20253396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com