Solved

System call in Delphi/Kylix

Posted on 2003-11-19
7
991 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
Comment Utility
hi, Dragon
what you mean with "system call" and "system response"?
0
 

Author Comment

by:Dragon222
Comment Utility
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
Comment Utility
You need to redirect child stdout and stderr streams.
0
 

Author Comment

by:Dragon222
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now