Recent

Author Topic: [SOLVED] Accepting Input via PIPE but not via Keyboard  (Read 2829 times)

ASBzone

  • Hero Member
  • *****
  • Posts: 633
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
[SOLVED] Accepting Input via PIPE but not via Keyboard
« on: July 18, 2018, 04:47:37 pm »
Greetings,

I am developing a Windows console app that accepts parameters at the command-line, and I have added the following to enable it to grab information via PIPE as well.    (e.g. ECHO Some Stuff Here | myapp.exe )

The pipe part works fine using the following:

Code: Pascal  [Select][+][-]
  1. while (not EOF) do
  2. begin
  3.   ReadLn(InputVar);
  4.   WriteLn('You entered: ', InputVar);
  5. end;
  6.  

However, if I run the command without having piped input, it waits for keyboard input.   What are my options for forcing STDIN to ignore the keyboard, so that it only tries to execute the ReadLn command if, in fact, data has been piped to the app?


I have seen threads such as below, which were helpful to me for getting the piping working at all.  But they don't address the other part of my issue.  ;D

https://forum.lazarus.freepascal.org/index.php?topic=16274.0
https://forum.lazarus.freepascal.org/index.php?topic=24663.0


Thanks
« Last Edit: August 08, 2018, 02:55:38 am by ASBzone »
-ASB: https://www.BrainWaveCC.com/

OLD: Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055
NEW: Lazarus v2_2_0-82-g6aebd4d4ae / FPC v3.2.3-587-g65733801e5
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

ASerge

  • Hero Member
  • *****
  • Posts: 1922
Re: Accepting Input via PIPE but not via Keyboard
« Reply #1 on: July 18, 2018, 05:15:38 pm »
I am developing a Windows console app...
What are my options for forcing STDIN to ignore the keyboard, so that it only tries to execute the ReadLn command if, in fact, data has been piped to the app?
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses Windows;
  5.  
  6. function IsStdHandleRedirected(Handle: THandle): Boolean;
  7. var
  8.   Unused: DWORD;
  9. begin
  10.   Result := (GetFileType(Handle) <> FILE_TYPE_CHAR) or not GetConsoleMode(Handle, @Unused);
  11. end;
  12.  
  13. begin
  14.   if IsStdHandleRedirected(StdInputHandle) then
  15.     Writeln('In pipe')
  16.   else
  17.     Writeln('OK')
  18. end.

ASBzone

  • Hero Member
  • *****
  • Posts: 633
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Accepting Input via PIPE but not via Keyboard
« Reply #2 on: July 18, 2018, 07:41:52 pm »
Awesome and exquisite.

Thanks, ASerge.  Works perfectly...
-ASB: https://www.BrainWaveCC.com/

OLD: Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055
NEW: Lazarus v2_2_0-82-g6aebd4d4ae / FPC v3.2.3-587-g65733801e5
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

ASerge

  • Hero Member
  • *****
  • Posts: 1922
Re: Accepting Input via PIPE but not via Keyboard
« Reply #3 on: July 18, 2018, 11:51:48 pm »
Advice for the future. If you later want to read the console output, even if you are redirected, you can do so by opening a file with a special name 'CONIN$'.

ASBzone

  • Hero Member
  • *****
  • Posts: 633
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Accepting Input via PIPE but not via Keyboard
« Reply #4 on: July 19, 2018, 01:41:32 am »
Advice for the future. If you later want to read the console output, even if you are redirected, you can do so by opening a file with a special name 'CONIN$'.

Advice accepted and stashed away safely. :D

Thanks again.
-ASB: https://www.BrainWaveCC.com/

OLD: Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055
NEW: Lazarus v2_2_0-82-g6aebd4d4ae / FPC v3.2.3-587-g65733801e5
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

 

TinyPortal © 2005-2018