






Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!







menu (^) Home papers colibri_helpers stdin_stdout
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.
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:
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:
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.
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:
Here is a sample application:
This unit has been created to solve CGI server problem. In our case:
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.
Here are the source code files:
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