I have recently used Inno Setup for my Java software. I am writing a function to check whether a printer driver exists by calling wmic printer get name /All
and reading it output. But the problem is when I am reading the text file and check if it contains a specific substring by Pos()
, it always returning 0, but when I tried to test with a character it returned the true value. I'm currently using version 5.6.1 Unicode.
I have looked at Delphi Pos always returning 0 but I think it's not my case:
Here is how I did it:
function isContainedInFile(File, Substring: String): Boolean;
var
Lines: TArrayOfString;
i: Integer;
line: String;
begin
Substring := Uppercase(Substring);
Result := False;
if LoadStringsFromFile(File, Lines) then
begin
for i:= 0 to GetArrayLength(Lines) - 1 do
begin
line := Lines[i];
if (Length(line) = 0) then
continue;
line := Uppercase(Trim(line));
Log('Substring:' + Substring + ', Line:' + line + ', Pos:' + IntToStr(Pos(Substring, line)));
if (Pos(Substring, line) <> 0) then
begin
Result:= True;
break;
end;
end;
end;
end;
This is how I called the isContainedInFile()
:
function IsBrotherDriverInstalled(): Boolean;
var
path, brotherPath, ListPrinterPath, ListPrinter: String;
check, index: Integer;
begin
ListPrinterPath := ExpandConstant('{tmp}\printerlist.tdm');
{ Save temporarily the list }
Exec(ExpandConstant('{cmd}'), '/c wmic printer get name /All > "' + ListPrinterPath + '"',
'', SW_HIDE, ewWaitUntilTerminated, check);
{ Check if the list has the printer }
Result := isContainedInFile(ListPrinterPath, PrinterName);
{ Delete the file }
DeleteFile(ListPrinterPath);
end;
Here is my output when the substring has length > 1:
And when the substring has length = 1:
Thanks in advance.
wmic
uses UTF-16 encoding in its output. LoadStringsFromFile
does not support UTF-16 encoding. See Inno Setup Pascal Script - Reading UTF-16 file.
So the file is read incorrectly.
You seem to be using Inno Script Studio IDE. Its Messages console does not print messages accurately, so it obfuscates the real problem. Had you used original Inno Setup Compiler IDE or checked a physical log file, you would see the problem straight away:
2018-08-26 10:44:35.783 Substring:BROTHER, Line:ÿþN A M E, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:S E N D T O O N E N O T E 2 0 1 6, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:S A M S U N G S C X - 3 4 0 0 S E R I E S ( U S B 0 0 1 ), Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:M S P U B L I S H E R C O L O R P R I N T E R, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:M I C R O S O F T X P S D O C U M E N T W R I T E R, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:M I C R O S O F T P R I N T T O P D F, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:H P E P R I N T + J E T A D V A N T A G E, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:F A X, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
2018-08-26 10:44:35.783 Substring:BROTHER, Line:, Pos:0
Solutions: