Home > Software design >  What is "ANSI encoding" in NSIS?
What is "ANSI encoding" in NSIS?

Time:11-27

I have an application that is installed using NSIS 3.x. The installer stores a path in a file that will be later checked by the application. The path is written using the FileWrite function; the documentation for FileWrite states the following:

Writes an ANSI string to a file opened with FileOpen. If an error occurs writing, the error flag will be set.

(If you are building a Unicode installer, the function makes the adequate conversion and writes an ANSI string)

I know that ANSI is a loosely defined term (see this question, and its many answers). Is there a way to find out what is the actual encoding used by this NSIS function, so that I can later read the generated file properly from the application?

CodePudding user response:

It is the Windows meaning of ANSI; it is the currently active/default Windows codepage.

Before NSIS v3, NSIS was always "ANSI". It called the "A" versions of all API functions. Internally every string was just treated as bytes and no conversions were done. This is why FileWrite effectively wrote ANSI strings.

Unicode NSIS installers call WideCharToMultiByte(CP_ACP, ...) to convert the Unicode string before calling FileWrite.

It should be considered safe and portable for ASCII but not other characters. Another machine might be configured with a different codepage as the default and not all Unicode characters can be converted correctly even on the same machine. The only exception would be Windows 10/11 configured to use UTF-8 as the default or if you opt-in in the manifest.

If the application you are installing is also Unicode enabled then you should be using FileWriteUTF16LE.

You can also write Unicode .ini files:

FileOpen $0 $INSTDIR\cfg.ini a
FileWriteUTF16LE /BOM $0 "" ; Make sure it has a BOM
FileClose $0
WriteIniStr $INSTDIR\cfg.ini Paths App $InstDir
  • Related