InnoSetup脚本使用JSONConfig.dll读取JSON文件失败排查
问题分析与解决方案
你遇到的读取失败问题主要由JSON格式错误、字符串类型不匹配这两个核心问题导致,以下是具体排查和修复步骤:
1. 修正JSON文件的布尔值格式
你的Example.json中布尔值用了字符串形式("True"/"False"),但JSON规范中布尔值是原生类型,不需要加引号。JSONQueryBoolean函数仅识别原生布尔值,字符串形式会被判定为无效,直接返回默认值。
修正后的Example.json:
{ "Section_1": { "Key_1": "String 1", "Key_2": "1", "Key_3": true }, "Section_2": { "Key_1": "String 2", "Key_2": "2", "Key_3": false } }
2. 修复函数声明的字符串类型不匹配
原JSONConfig.dll是基于Unicode(WideString)编写的,你使用的Inno Setup 5.6.1如果是ANSI版本,直接替换WideString为String会导致编码不兼容,DLL无法正确解析传入的路径和键名参数。
解决方案:
- 切换到Inno Setup 5.6.1 Unicode版本(该版本支持
WideString),恢复函数声明的原始类型:
function JSONQueryString(FileName, Section, Key, Default: WideString; var Value: WideString; var ValueLength: Integer): Boolean; external 'JSONQueryString@files:jsonconfig.dll stdcall'; function JSONQueryBoolean(FileName, Section, Key: WideString; Default: Boolean; var Value: Boolean): Boolean; external 'JSONQueryBoolean@files:jsonconfig.dll stdcall';
- 若必须使用ANSI版本,需要重新编译
JSONConfig.dll为ANSI兼容版本,或确保所有字符串参数编码一致(不推荐,容易引发乱码问题)。
3. 优化变量处理逻辑
- 初始化未定义的变量:
BoolValue在使用前未初始化,虽然函数会赋值,但建议显式初始化避免随机值干扰日志输出:BoolValue := False; - 截取有效字符串长度:调用
JSONQueryString后,StrLength会返回实际读取的字符长度,需截取有效部分避免空字符残留:if JSONQueryString(FileName, 'Section_1', 'Key_1', 'Default', StrValue, StrLength) then begin StrValue := Copy(StrValue, 1, StrLength); // 新增这行 MsgBox('Section_1:Key_1=' + StrValue, mbInformation, MB_OK); end;
修正后的完整代码示例
[Setup] AppName=My Program AppVersion=1.5 DefaultDirName={pf}\My Program OutputDir=userdocs:Inno Setup Examples Output [Files] Source: "C:\inno-json-config-master\JSONConfig.dll"; Flags: dontcopy [code] function JSONQueryString(FileName, Section, Key, Default: WideString; var Value: WideString; var ValueLength: Integer): Boolean; external 'JSONQueryString@files:jsonconfig.dll stdcall'; function JSONQueryBoolean(FileName, Section, Key: WideString; Default: Boolean; var Value: Boolean): Boolean; external 'JSONQueryBoolean@files:jsonconfig.dll stdcall'; function BoolToStr(Value: Boolean): string; begin Result := 'True'; if not Value then Result := 'False'; end; procedure InitializeWizard; var FileName: WideString; StrValue: WideString; StrLength: Integer; BoolValue: Boolean; begin FileName := 'C:\inno-json-config-master\Example.json'; if FileExists(FileName) then begin Log('file found'); end else begin Log('file not found'); end; SetLength(StrValue, 16); StrLength := Length(StrValue); BoolValue := False; // 初始化布尔变量 Log('StrValue_before:' + StrValue); Log('BoolValue_before: ' + BoolToStr(BoolValue)); if JSONQueryString(FileName, 'Section_1', 'Key_1', 'Default', StrValue, StrLength) then begin StrValue := Copy(StrValue, 1, StrLength); MsgBox('Section_1:Key_1=' + StrValue, mbInformation, MB_OK); end; Log('Section_1:Key_1=' + StrValue); if JSONQueryBoolean(FileName, 'Section_1', 'Key_3', True, BoolValue) then begin MsgBox('Section_1:Key_3=' + BoolToStr(BoolValue), mbInformation, MB_OK); end; Log('Section_1:Key_3=' + BoolToStr(BoolValue)); if JSONQueryBoolean(FileName, 'Section_2', 'Key_3', True, BoolValue) then begin MsgBox('Section_2:Key_3=' + BoolToStr(BoolValue), mbInformation, MB_OK); end; Log('Section_2:Key_3=' + BoolToStr(BoolValue)); end;
额外排查点
- 确认
JSONConfig.dll与Inno Setup的位数匹配(32位Inno Setup对应32位DLL); - 检查文件读取权限:若JSON文件位于系统保护目录(如C:\根目录),尝试将文件移至非保护路径测试。
内容的提问来源于stack exchange,提问作者bibi
相关产品推荐
相关产品推荐

