Hey Maurobio,
I think you're getting stuck on what could be called a difference of philosophy.
Let's see if I can't explain myself...
Linux, from the get go, has been planned to run binaries, no matter from where they originate. This means that they will always run,no matter if they have been initiated from a shell, or a GUI application.
Windows, from the get go, has been designed to ALWAYS have a GUI involved. Hence the fact that even
cmd.exe MUST spawn a visual window in which then it runs what was asked of it.
To be honest, in my opinion, the outsider here is Windows, It's insistence that everything should have a window shown(Yes big irony it's called Windows for a reason, LOL) is a bit nonsensical.
But that's just my opinion and it won't solve your problem, so back to your problem...
So let's try and make things the same for each OS, and by that I mean, let's ask both to run our
count inside their respective visual command line hosts:
- For Windows we'll ask cmd.exe to run our command
- For Linux we will use the function DetectXTerm to give us what is the system's visual command line host
- This usually defaults to the gnome-terminal for GNOME based systems
- Or it defaults to konsole on a KDE based system
If a system is well configured, it should have a wrapper for the default Terminal app.
What
DetectXTerm should return is either that wrapper or the actual Terminal app itself.
Looking at the source of
DetectXTerm you can see that both
gnome-terminal and
konsole are there and some of the older Terminal apps are also listed.
That wrapper, under modern Linux is usually
x-terminal-emulator.
This is usually a symlink to a shell script that will eventually call the intended Terminal app.
On my Ubuntu 21.04 64b this is where it all resides:
$ whereis x-terminal-emulator
x-terminal-emulator: /usr/bin/x-terminal-emulator
$ ll /usr/bin/x-terminal-emulator
lrwxrwxrwx 1 root root 37 Aug 17 2017 /usr/bin/x-terminal-emulator -> /etc/alternatives/x-terminal-emulator*
$ ll /etc/alternatives/x-terminal-emulator
lrwxrwxrwx 1 root root 31 Aug 17 2017 /etc/alternatives/x-terminal-emulator -> /usr/bin/gnome-terminal.wrapper
That last file is the shell script that eventually calls the GNOME Terminal app and while looking at it's source I found out that you need to use the param "-e" to pass the program you want the Terminal to run.
With that in mind, why don't you try this:
procedure TForm1.Button2Click(Sender: TObject);
var
ShellProg, CmdLine, Datafile: string;
begin
{$IFDEF WINDOWS}
ShellProg:= 'cmd.exe';
CmdLine:= ExtractFilePath(Application.ExeName)+'counter.exe';
{$ELSE}
ShellProg:= DetectXTerm;
CmdLine := ExtractFilePath(Application.ExeName)+'counter';
{$ENDIF}
Datafile := {ExtractFilePath(Application.ExeName)+}'lines.txt'; // Not sure it needs full path, so I'm leaving the comment in
AProcess := TProcess.Create(nil);
try
AProcess.Executable := ShellProg;
{$IFDEF WINDOWS}
AProcess.Parameters.Add('/c');
{$ELSE}
AProcess.Parameters.Add('-e');
{$ENDIF}
AProcess.Parameters.Add(CmdLine);
AProcess.Parameters.Add(Datafile);
AProcess.Options := AProcess.Options + [poWaitOnExit];
AProcess.Execute;
if AProcess.ExitCode <> 0 then
ShowMessageFmt('Error: %s reports error code %d', [CmdLine, AProcess.ExitCode]);
finally
AProcess.Free;
end;
end;
It works on my Ubuntu 21.04 64b and I was also able to make it work under
wine doing a dir, cuz I don't have a C cross-compiler to Windows, only a FPC one.
Please adjust accordingly to your LUbuntu.
Hope this helps!!
Cheers,
Gus