Using InnoSetup to Convert LF to CRLF For Read-Only Files

My application setup was tasked with installing README.txt file. As Windows versions before 10 have trouble dealing with LF characters, I needed a way to convert line endings to CRLF.

I thought that was easy, I just needed to call AfterInstall:

Source: "..\README.md"; DestDir: "{app}"; DestName: "ReadMe.txt"; Attribs: readonly; AfterInstall: AdjustTextFile;

and then call a simple function:

[code]
procedure AdjustTextFile();
var
  path : String;
  data : String;
begin
  path := ExpandConstant(CurrentFileName)
  LoadStringFromFile(path, data);
  StringChangeEx(data, #10, #13#10, True);
  SaveStringToFile(path, data, False);
end;

However, this didn’t work. While code seemingly worked without an issue, it wouldn’t save to read-only file. Yep, our read-only flag was an issue.

While this meant I had to change my InnoSetup project to install file without read-only attribute set, nothing prevented me setting attribute (after I wrote corrected line endings) using a bit of interop magic:

[code]
function SetFileAttributes(lpFileName: string; dwFileAttributes: LongInt): Boolean;
  external 'SetFileAttributesA@kernel32.dll stdcall';

procedure AdjustTextFile();
var
  path : String;
  data : String;
begin
  path := ExpandConstant(CurrentFileName)
  LoadStringFromFile(path, data);
  StringChangeEx(data, #10, #13#10, True);
  SaveStringToFile(path, data, False);
  SetFileAttributes(path, 1);
end;