You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Visual Studio中引入json-c库解析JSON时遇错误的解决咨询

解决Windows驱动中引入json-c头文件的编译错误问题

问题场景

在Visual Studio中编写Windows内核驱动(C语言)时,引入json-c头文件后出现宏重定义、语法警告及警告被视为错误等编译失败问题。

代码片段

#include<ntddk.h>
#include<wdf.h>
#include<fwpmk.h>
#include<fwpsk.h>
#define INITGUID
#include<guiddef.h>
#include<fwpmu.h>

#undef _CRT_STRINGIZE
#undef _CRT_WIDE
#undef __CRTDECL
#include<json-c/json.h>
#include<json-c/json_object.h>

编译错误信息

Severity    Code    Description Project File    Line    Suppression State
Warning C6067   _Param_(2) in call to 'DbgPrint' must be the address of a string. Actual type: 'void *'.    WfpcalloutDriver3   C:\Users\hp\Desktop\VK\WfpcalloutDriver3\Source.c   88  
Warning C4083   expected ')'; found identifier '_VCRUNTIME_DISABLED_WARNINGS'   WfpcalloutDriver3   C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\stdint.h    16  
Error   C2220   the following warning is treated as an error    WfpcalloutDriver3   C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vcruntime.h 61  
Warning C4083   expected ')'; found identifier '_VCRUNTIME_DISABLED_WARNINGS'   WfpcalloutDriver3   C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vcruntime.h 61  
Warning C4005   '_CRT_STRINGIZE': macro redefinition    WfpcalloutDriver3   C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vcruntime.h 111 
Warning C4005   '_CRT_WIDE': macro redefinition WfpcalloutDriver3   C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vcruntime.h 114 
Warning C4005   '__CRTDECL': macro redefinition WfpcalloutDriver3   C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vcruntime.h 155 

解决方法

1. 先处理"警告视为错误"问题

打开项目属性:

  • 进入C/C++ -> 常规
  • 将"将警告视为错误"设置为否(若需保持严格编译,可后续针对特定警告单独禁用)

2. 解决宏重定义与用户态头文件冲突

json-c是为用户态程序设计的库,依赖用户态CRT,与内核驱动环境不兼容。解决思路:

  • 替换内核态JSON库:使用专为内核设计的JSON解析库,比如微软的ntjson(需Windows 10 1709及以上版本支持),或轻量的内核态JSON实现,彻底避免用户态依赖冲突。
  • 调整头文件包含逻辑:若坚持使用json-c,在包含其头文件前添加以下宏定义,阻断用户态CRT头文件的引入:
    #define _KERNEL_MODE
    #define NO_CRT_STDIO_INCLUDE
    #undef _CRT_STRINGIZE
    #undef _CRT_WIDE
    #undef __CRTDECL
    
    确保上述定义放在#include<json-c/json.h>之前。

3. 修复C6067警告

定位到Source.c第88行的DbgPrint调用,第二个参数要求为字符串指针,你传入了void*类型,需强制转换为PVOID:

// 错误示例
DbgPrint("Some message: %p", ptr);
// 修正后
DbgPrint("Some message: %p", (PVOID)ptr);

4. 解决C4083语法警告

该警告源于json-c引入的用户态头文件与内核头文件的宏冲突,通过步骤2中定义_KERNEL_MODE可避免vcruntime.h等用户态头文件被引入,从而解决该问题。


内容的提问来源于stack exchange,提问作者vk_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 13:18:09