encoding - 在 Inno Setup 中以 UTF-8 而不是 ANSI 格式保存 INI 文件

标签 encoding utf-8 inno-setup ini codepages

我开始使用 Inno Setup,但我的 INI 文件编码有一些问题。
我想将用户输入保存在 INI 文件中,并且此输入可以包含重音符号。

我使用 Inno Setup Unicode,我的 setupScript.iss 是 UTF-8 编码的,这是我的代码(一部分):

[INI]
Filename: "{app}\www\conf\config.ini"; Section: "Settings"; Key: "ca.plafondAnnuel"; String: "{code:GetUser|Plafond}"
Filename: "{app}\www\conf\config.ini"; Section: "Settings"; Key: "app.siren"; String: "{code:GetUser|Siren}"
Filename: "{app}\www\conf\config.ini"; Section: "Settings"; Key: "app.adresse"; String: "{code:GetUser|Adresse}"


[Code]
var
  UserPage: TInputQueryWizardPage;
  ExamplePage : TInputOptionWizardPage;
  ImmatriculationPage : TInputOptionWizardPage;
  FakeElemIndex: Integer;
  FakeElem: TCustomEdit;
  AdresseTextarea: TNewMemo;

procedure InitializeWizard;
begin
  UserPage := CreateInputQueryPage(wpWelcome,
    'Configuration de l''application', '',
    'Configurez ici votre application. Une fois installée, vous pourrez modifier ces valeurs.');

  UserPage.Add('Siren :', False);
  UserPage.Add('Plafond annuel (utilisé par les auto-entreprises, mettre 0 si vous ne souhaitez pas plafonner votre chiffre d''affaire.):', False);

  FakeElemIndex := UserPage.Add('Votre adresse complète (telle qu''elle s''affichera sur les devis et factures, avec nom complet):', False);
  FakeElem  := UserPage.Edits[FakeElemIndex];

  AdresseTextarea := TNewMemo.Create(WizardForm);
  AdresseTextarea.Parent := FakeElem.Parent;
  AdresseTextarea.SetBounds(FakeElem.Left, FakeElem.Top, FakeElem.Width, ScaleY(50));

  // Hide the original single-line edit
  FakeElem.Visible := False;
end; 

function GetUser(Param: String): String;
begin
  if Param = 'Adresse' then
    Result := AdresseTextarea.Text
  else if Param = 'Siren' then
    Result := UserPage.Values[0]
  else if Param = 'Plafond' then
    Result := UserPage.Values[1];
end;
getUser|Adresse 返回的值在 [INI]部分不是 UTF-8 编码的:我用 Notepad++ 打开 INI 文件,我看到文件是 UTF-8 编码的。但是值adresse是 ANSI 编码的(如果我将文件的编码更改为 ANSI,则此值是可读的)

有人可以帮助我了解如何以 UTF-8 保存此用户输入?

非常感谢 !

最佳答案

Inno Setup 的 INI 函数([INI] 部分和 SetIni* 函数)在内部使用 Windows API 函数 WritePrivateProfileString .
此函数根本不支持 UTF-8。它只支持 ANSI 编码和 UTF-16。
How to read/write Chinese/Japanese characters from/to INI files?
因此,目标应用程序是否能够读取 UTF-8 编码的 INI 文件甚至是值得怀疑的,如果它依赖于 Windows API 函数来读取它。

无论如何,如果您需要 UTF-8,您必须自己将条目格式化为 INI 格式并使用 SaveStringsToUTF8File function写它。

最后一个选项是使用系统调用 WritePrivateProfileString 破解它。编写看似 ANSI 编码的字符串,实际上是 UTF-8 编码的。
为此,您需要在代码中将字符串转换为 UTF-8。您可以使用 WideCharToMultiByte 为了那个原因。

function WideCharToMultiByte(CodePage: UINT; dwFlags: DWORD;
  lpWideCharStr: string; cchWideChar: Integer; lpMultiByteStr: AnsiString;
  cchMultiByte: Integer; lpDefaultCharFake: Integer;
  lpUsedDefaultCharFake: Integer): Integer;
  external 'WideCharToMultiByte@kernel32.dll stdcall';

const
  CP_UTF8 = 65001;

function GetStringAsUtf8(S: string): AnsiString;
var
  Len: Integer;
begin
  Len := WideCharToMultiByte(CP_UTF8, 0, S, Length(S), Result, 0, 0, 0);
  SetLength(Result, Len);
  WideCharToMultiByte(CP_UTF8, 0, S, Length(S), Result, Len, 0, 0);
end;

function WritePrivateProfileString(
  lpAppName, lpKeyName, lpString, lpFileName: AnsiString): Integer;
  external 'WritePrivateProfileStringA@kernel32.dll stdcall';

procedure CurStepChanged(CurStep: TSetupStep);
var
  IniFileName: string;
begin
  if CurStep = ssInstall then
  begin
    Log('Writting INI file');
    if not ForceDirectories(ExpandConstant('{app}\www\conf')) then
    begin
      MsgBox('Error creating directory for INI file', mbError, MB_OK);
    end
      else
    begin
      IniFileName := ExpandConstant('{app}\www\conf\config.ini');
      if (WritePrivateProfileString(
            'Settings', 'ca.plafondAnnuel', GetStringAsUtf8(GetUser('Plafond')),
            IniFileName) = 0) or
         (WritePrivateProfileString(
            'Settings', 'app.siren', GetStringAsUtf8(GetUser('Siren')),
            IniFileName) = 0) or
         (WritePrivateProfileString(
            'Settings', 'app.adresse', GetStringAsUtf8(GetUser('Adresse')),
            IniFileName) = 0) then
      begin
        MsgBox('Error writting the INI file', mbError, MB_OK);
      end;
    end;
  end;
end;

关于encoding - 在 Inno Setup 中以 UTF-8 而不是 ANSI 格式保存 INI 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35117362/

相关文章:

jenkins - 使用 Jenkins 创建 Inno Setup 安装程序,无需在代理上预安装

c# - 针对 .NET DataView RowFilter 的注入(inject)攻击

algorithm - 将数字列表压缩或编码为单个字母数字字符串的最佳方法是什么?

javascript - 告诉浏览器用连字符替换 UTF-8 分词符?

node.js - 如何通过nodejs以正确的编码保存pdf

inno-setup - 在 Inno Setup 中使用 "taskkill/f/im"在(重新)安装之前杀死进程

encoding - Clojure base64 编码

json - 结果打印空 Json

mysql - 将数据库从一种字符编码转换为另一种字符编码

inno-setup - 具有三个按钮和三个结果的 Inno Setup MsgBox