Recent

Author Topic: Run CMD line command from within LAZ  (Read 2532 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: Run CMD line command from within LAZ
« Reply #15 on: October 13, 2019, 11:01:06 pm »
Well, can't truly hide the window, but I can minimize it using...
Code: Pascal  [Select]
  1. Process.Parameters.Add('/min');
  2.  

Now, all I need to do is figure out how to fix the SIG error when the cmd prompt closes and tries to populate my memo control.
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7499
Re: Run CMD line command from within LAZ
« Reply #16 on: October 14, 2019, 08:27:54 am »
Your code doesn't make sense? You load the stderr pipe, but tell TProcess not to create it with postderrtooutput ?

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: Run CMD line command from within LAZ
« Reply #17 on: October 14, 2019, 01:37:37 pm »
Your code doesn't make sense? You load the stderr pipe, but tell TProcess not to create it with postderrtooutput ?

Yeah, I am just trying every option to try and figure out why i get a SIGSERV error.

I am not using that option now.

Do you know why I am getting a SIG error when the CMD prompt closes?
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7499
Re: Run CMD line command from within LAZ
« Reply #18 on: October 14, 2019, 02:12:24 pm »
No. My executecommand example didn't have that problem (and is internally also tprocess based), so something must be wrong.

Note that your code doesn't cater for "large" output (which IMHO should have been removed from all example code a long time ago).

It might have something to do with that. If you have a complex problem, you need the full "large output" code, not the quick and dirty option


jamie

  • Hero Member
  • *****
  • Posts: 2073
Re: Run CMD line command from within LAZ
« Reply #19 on: October 14, 2019, 05:02:05 pm »
I really don't understand why @PixelInk isn't doing all the directory structuring within the app and then simply call the EXE needed afterwards?
Number 1 at blue screen app creations!

lucamar

  • Hero Member
  • *****
  • Posts: 2076
Re: Run CMD line command from within LAZ
« Reply #20 on: October 14, 2019, 07:03:17 pm »
I really don't understand why @PixelInk isn't doing all the directory structuring within the app and then simply call the EXE needed afterwards?

I might be wrong but I think those are just concept tests using well-known "commands". Remember that his original post was about calling "ffmpeg".
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: Run CMD line command from within LAZ
« Reply #21 on: October 14, 2019, 09:33:32 pm »
No. My executecommand example didn't have that problem (and is internally also tprocess based), so something must be wrong.

Note that your code doesn't cater for "large" output (which IMHO should have been removed from all example code a long time ago).

It might have something to do with that. If you have a complex problem, you need the full "large output" code, not the quick and dirty option

Your code did work, but I have to manually X out to get my output in the memo. Can't have that, plus I need to minimize it so it is not shown.

Also, I don't get the SIG error with RunCommand, but I do if I use TProcess... see last code block I submitted on Page 1 for TProcess
« Last Edit: October 14, 2019, 09:37:51 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

Thaddy

  • Hero Member
  • *****
  • Posts: 9167
Re: Run CMD line command from within LAZ
« Reply #22 on: October 14, 2019, 09:40:59 pm »
A pipe will do the job (and cross platform)
also related to equus asinus.

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: Run CMD line command from within LAZ
« Reply #23 on: October 14, 2019, 10:25:51 pm »
A pipe will do the job (and cross platform)

Worked... got rid of the SIG error

I used:
Code: Pascal  [Select]
  1.  
  2. Process.Options := Process.Options + [poUsePipes];
  3.  


Now I get an output error that it doesn't like my "/min" command

'/min' is not recognized as an internal or external command,
operable program or batch file.


Code: Pascal  [Select]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   Process: TProcess;
  4.   AStringList: TStringList;
  5. begin
  6.   Process := TProcess.Create(nil);
  7.   AStringList := TStringList.Create;
  8.   try
  9.     Process.Executable := 'cmd.exe';
  10.     Process.Parameters.Add('/c');
  11.     Process.Parameters.Add('/min');
  12.     Process.Parameters.Add('dir');
  13.     Process.Parameters.Add('/s');
  14.     Process.Parameters.Add('c:\???\???\Blender\');
  15.  
  16.     Process.Options := Process.Options + [poUsePipes];
  17.     Process.Execute;
  18.  
  19.     AStringList.LoadFromStream(Process.stderr);
  20.     Memo1.Lines.Text:=AStringList.Text;
  21.  
  22.   except
  23.  
  24.          Process.Free;
  25.          AStringList.Free;
  26.  
  27.   end;
  28.   ShowMessage('done');
  29.  

So, why does '/min' work on RunCommand but not on TProcess?
« Last Edit: October 14, 2019, 10:48:13 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: Run CMD line command from within LAZ
« Reply #24 on: October 14, 2019, 11:10:21 pm »
I found this code.... WORKS PERFECT

Hides cmd and all.

The only thing I need to add to it, is the ability to display the output in memo control.

Code: Pascal  [Select]
  1.  
  2. procedure TForm1.ExecuteAndWait(FileName, Params, Directory: WideString);
  3. var
  4.   SEInfo: TShellExecuteInfoW;
  5.   ExitCode: DWORD;
  6. begin
  7.   FillChar(SEInfo, SizeOf(SEInfo), 0) ;
  8.   SEInfo.cbSize := SizeOf(TShellExecuteInfoW) ;
  9.   with SEInfo do
  10.   begin
  11.     fMask := SEE_MASK_NOCLOSEPROCESS;
  12.     Wnd := Form1.Handle;
  13.     lpFile := PWideChar(FileName);
  14.     lpParameters := PWideChar(Params);
  15.     lpDirectory := PWideChar(Directory);
  16.     nShow := SW_HIDE;  //SW_SHOW;
  17.   end;
  18.   if ShellExecuteExW(@SEInfo) then
  19.   begin
  20.     repeat
  21.       Application.ProcessMessages;
  22.       GetExitCodeProcess(SEInfo.hProcess, ExitCode) ;
  23.     until (ExitCode <> STILL_ACTIVE) or  Application.Terminated;
  24.   end;
  25. end;
  26.  
  27. procedure TForm1.Button4Click(Sender: TObject);
  28. begin
  29.  ExecuteAndWait('c:\windows\system32\cmd.exe', '/c dir /s c:\windows\system32', '');
  30.   ShowMessage('ok');
  31. end;
  32.  
  33.  
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

GeneCode

  • New Member
  • *
  • Posts: 19
  • What is the cost of lies?
Re: Run CMD line command from within LAZ
« Reply #25 on: October 16, 2019, 04:01:41 am »
Maybe I am too late but using TProcess we can hide it also using the following:

Code: Pascal  [Select]
  1. MyProcess.ShowWindow := swoHide;

MyProcess is a TProcess object of course.
Windows 10
Lazarus 1.8.4

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: Run CMD line command from within LAZ
« Reply #26 on: October 16, 2019, 02:24:28 pm »
Maybe I am too late but using TProcess we can hide it also using the following:

Code: Pascal  [Select]
  1. MyProcess.ShowWindow := swoHide;

MyProcess is a TProcess object of course.

Well, it freezes the app now.

Code: Pascal  [Select]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   Process: TProcess;
  4.   AStringList: TStringList;
  5. begin
  6.   Process := TProcess.Create(nil);
  7.   AStringList := TStringList.Create;
  8.   try
  9.     Process.Executable := 'cmd.exe';
  10.     Process.ShowWindow := swoHide;  //<- ADDED THIS LINE
  11.     Process.Parameters.Add('/c');
  12.     Process.Parameters.Add('dir');
  13.     Process.Parameters.Add('/s');
  14.     Process.Parameters.Add('c:\MyProgs\Blender\');
  15.  
  16.     Process.Options := Process.Options + [poUsePipes];
  17.     Process.Execute;
  18.     // Now read the output of the program we just ran into a TStringList.
  19.  
  20.     AStringList.LoadFromStream(Process.stderr);
  21.     Memo1.Lines.Text:=AStringList.Text;
  22.  
  23.     // Save the output to a file
  24.     //AStringList.SaveToFile('output.txt');
  25.  
  26.   except
  27.       //on E: Exception do
  28.       //   showmessage(pchar(Exception));
  29.  
  30.          Process.Free;
  31.          AStringList.Free;
  32.  
  33.   end;
  34.   ShowMessage('done');
  35.  
  36.  
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

avra

  • Hero Member
  • *****
  • Posts: 1712
    • Additional info
Re: Run CMD line command from within LAZ
« Reply #27 on: October 16, 2019, 03:57:22 pm »
The only thing I need to add to it, is the ability to display the output in memo control.
You can take a look at sources of ct2laz. It executes 7zip commands and shows console output in Memo. Cross platform and does the job.
https://bitbucket.org/avra/ct2laz/
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

pixelink

  • Hero Member
  • *****
  • Posts: 1171
    • Pixelink Software
Re: Run CMD line command from within LAZ
« Reply #28 on: October 16, 2019, 04:37:02 pm »
The only thing I need to add to it, is the ability to display the output in memo control.
You can take a look at sources of ct2laz. It executes 7zip commands and shows console output in Memo. Cross platform and does the job.
https://bitbucket.org/avra/ct2laz/

It doesn't use Shell or TProcess or RunCommand.

All this utility does is download the files and renames them and post updates to memo.

Thanks anyways.
Can't Type - Forgetful - Had Stroke = Forgive this old man!

Pixelink Software
VB.NET 2019 • LAZ 2.02 •  Win10 • 16G RAM • Nivida GForce RTX 2060

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7499
Re: Run CMD line command from within LAZ
« Reply #29 on: October 16, 2019, 05:21:24 pm »
It doesn't use Shell or TProcess or RunCommand.

It's utils.runprocess does use TProcess, but doesn't do anything new or special. It is just one of many of the homebrewn tprocess wrappers.   RunCommand is meant to be a superset of those, and in FPC 3.2+ TProcess contains the main, parameterized loop of runcommand, so that these old ones can be reimplemented to use the RunCommand mainloop.

Because of this thread I added showwindow parameters to runcommand in r43208. If it causes no problems, I'll merge it to 3.2 in a few days.

Runcommand has many overloaded forms, but looks generally something like this:


Code: Pascal  [Select]
  1. function RunCommand(const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):boolean;
  2. Var
  3.     p : TProcessnamemacro;
  4.     i,
  5.     exitstatus : integer;
  6.     ErrorString : String;
  7. begin
  8.   p:=DefaultTProcess.create(nil);
  9.   if Options<>[] then
  10.     P.Options:=Options - ForbiddenOptions;
  11.   P.ShowWindow:=SwOptions;
  12.   p.Executable:=exename;
  13.   if high(commands)>=0 then
  14.    for i:=low(commands) to high(commands) do
  15.      p.Parameters.add(commands[i]);
  16.   try
  17.     result:=p.RunCommandLoop(outputstring,errorstring,exitstatus)=0;
  18.   finally
  19.     p.free;
  20.   end;
  21.   if exitstatus<>0 then result:=false;
  22. end;
  23.  
  24.  

« Last Edit: October 18, 2019, 11:17:36 am by marcov »