Docsity

StdIn and StdOut - Felix John COLIBRI. Programação console Delphi, Manuais, Projetos, Pesquisas de Programação em Windows

Toggle sidebar
bg1
menu Home papers colibri_helpers stdin_stdout
StdIn and StdOut - Felix John COLIBRI.
•abstract : sending and receiving strings to and from a CONSOLE project
which uses Readln and Writeln
•key words : Windows pipes, StdIn, StdOut, Readln, Writeln, CONSOLE,
CreateProcess
•software used : Windows XP, Delphi 6
•hardware used : Pentium 1.400Mhz, 256 M memory, 140 G hard disc
•scope : Delphi 1 to 8 for Windows
•level : Delphi developer
•plan :
◦Introduction
◦IO principle
◦The Delphi source code
◦Improvements
◦Download the Sources
◦References
1 - Introduction
For a simple CGI server, we had to communicate with a CGI.EXE CONSOLE application
which uses Readln and Writeln. When our web server itself is a CONSOLE application, those
input / output procedures work as expected. But when the CGI server is a GUI application,
Delphi tells us that Readln and Writeln are no longer valid.
A couple of communications found using Google and the Borland Newsgroups did provide the
solution. It was still a one day fight before I understood what was required. Here is our result,
without all possible bells and whistles, but which somehow seem to work, at least for our
simple CGI server.
2 - IO principle
2.1 - Console applications
Here is a simple my_echo.exe CONSOLE program, which reads a string, and sends it back in
uppercase:
(*$APPTYPE CONSOLE *)
program p_console_read_write_one;
var g_string: String;
begin
readln(g_string);
writeln(UpperCase(g_string));
end.
If we compile and execute
we will see a black DOS windows, waiting for input:
Felix J COLIBRI
+ Home
+ articles_with_sources
+ database
+ web_internet_sockets
+ rest_services
+ oop_components
+ uml_design_patterns
+ debug_and_test
+ graphic
+ controls
+ colibri_utilities
+ colibri_helpers
– windows_environment
– stdin_stdout
+ delphi
+ IDE
+ firemonkey
+ compilers
+ vcl
+ delphi_training
+ delphi_developments
+ sweet_home
– download_zip_sources
+ links
– Contacts
– Site Map
– search :
RSS feed
Blog
pf3
pf4
pf5
pf8
pf9
pfa

Pré-visualização parcial do texto

Baixe StdIn and StdOut - Felix John COLIBRI. Programação console Delphi e outras Manuais, Projetos, Pesquisas em PDF para Programação em Windows, somente na Docsity!

menu (^) Home papers colibri_helpers stdin_stdout

StdIn and StdOut - Felix John COLIBRI.

  • abstract : sending and receiving strings to and from a CONSOLE project which uses Readln and Writeln
  • key words : Windows pipes, StdIn, StdOut, Readln, Writeln, CONSOLE, CreateProcess
  • software used : Windows XP, Delphi 6
  • hardware used : Pentium 1.400Mhz, 256 M memory, 140 G hard disc
  • scope : Delphi 1 to 8 for Windows
  • level : Delphi developer
  • plan : ◦ Introduction ◦ IO principle ◦ The Delphi source code ◦ Improvements ◦ Download the Sources ◦ References

1 - Introduction

For a simple CGI server, we had to communicate with a CGI.EXE CONSOLE application which uses Readln and Writeln. When our web server itself is a CONSOLE application, those input / output procedures work as expected. But when the CGI server is a GUI application, Delphi tells us that Readln and Writeln are no longer valid. A couple of communications found using Google and the Borland Newsgroups did provide the solution. It was still a one day fight before I understood what was required. Here is our result, without all possible bells and whistles, but which somehow seem to work, at least for our simple CGI server.

2 - IO principle

2.1 - Console applications

Here is a simple my_echo.exe CONSOLE program, which reads a string, and sends it back in uppercase: (*$APPTYPE CONSOLE *) program p_console_read_write_one; var g_string: String; begin readln(g_string); writeln(UpperCase(g_string)); end. If we compile and execute we will see a black DOS windows, waiting for input:

Felix J COLIBRI

  • Home
  • articles_with_sources
  • database
  • web_internet_sockets
  • rest_services
  • oop_components
  • uml_design_patterns
  • debug_and_test
  • graphic
  • controls
  • colibri_utilities
  • colibri_helpers
  • windows_environment
  • stdin_stdout
  • delphi
  • IDE
  • firemonkey
  • compilers
  • vcl
  • delphi_training
  • delphi_developments
  • sweet_home
  • download_zip_sources
  • links
  • Contacts
  • Site Map
  • search : RSS feed Blog

we type some string on the keyboard and hit Enter, like Hello Enter the string will be echoed back And since this is the last instruction, the program will terminate (and you won't see anything unless you block the program with another Readln before the END, and type Enter to quit the program). Basically, the keyboard feeds the Readln, and Writeln sends the output to the screen. In C parlance, Readln grabs input from the StdIn handle, and Writeln sends characters to the StdOut handle.

In our case, the GUI application will create two pipes:

  • a std_in pipe where the my_gui application pushes the string, and my_echo will use for its Readln calls
  • a std_out pipe where my_echo will place its Writeln strings and that my_gui will use to get this string Therefore
  • my_gui creates both pipes
  • it launches my_echo by creating a Windows Process with special parameters to force my_echo to use our pipes for StdIn and StdOut
  • after my_eco is started ◦ my_gui write a string ◦ my_echo reads this string and writes the uppercase string and terminates ◦ my_gui reads the uppercase string ◦ everything is cleaned up

3 - The Delphi source code

3.1 - The echo program

The echo program Reads and Writes, and we also added logging to be able to monitor what's going on. Here is the program: (*$APPTYPE CONSOLE *) program p_console_read_write_one; var g_string: String; begin readln(g_string); writeln(UpperCase(g_string)); end.

3.2 - The u_c_stdin_sdtout_exe unit

This unit creates the pipes, starts the console exe and can read and write strings to and from the console exe.

Since the pipe uses two handles, we placed them in a record: t_pipe= record m_read_handle, m_write_handle: tHandle; end; And the handles are created with this function: function f_create_pipe(Var pv_pipe: t_pipe): boolean; const k_pipe_buffer_size= 4096; begin with pv_pipe do begin // -- Create the pipe result:= CreatePipe(m_read_handle, m_write_handle, nil, k_pipe_buffer_size); // -- recreate the handles, this time with the "inheritable" flag if result then result:= DuplicateHandle(GetCurrentProcess, m_read_handle, GetCurrentProcess, @ m_read_handle, 0, True, DUPLICATE_CLOSE_SOURCE OR DUPLICATE_SAME_ACCESS); if result then result:= DuplicateHandle(GetCurrentProcess, m_write_handle, GetCurrentProcess, @ m_write_handle, 0, True, DUPLICATE_CLOSE_SOURCE OR DUPLICATE_SAME_ACCESS); end; end; // f_create_pipe procedure close_pipe(p_pipe: t_pipe); begin with p_pipe do begin CloseHandle(m_read_handle); CloseHandle(m_write_handle); end; end; // close_pipe Note that:

  • the buffer size value included in the CreatePipe is irrelevant, since each ReadFile and WriteFile has its own size parameters
  • the strange DuplicateHandle call is mandatory to allow the handles to be inherited by the child process that will be created later. It seems that the same result could be obtained by using an NT security record during the CreatePipe call, but this record has to be created, initialized etc.
  • our creation function will be called to initialize both StdIn and StdOut pipes The console .EXE is launched as a child process of the gui process, using the Windows CreateProcess function: function CreateProcess(lpApplicationName: PChar; lpCommandLine: PChar; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: PChar; const lpStartupInfo: TStartupInfo; var lpProcessInformation: TProcessInformation): BOOL; stdcall;

3.3 - The gui application

Here is a sample application:

  • clicking the "start_" button will ◦ create a c_stdin_stdout_exe object ◦ call the f_create_stdin_stdout pipes ◦ fill the m_exe_name with p_console_read_write.exe ◦ call f_create_process
  • clicking "do_write_" will sent Edit1.Text to p_console_read_write.exe
  • clicking "do_read_" will fetch the any string that p_console_read_write.Write has sent
  • kill can abort the p_console_read_write.exe Here is a snapshot of the project:

4 - Improvements

This unit has been created to solve CGI server problem. In our case:

  • our simple server will send a "small" string (like 100 or 200 character long) to the CGI exe
  • the CGI.EXE only peforms one Readln, processes this string, which results in a single Writeln. And after this Writeln, the CGI.EXE reaches the end and the CGI.EXE terminates.
  • the CGI server will read back the "small" HTML page (also about 100 or 200 characters) We did not spend much time to investigate other communication possibilities and on "industrial strength" issues:
  • are all the handles closed at the end, or if some exception is raised
  • what happens to programs which require many read / write sequences
  • can we transfer huge blocks (several megs). If the CONSOLE sends a big amount in one Write, is a single read enough? What if the CGI.EXE calls several Writeln? Let us mention some possible solutions:
  • in the Borland Technical Information note, there is a read / write loop similar to:

while WaitForSingleObject(m_process_handle, 0)<> WAIT_OBJECT_0 do begin // -- check if the process is still active GetExitCodeProcess(m_process_handle, l_process_exit_code); if l_process_exit_code<> STILL_ACTIVE then break; // -- check to see if there is any data to read from stdout PeekNamedPipe(m_stdout_pipe.m_read_handle, @ m_reception_buffer, 1024, @ l_bytes_to_read_count, @ l_bytes_read_count, nil); if l_bytes_read_count<> 0 then begin FillChar(m_reception_buffer, sizeof(m_reception_buffer), 'z'); // -- read by 1024 bytes chunks l_bytes_read_remaining_count:= l_bytes_read_count; while l_bytes_read_remaining_count>= 1023 do begin // -- read the stdout pipe ReadFile(m_stdout_pipe.m_read_handle, m_reception_buffer, 1024, l_bytes_read_count, nil); Dec(l_bytes_read_remaining_count, l_bytes_read_count); handle_received(l_bytes_read_count); end; if l_bytes_read_remaining_count> 0 then begin ReadFile(m_stdout_pipe.m_read_handle, m_reception_buffer, l_bytes_read_remaining_count, l_bytes_read_count, nil); display('read '+ IntToStr(l_bytes_read_count)); handle_received(l_bytes_read_count); end; end; // received some bytes // -- now write some WriteFile(m_stdin_pipe.m_write_handle, 'xxx', Length('xxx'), l_bytes_write_count, Nil); end; // while true This is the time-honored "busy waiting" loop, which blocks the execution alternately reading and writing.

  • the other suggestion is to use threads for reading and writing

5 - Download the Sources

Here are the source code files:

  • console_read_write.zip: the CONSOLE application with Readln and Writeln (5 K)
  • gui_write_read.zip: the gui project with the CreateProcess call unit (23 K) Those .ZIP files contain:
  • the main program (.DPR, .DOF, .RES), the main form (.PAS, .DFM), and any other auxiliary form
  • any .TXT for parameters
  • all units (.PAS) for units Those .ZIP

forthcoming Delphi, FireBird, Tcp/IP, Web Services, OOP / UML, Design Patterns, Unit Testing training sessions. Created: nov-04. Last updated: dec-2019 - 103 articles, 239 .ZIP sources, 1292 figures Contact : Felix COLIBRI - Phone: (33)1.42.83.69.36 / 06.87.88.23.91 - email:fcolibri@felix- colibri.com Copyright © Felix J. Colibri http://www.felix-colibri.com 2004 - 2019. All rigths reserved Back: Home Papers Training Delphi developments Links Download