Recent

Author Topic: Problems with SimpleIPC in communication between GUI and Console Programs  (Read 3196 times)

simone

  • Hero Member
  • *****
  • Posts: 573
I have some problems to implement Interprocess Comunication with SimpleIPC,
in order to allow communication between a GUI program (the server, named ide in
the attached files) and a console program (the client, named prog in the attached files).

I would expect that when I start the server, after pressing the button, it runs the client
that sends a message to show in the memo of server. But this does not happen. Moreover,
if in server code I add Process.Options:=[poWaitOnExit], the client process freezes without
doing anything.

Can someone give me some hint? Many thanks.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

CCRDude

  • Hero Member
  • *****
  • Posts: 596
"unit1" is missing in the zip, and since prog.lpr doesn't show {$APPTYPE CONSOLE}, but instead embeds a form, I can only guess it's not really a console program, but a standard GUI program.

simone

  • Hero Member
  • *****
  • Posts: 573
Unit1 is missing because does not exist such unit in the project. prog.lpr is an unused file.

However I attach a new zip for your suggestions.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

simone

  • Hero Member
  • *****
  • Posts: 573
For convenience, I report the code on the web page.

This is the server code:

Code: Pascal  [Select][+][-]
  1. program ide;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Interfaces, Forms, unit2;
  7.  
  8. {$R *.res}
  9.  
  10. begin
  11.   RequireDerivedFormResource:=True;
  12.   Application.Initialize;
  13.   Application.CreateForm(TForm1, Form1);
  14.   Application.Run;
  15. end.

Code: Pascal  [Select][+][-]
  1. unit unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, SimpleIpc, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, ExtCtrls, Process;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     Panel1: TPanel;
  19.     SimpleIPCServer: TSimpleIPCServer;
  20.     SimpleIPCServer1: TSimpleIPCServer;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure SimpleIPCServer1MessageQueued(Sender: TObject);
  23.   private
  24.     Process : TProcess;
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. begin
  40.   SimpleIPCServer:=TSimpleIPCServer.Create(nil);
  41.   SimpleIPCServer.ServerID:='simone';
  42.   SimpleIPCServer.Global:=True;
  43.   SimpleIPCServer.OnMessageQueued:=@SimpleIPCServer1MessageQueued;
  44.   SimpleIPCServer.StartServer(False);
  45.  
  46.   Process:=TProcess.Create(nil);
  47.   if FileExists('prog.exe') then
  48.     begin
  49.       Process.Executable:='prog.exe';
  50.       //Process.Options:=[poWaitOnExit];
  51.       Process.Execute;
  52.     end;
  53.   Process.Free;
  54.   SimpleIPCServer.Free;
  55. end;
  56.  
  57. procedure TForm1.SimpleIPCServer1MessageQueued(Sender: TObject);
  58. var
  59.   Msg : string;
  60. begin
  61.   SimpleIPCServer.ReadMessage;
  62.   Msg:=SimpleIPCServer.StringMessage;
  63.   Memo1.Lines.Add(Msg);
  64. end;
  65.  
  66. end.

This is the client code:

Code: Pascal  [Select][+][-]
  1. program prog;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   Classes,SimpleIpc,SysUtils;
  5. var
  6.   SimpleIPCClient: TSimpleIPCClient;
  7. begin
  8.   SimpleIPCClient:=TSimpleIPCClient.Create(nil);
  9.   SimpleIPCClient.ServerID:='simone';
  10.   if SimpleIPCClient.ServerRunning then
  11.     begin
  12.       SimpleIPCClient.Connect;
  13.       SimpleIPCClient.SendStringMessage('Hello!');
  14.     end;
  15.   SimpleIPCClient.Free;
  16. end.

Thanks in advance for the help.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Thanks in advance for the help.
Add after Process.Execute; SimpleIPCServer.PeekMessage(MSecsPerSec, False); or use SimpleIPCServer1

simone

  • Hero Member
  • *****
  • Posts: 573
Sorry for delay and thanks for the help. A further question: using TProcess, the process that launches the subprocess must run with administrator privileges?
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Sorry for delay and thanks for the help. A further question: using TProcess, the process that launches the subprocess must run with administrator privileges?

No, it just needs to have enough privileges to run that particular executable.

In Linux, for example, you can't normally run mount as a normal user ... but you can run sudo, which can do it by elevating the privileges.
« Last Edit: January 10, 2019, 02:51:15 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018