Solved

System call in Delphi/Kylix

Posted on 2003-11-19
7
995 Views
Last Modified: 2010-05-18
Hi!
How to make a system call in Delphi/Kylix AND get the system responce into the application? (working under Windows and Linux)
0
Comment
Question by:Dragon222
  • 2
  • 2
7 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 9778601
hi, Dragon
what you mean with "system call" and "system response"?
0
 

Author Comment

by:Dragon222
ID: 9778802
Well...

If I run System('ls') in my Kylix application, how do I then get te output from ls back into my application (like into some Memo component)?

(Ok I can do like this, System('ls > myfile') and then read from this temporary file, but this is ugly...)
0
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 125 total points
ID: 9779331
You need to redirect child stdout and stderr streams.
0
 

Author Comment

by:Dragon222
ID: 9779370
Tanks, GloomyFriar!

But please, for a novice like me, can you write down the few lines of code that I need... without some example I'm as lost as before...
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9780453
I have no the sample code in Delphi. If it helps below is code in C (for Windows)

   // The steps for redirecting child process's STDOUT:
   //     1. Save current STDOUT, to be restored later.
   //     2. Create anonymous pipe to be STDOUT for child process.
   //     3. Set STDOUT of the parent process to be write handle to
   //        the pipe, so it is inherited by the child process.
   //     4. Create a noninheritable duplicate of the read handle and
   //        close the inheritable read handle.
 
// Save the handle to the current STDOUT.
 
   hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
 
// Create a pipe for the child process's STDOUT.
 
   if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
      ErrorExit("Stdout pipe creation failed\n");
 
// Set a write handle to the pipe to be STDOUT.
 
   if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr))
      ErrorExit("Redirecting STDOUT failed");
 
// Create noninheritable read handle and close the inheritable read
// handle.

    fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
        GetCurrentProcess(), &hChildStdoutRdDup , 0,
        FALSE,
        DUPLICATE_SAME_ACCESS);
    if( !fSuccess )
        ErrorExit("DuplicateHandle failed");
    CloseHandle(hChildStdoutRd);

// Now create the child process.
 
   if (! CreateChildProcess((LPCTSTR)run)) /* Just a wrapper to CreateProcess */
      ErrorExit("Create process failed");
 
// After process creation, restore the saved STDIN and STDOUT.
 
   if (! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout))
      ErrorExit("Re-redirecting Stdout failed\n");
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question