VS解除strcpy_s的错误

本文介绍了解决使用strcpy函数时遇到的安全性警告问题,微软认为直接使用strcpy不安全,推荐使用strcpy_s替代。但考虑到代码移植性,文章提供了两种解决方案:一是在编译器设置中添加预处理器定义_CRT_SECURE_NO_WARNINGS;二是在代码中直接添加预定义宏#define _CRT_SECURE_NO_WARNINGS。这两种方法都可以避免编译器因使用strcpy而产生的警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此方法出错,并非真正有什么错误,而是微软认为这样用不安全,微软推荐用strcpy_s代替。

但对于strcpy_s并非出自标准C,不方便代码的移植,为了阻止编译器报错,可以点击工程属性,

"Configuration Properties"->"C/C++"->"Preprocessor"->"Preprocessor “

如果是vs2017中文版,则是 “项目“->”属性页“->"C/C++"->”预处理器“->”预处理器定义“ 中添加“_CRT_SECURE_NO_WARNINGS”即可。

或者在代码中添加#define _CRT_SECURE_NO_WARNINGS

void start_qrcode(struct sample_media_configs_2 *ai_media_configs) { pthread_t qrcode_thread; int ret = 0; ret = pthread_create(&qrcode_thread, NULL, qrcode_run_thread, ai_media_configs); pthread_detach(qrcode_thread); CHECK_RET_SUCCESS(ret); } #if USE_XY == 1 extern int StartXy(); #else extern int webrtc_start(); extern int webrtc_stop(); #endif int LoadXyConfig() { // 获取xyconfig的值 char value[MAX_VALUE_LENGTH]; memset(&g_xy_info, 0, sizeof(XY_QRCODE_INFO_S)); int ret = read_ini_file("xyconfig.ini", "system", "sid", value); if (ret != INI_OK) { g_xy_info.isBind = 0; return -1; } strcpy(g_xy_info.s, value); ret = read_ini_file("xyconfig.ini", "system", "pwd", value); if (ret != -1) strcpy(g_xy_info.p, value); ret = read_ini_file("xyconfig.ini", "system", "isBind", value); if (ret != -1 && value[0] == '1') { g_xy_info.isBind = 1; } else { g_xy_info.isBind = 0; } printf("func = %s, sid = %s, pwd = %s, bindstate=%d\n", __FUNCTION__, g_xy_info.s, g_xy_info.p, g_xy_info.isBind); return 0; } void LoadKo() { system("/bin/sh /home/zn/setdev.sh"); system("/bin/sh /home/zn/setko.sh"); } void signalHandler(int sig_num) { g_isExit = 1; fflush(stdout); } XMEDIA_S32 SetRoi(int man) { XMEDIA_S32 s32Ret = XMEDIA_FAILURE; VENC_ROI_ATTR_S stRoiAttr; VENC_CHN_ATTR_S stChnAttr; XMEDIA_S32 index = 0; VENC_CHN VeChnId = 0; //...omit other thing s32Ret = XMEDIA_API_VENC_GetChnAttr(VeChnId, &stChnAttr); if (XMEDIA_SUCCESS != s32Ret) { printf("XMEDIA_API_VENC_GetChnAttr err 0x%x\n", s32Ret); return XMEDIA_FAILURE; } s32Ret = XMEDIA_API_VENC_GetRoiAttr(VeChnId, index, &stRoiAttr); if (XMEDIA_SUCCESS != s32Ret) { printf("XMEDIA_API_VENC_GetRoiAttr err 0x%x\n", s32Ret); return XMEDIA_FAILURE; } else { printf(" stRoiAttr.stRect.s32X = %d;stRoiAttr.stRect.s32Y = %d; stRoiAttr.stRect.u32Width = %d; stRoiAttr.stRect.u32Height = %d;\n", stRoiAttr.stRect.s32X, stRoiAttr.stRect.s32Y, stRoiAttr.stRect.u32Width, stRoiAttr.stRect.u32Height); } stRoiAttr.bEnable = XMEDIA_TRUE; stRoiAttr.bAbsQp = XMEDIA_TRUE; stRoiAttr.s32Qp = 10; if (0 == man) { stRoiAttr.stRect.s32X = 0; stRoiAttr.stRect.s32Y = 0; stRoiAttr.stRect.u32Width = 160; stRoiAttr.stRect.u32Height = 160; } else if (1 == man) { stRoiAttr.stRect.s32X = 0; stRoiAttr.stRect.s32Y = 0; stRoiAttr.stRect.u32Width = 165; stRoiAttr.stRect.u32Height = 160; } else if (2 == man) { stRoiAttr.stRect.s32X = 0; stRoiAttr.stRect.s32Y = 0; stRoiAttr.stRect.u32Width = 160; stRoiAttr.stRect.u32Height = 165; } else { return XMEDIA_FAILURE; } stRoiAttr.u32Index = 0; s32Ret = XMEDIA_API_VENC_SetRoiAttr(VeChnId, &stRoiAttr); if (XMEDIA_SUCCESS != s32Ret) { printf("XMEDIA_API_VENC_SetRoiAttr err 0x%x\n", s32Ret); return XMEDIA_FAILURE; } return XMEDIA_SUCCESS; } void *thread_SD_status_proc(void *arg) { zn_monitor_netlink_uevent(); } void *thread_osd_time_proc(void *arg) { printf("into %s\n", __func__); struct test_resource *res = (struct test_resource *)arg; char buffer[20] = {0}; //"2025-03-05 09:30:00" char new_buffer[20] = {0}; int flag = 0; time_t rawtime; struct tm *timeinfo; rgn_handle_create(res); // 循环时间输出时间水印 while (1) { if (g_osd_enabled) { // 当开关为 true 时,绑定通道,添加水印 rgn_bind_chn(res); time(&rawtime); timeinfo = localtime(&rawtime); // 转换为本地时间 // 格式化时间为 "YYYY-MM-DD HH:MM:SS" strftime(new_buffer, sizeof(new_buffer), "%Y-%m-%d %H:%M:%S", timeinfo); rgn_add(res, new_buffer); } else { // 当开关为 false 时,解除绑定,确保不显示水印 rgn_unbind_chn(res); } // 每秒更新一次 sleep(1); } }注释一下代码
最新发布
07-15
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值